PHP Fatal Error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Unable to connect to SMTP host
P粉128563140
P粉128563140 2023-08-31 10:04:24
0
1
583
<p>I tried to use the Forgot Password feature in PHPMailer and got this error: </p> <pre class="brush:php;toolbar:false;">[12-May-2023 17:53:32 UTC] PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host . in /home/raso1970/cliquedigitalepdo.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php:2233 Stack trace: #0 /home/raso1970/cliquedigitalepdo.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(2019): PHPMailer\PHPMailer\PHPMailer->smtpConnect() #1 /home/raso1970/cliquedigitalepdo.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1678): PHPMailer\PHPMailer\PHPMailer->smtpSend() #2 /home/raso1970/cliquedigitalepdo.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->postSend() #3 /home/raso1970/cliquedigitalepdo.com4muz.com/forgot.php(75): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in /home/raso1970/cliquedigitalepdo.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2233</pre> <p>This is my code in forget.php: </p> <pre class="brush:php;toolbar:false;"><?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; ?> <?php ob_start(); ?> <?php include "config/db.php"; ?> <?php include "includes/header.php"; ?> <!-- LOGIN --> <?php require './vendor/autoload.php'; if(!isset($_GET['forgot'])) { redirect('https://cliquedigitalepdo.com4muz.com/'); }if(IfItIsMethod('post')) { if(isset($_POST['email'])) { $email = $_POST['email']; $length = 50; $token = bin2hex(openssl_random_pseudo_bytes($length)); if(email_exists($email)) { if($stmt = $connection->prepare("UPDATE users SET token = :token WHERE user_email = :email")) { $stmt->bindValue(':token', $token); $stmt->bindValue(':email', $email); $stmt->execute([ 'token' => $token, 'email' => $email ]); // $stmt->close(); /*** * * Configure PHPMailer * **/ $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = Config::SMTP_HOST; $mail->Username = Config::SMTP_USER; $mail->Password = Config::SMTP_PASSWORD; $mail->Port = Config::SMTP_PORT; $mail->SMTPSecure = 'PHPMailer::ENCRYPTION_SMTPS'; $mail->SMTPAuth = 'ssl'; $mail->isHTML(true); $mail->CharSet = 'UTF-8'; $mail->setFrom('contact@com4muz.com'); $mail->addAddress('contact@com4muz.com'); $mail->Subject = 'Veuillez réinitialiser votre mot de passe'; $mail->Body = '<p>Cliquez sur le lien pour réinitialiser votre mot de passe <a href="https://cliquedigitalepdo.com4muz.com/reset.php?email='.$email.'&token='.$token.' ">https://cliquedigitalepdo.com4muz.com/reset.php?email='.$email.'&token='.$token.'</a> </p>'; if($mail->send()) { $emailSent = true; } else { echo "NOT SENT"; } } } } }?> <main class="main__content_wrapper"> <!-- Start breadcrumb section --> <section class="breadcrumb__section breadcrumb__bg"> <div class="container"> <div class="row row-cols-1"> <div class="col"> <div class="breadcrumb__content text-center"> <h1 class="breadcrumb__content--title mb-10">Mot de Passe Oublié</h1> <ul class="breadcrumb__content--menu d-flex justify-content-center"> <li class="breadcrumb__content--menu__items"><a href="https://cliquedigitalepdo.com4muz.com/">Accueil</a></li> <li class="breadcrumb__content--menu__items"><span class="text__secondary">Mot de Passe Oublié</span></li> </ul> </div> </div> </div> </div> <img class="breadcrumb__section--shape--bg" src="https://cliquedigitalepdo.com4muz.com/assets/img/other/breadcrumb-shape-bg.webp" alt="breadcrumb-img"> </section> <!-- End breadcrumb section --> <!-- Start login section --> <div class="login__section section--padding"> <?php if(!isset($emailSent)): ?> <div class="container"> <form method="post"> <div class="login__section--inner"> <div class="row row-cols-md-1 row-cols-1"> <div class="col"> <div class="account__login"> <div class="account__login--header mb-25"> <h2 class="account__login--header__title h3 mb-10 text-center">Vous Avez Oubliez Votre Mot de passe ?</h2> <p class="account__login--header__desc text-center">Entrez votre adresse email</p> </div> <div class="account__login--inner"> <label> <input name="email" class="account__login--input" placeholder="Adresse Email" type="email"> </label> <button class="account__login--btn primary__btn" name="login" type="submit">Envoyer</button> </div> </div> </div> </div> </div> </form> </div> <?php else: ?> <h4>Veuillez Vérifier Votre Boîte Mail</h4> <?php endIf; ?> </div> <!-- End login section --> <!-- Start newsletter section --> <section class="newsletter__section newsletter__bg2 section--padding"> <div class="container"> <div class="row row-cols-md-2 row-cols-1 align-items-center"> <div class="col"> <div class="newsletter__content"> <h2 class="newsletter__content--title text-white">Rejoignez Notre Newsletter</h2> <p class="newsletter__content--desc text-white">Devenez les premiers à découvrez toutes nos actualités relatives au monde du digital et les nouvelles tendances</p> </div> </div> <div class="col"> <div class="newsletter__subscribe"> <form class="newsletter__subscribe position__relative" action="#"> <label> <input class="newsletter__subscribe--input" placeholder="Entrez votre adresse email" type="email"> </label> <button class="newsletter__subscribe--button style2 primary__btn" type="submit">S'inscrire <svg class="newsletter__subscribe--button__icon" xmlns="http://www.w3.org/2000/svg" width="14.34" height="14.34" viewBox="0 0 14.34 14.34"> <path id="ic_near_me_24px" d="M17.34,3,3,9V9.78l5.449,2.111,2.1,5.449h.781Z" transform="translate(-3 -3)" fill="currentColor"/> </svg> </button> </form> </div> </div> </div> </div> </section> <!-- End newsletter section --> </main> <?php include "includes/footer.php" ?></pre> <p>I tried commenting out these two lines: </p> <pre class="brush:php;toolbar:false;">use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception;</pre> <p>It doesn't seem to be able to connect to SMTP, but I'm using the same SMTP configuration to send emails from contact.php and it works. </p> <p><strong>PS: </strong>It’s worth noting that I’m using the same code locally and it works. Therefore, the problem only exists online. </p> <p>I use the same PHP version 8.2 for both local and online applications. </p>
P粉128563140
P粉128563140

reply all(1)
P粉094351878

I have sorted it out. There is actually a typo on line 60 of forgot.php.

For some reason I wrote single quotes. So, I replaced this:

$mail->SMTPSecure = 'PHPMailer::ENCRYPTION_SMTPS';

through this:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

Now, it works and I can send emails to reset my password.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template