Waleed Afzal
Waleed Afzal

Reputation: 21

PHPMailer do not work properly sometimes on gmail port 465 or 587 on localhost

I am using the latest version of PHPMailer 6.0.2 on LOCALHOST with Gmail SMTP Ports [ SSL on 465 and TLS 587 ]. It works well but not stable or works properly, very strange while OpenSSL Extension is also active in PHP Config /PHP.ini file.

It returns the error " SMTP Error: Could not connect to SMTP host ", sometimes on 465 or 587 port.

As right now it's on 587.

Here the exact error on port 587 ;

2017-12-05 13:00:26 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2017-12-05 13:00:26 Connection: opened
2017-12-05 13:00:26 SMTP INBOUND: "220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp"
2017-12-05 13:00:26 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp
2017-12-05 13:00:26 CLIENT -> SERVER: EHLO localhost
2017-12-05 13:00:27 SMTP INBOUND: "250-smtp.gmail.com at your service, [110.36.136.72]"
2017-12-05 13:00:27 SMTP INBOUND: "250-SIZE 35882577"
2017-12-05 13:00:27 SMTP INBOUND: "250-8BITMIME"
2017-12-05 13:00:27 SMTP INBOUND: "250-STARTTLS"
2017-12-05 13:00:27 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2017-12-05 13:00:27 SMTP INBOUND: "250-PIPELINING"
2017-12-05 13:00:27 SMTP INBOUND: "250-CHUNKING"
2017-12-05 13:00:27 SMTP INBOUND: "250 SMTPUTF8"
2017-12-05 13:00:27 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [110.36.136.72]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-05 13:00:27 CLIENT -> SERVER: STARTTLS
2017-12-05 13:00:27 SMTP INBOUND: ""
2017-12-05 13:00:27 SERVER -> CLIENT: 
2017-12-05 13:00:27 SMTP ERROR: STARTTLS command failed: 
SMTP Error: Could not connect to SMTP host.
2017-12-05 13:00:27 SMTP NOTICE: EOF caught while checking if connected
2017-12-05 13:00:27 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting     
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

HERE IS THE RELEVANT CODE:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;

require '../../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;

$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

$mail->Username = $from_address;
$mail->Password = $from_password;
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->SetFrom($from_address, $from_name);

foreach ($to_email_list as $to) {
    $mail->AddAddress($to);
}

$mail->AddReplyTo($from_address, $from_name);
$mail->Subject = $email_subject;

//Creating Email Body
$message = "<html>\n";
$message .= "<body>\n";
$message .= '<p>Greetings,</p>';
$message .= '<p>' . $email_message . '</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$mail->isHTML(true);
$mail->MsgHTML($message);

if(!$mail->Send()) {
    echo "On Port: " . $from_smtp_port . " </br> Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!  on Port " . $from_smtp_port . "</br>";

   foreach($to_email_list as $list){
        echo $list . "</br>";
    }

}

Upvotes: 1

Views: 1403

Answers (1)

Waleed Afzal
Waleed Afzal

Reputation: 21

Finally, I found the solution on troubleshooting guide.

I recommend to other users who are facing the same issue; please see following Points carefully on troubleshooting guide; - Opportunistic TLS - PHP 5.6 certificate verification failure - cURL error 60

My Script is working fine with Gmail ss/465 and tls/587.

Thanks! @Synchro.

Upvotes: 1

Related Questions