PHPMailer SMTP Connect Error

不羁的心 提交于 2019-12-11 06:35:23

问题


I'm trying to setup PHPMailer in AWS. It returns error saying SMTP connect() failed.

Code

<?php
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug =2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$mail->oauthUserEmail = "xxx@gmail.com";
$mail->oauthClientId = "client-key.apps.googleusercontent.com";
$mail->oauthClientSecret = "secretkey";
$mail->oauthRefreshToken = "refresh_token";
$mail->setFrom('tittuonnet@gmail.com', 'ParkIt');
$mail->addAddress("tittuhpd@gmail.com", "Tittu Varghese");
$mail->Subject = 'ParkIt - Verification Code';
$mail->Body = 'Please verify your account';

$mail->send();
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

Debug log

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 22sm11056028qkv.52 - gsmtp
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection 22sm11056028qkv.52 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s191sm6833675qke.56 - gsmtp
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection s191sm6833675qke.56 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

AWS Security Rules


回答1:


Finally, I resolved my issue. I have found some useful resources during the process (which may cause similar issues during SMTP authentication.)

Solution You have to include PHPMailerAutoload.php file to the code even if you are using composer.

require 'PHPMailer/PHPMailerAutoload.php';
require 'vendor/autoload.php';

Join the group Allow Risky Access Permissions By Unreviewed Apps in order to execute unreviewed / development / single SMTP authenticated applications. You can read more about it from here.



来源:https://stackoverflow.com/questions/43999573/phpmailer-smtp-connect-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!