0

I am trying to use PHPMailer with XAMPP on a localhost server and I am using the Gmail SMTP, however SMTP will not connect. Can someone help me find the issue? I am using the same Gmail example they have on their Github. The issue was resolved when I changed the SMTPOptions as the troubleshooting guide said, however, if it is an insecure option, I would like to do it the correct way.

This is the Error message that the mailer.php outputs, and I have tried troubleshooting from their Github, but can't seem to find a solution:

2020-03-21 18:04:57 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f2sm448312ljn.101 - gsmtp
2020-03-21 18:04:57 CLIENT -> SERVER: EHLO 127.0.0.1
2020-03-21 18:04:57 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa7:460f:7e0c:c0fb:2279:35d7:a54a]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2020-03-21 18:04:57 CLIENT -> SERVER: STARTTLS
2020-03-21 18:04:58 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2020-03-21 18:04:58 CLIENT -> SERVER: QUIT
2020-03-21 18:04:58 SERVER -> CLIENT:
2020-03-21 18:04:58 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

My settings:
PHP.ini:
SMTP=smtp.gmail.com
smtp_port=587

Sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
auth_username=EMAIL
auth_password=PASSWORD

My Mailer.php:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'PHPMailer/vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'EMAIL';                     // SMTP username
    $mail->Password   = 'PASSWORD';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('EMAIL', 'NAME');
    $mail->addAddress('EMAIL', 'Joe User');     // Add a recipient


    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
  • See this answer https://stackoverflow.com/a/60502415/12232340 and you need to allow google account for less secure connection see in this answer https://stackoverflow.com/a/60394732/12232340 –  Mar 21 '20 at 18:17
  • That's not the problem. It's failing immediately after STARTTLS, so it's most likely that your CA certificates are outdated, or the ISP is redirecting SMTP traffic and so validation fails (as it should). Both of these are described in great detail in [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#certificate-verification-failure), and also in many identical questions on here - please [search before you post](https://stackoverflow.com/search?q=phpmailer+xampp+gmail)! – Synchro Mar 21 '20 at 18:23
  • I have already allowed my Google account for less secure connection. It works when I change the SMTP options as it says in the troubleshooting guide, but they state that it is a less secure option, therefore, I would like to implement it the correct way. – displayNamePHP Mar 21 '20 at 18:38

0 Answers0