2017-05-16 180 views
1

我想在AWS中設置PHPMailer。它返回錯誤說SMTP connect()失敗。PHPMailer SMTP連接錯誤

代碼

<?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 = "[email protected]"; 
$mail->oauthClientId = "client-key.apps.googleusercontent.com"; 
$mail->oauthClientSecret = "secretkey"; 
$mail->oauthRefreshToken = "refresh_token"; 
$mail->setFrom('[email protected]', 'ParkIt'); 
$mail->addAddress("[email protected]", "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!"; 
} 

?> 

調試日誌

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安全規則 AWS Security Rules

+1

那麼,你的錯誤日誌指出'SMTP錯誤:無法驗證'。你有沒有設置正確的憑據? – Justinas

+0

您是否已經完成了[PHPMailer wiki中的Google xoauth2指南](https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2)? – Synchro

回答

1

最後,我下定決心米問題。過程中,我發現了一些有用的資源(SMTP認證過程中可能會導致類似的問題。)

解決方案 你必須包括即使您正在使用的作曲家PHPMailerAutoload.php文件的代碼。

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

加入羣組Allow Risky Access Permissions By Unreviewed Apps爲了執行未經審覈/開發/單SMTP認證的應用程序。您可以從here瞭解更多關於它的信息。

相關問題