xampp + PHPMailer + Gmail + XOAuth2 = SMTP错误:无法连接到SMTP主机

问题描述:

我已经使用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代码。这是因为普通的网络应用被认为不太安全。 –

这与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