问题
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