繁体   English   中英

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

[英]xampp + PHPMailer + Gmail = SMTP Error: Could not connect to SMTP host

我已经使用Composer安装了PHPMailer,并添加了所有Composer依赖项以使用Google XOAuth2身份验证。 然后,我按照一些教程使用Gmail API获取刷新令牌。
一切都应该正常工作。 我正确地完成了所有文书工作。 对?!

尽管有我最好的意图和所有文档,但我无法建立与smtp.gmail.com的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结合使用的方法?

这与OAuth无关。 在TLS级别上,您有一个更早的问题。 您缺少openssl扩展名,配置错误或您的CA证书已过期。

故障排除指南中的错误链接涵盖了此问题-使用openssl检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM