2017-10-20 186 views
0

我已經使用Composer安裝了PHPMailer,並添加了所有Composer依賴項以使用Google XOAuth2身份驗證。 然後我按照一些教程來獲取帶有Gmail API的刷新令牌。
一切都應該工作得很好。我做了所有的文件工作。對?!

儘管我最好的意圖,所有的文檔,我不能夠建立SMTP連接爲smtp.gmail.com

我得到這個錯誤:xampp + PHPMailer + Gmail + XOAuth2 = SMTP錯誤:無法連接到SMTP主機

2017-10-20 18:01:45 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c17sm2715728wrg.26 - gsmtp 
2017-10-20 18:01:45 CLIENT -> SERVER: EHLO localhost 
2017-10-20 18:01:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [151.61.40.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 
2017-10-20 18:01:45 CLIENT -> SERVER: STARTTLS 
2017-10-20 18:01:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 
SMTP Error: Could not connect to SMTP host. 
2017-10-20 18:01:45 CLIENT -> SERVER: QUIT 
2017-10-20 18:01:45 
2017-10-20 18:01:45 
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 


我的代碼是直接從PHPMailer Gmail XOAUTH示例。

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\OAuth; 

use League\OAuth2\Client\Provider\Google; 

date_default_timezone_set('Europe/Rome'); 

require 'lib/php/vendor/autoload.php'; 

$mail = new PHPMailer; 

$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587; 
$mail->SMTPSecure = 'tls'; 
$mail->SMTPAuth = true; 
$mail->AuthType = 'XOAUTH2'; 

$email = 'seckrit-stuff'; 
$clientId = 'seckrit-stuff'; 
$clientSecret = 'seckrit-stuff'; 
$refreshToken = 'seckrit-stuff'; 

$provider = new Google(
    [ 
     'clientId' => $clientId, 
     'clientSecret' => $clientSecret, 
    ] 
); 
$mail->setOAuth(
    new OAuth(
     [ 
      'provider' => $provider, 
      'clientId' => $clientId, 
      'clientSecret' => $clientSecret, 
      'refreshToken' => $refreshToken, 
      'userName' => $email, 
     ] 
    ) 
); 

$mail->setFrom($email, 'Matteo Bini'); 
$mail->addAddress('seckrit-stuff', 'Matteo Bini'); 
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test'; 
$mail->CharSet = 'utf-8'; 
$mail->msgHTML('<strong>HTML</strong> message!'); 
$mail->AltBody = 'This is a plain-text message body'; 


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


能否請你幫我找到一種方法在本地主機(XAMPP)使用的PHPMailer與Gmail?

+0

嘗試在Google帳戶中檢查「允許安全性較低的應用:關」設置。您可能需要打開它才能使用GMail和PHP代碼。這是因爲普通的網絡應用被認爲不太安全。 –

回答

2

這與OAuth無關。您在TLS級別有更早的問題。要麼你錯過了openssl擴展,它配置錯誤,或者你的CA證書已經過期。

此問題在錯誤鏈接指向的錯誤鏈接中進行了介紹 - 使用openssl進行檢查。

+0

在發佈問題之前,我已經閱讀了故障排除指南,並且在xampp php.ini文件中啓用了openssl擴展。 使用故障排除指南技巧我用echo測試了擴展(extension_loaded('openssl')?'SSL loaded':'SSL not loaded')。「\ n」; 它說SSL加載。 我應該做更多或不同的事情嗎? – matteobin

+0

這很好,但請嘗試指南中的openssl命令行示例。也嘗試SMTPDebug = 3。 – Synchro

+0

我解決了我的問題。謝謝! 我使用SMTPDebug = 3,我發現錯誤是什麼,並將這行代碼添加到php.ini中openssl.cafile = C:\ xampp \ php \ extras \ ssl \ cacert.pem。很明顯,從https://curl.haxx.se/docs/caextract.html下載cacert.pem之後 – matteobin

相關問題