【问题标题】:PHPMailer SMTP settings for Google Suite on BluehostBluehost 上 Google 套件的 PHPMailer SMTP 设置
【发布时间】:2020-01-13 18:57:11
【问题描述】:

我正在尝试使用 PHPMailer 和我的 Google Suite 电子邮件 (info@mydomain.com) 使用 PHP 发送电子邮件。

在阅读了 Stack Overflow 的其他帖子并尝试了所有我无法使其工作的方法之后。我还在正确的 g 套件帐户中将“允许不太安全的应用程序”设置为“true”。

这是我不断收到的错误:

使用 SSL 和端口 465:

2019-09-11 22:27:33 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2019-09-11 22:27:33 Connection failed. Error #2: stream_socket_client(): Peer certificate CN=`*.bluehost.com' did not match expected CN=`smtp.gmail.com' [/home2/.../includes/PHPMailer/src/SMTP.php line 326]
2019-09-11 22:27:33 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/home2/lucapenn/public_html/algofinder.com/admin/includes/PHPMailer/src/SMTP.php line 326]
2019-09-11 22:27:33 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [/.../includes/PHPMailer/src/SMTP.php line 326]
2019-09-11 22:27:33 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

使用 TLS 和端口 587:

2019-09-12 08:12:39 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2019-09-12 08:12:39 Connection: opened
2019-09-12 08:12:39 SMTP INBOUND: "220-box5141.bluehost.com ESMTP Exim 4.92 #2 Thu, 12 Sep 2019 03:12:39 -0500"
2019-09-12 08:12:39 SMTP INBOUND: "220-We do not authorize the use of this system to transport unsolicited,"
2019-09-12 08:12:39 SMTP INBOUND: "220 and/or bulk e-mail."
2019-09-12 08:12:39 SERVER -> CLIENT: 220-box5141.bluehost.com ESMTP Exim 4.92 #2 Thu, 12 Sep 2019 03:12:39 -0500 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2019-09-12 08:12:39 CLIENT -> SERVER: EHLO mydomain.com
2019-09-12 08:12:39 SMTP INBOUND: "250-box5141.bluehost.com Hello mydomain.com [162.241.224.176]"
2019-09-12 08:12:39 SMTP INBOUND: "250-SIZE 52428800"
2019-09-12 08:12:39 SMTP INBOUND: "250-8BITMIME"
2019-09-12 08:12:39 SMTP INBOUND: "250-PIPELINING"
2019-09-12 08:12:39 SMTP INBOUND: "250-AUTH PLAIN LOGIN"
2019-09-12 08:12:39 SMTP INBOUND: "250-STARTTLS"
2019-09-12 08:12:39 SMTP INBOUND: "250 HELP"
2019-09-12 08:12:39 SERVER -> CLIENT: 250-box5141.bluehost.com Hello algofinder.com [162.241.224.176]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2019-09-12 08:12:39 CLIENT -> SERVER: STARTTLS
2019-09-12 08:12:39 SMTP INBOUND: "220 TLS go ahead"
2019-09-12 08:12:39 SERVER -> CLIENT: 220 TLS go ahead
2019-09-12 08:12:39 Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=`*.bluehost.com' did not match expected CN=`smtp.gmail.com' [/home2/.../PHPMailer/src/SMTP.php line 405]
SMTP Error: Could not connect to SMTP host.
2019-09-12 08:12:39 CLIENT -> SERVER: QUIT
2019-09-12 08:12:39
2019-09-12 08:12:39 SMTP INBOUND: ""
2019-09-12 08:12:39
2019-09-12 08:12:39
2019-09-12 08:12:39 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

这是我的 PHP 代码:

$mail = new PHPMailer(true);

try {

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 4; 
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; //or SSL
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; //or 587
$mail->Username = 'info@mydomain.com';  
$mail->Password = 'mypassword';           

$mail->setFrom($from, 'MyDomain');
$mail->addAddress($to);   

$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body_html;
$mail->AltBody = $body_txt;

$mail->send();

} catch (Exception $e) {

echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

我只想向收件箱发送一些普通的电子邮件。 PHPMailer 和 Google Suite 可以吗?还有其他更可靠的解决方案可以到达收件箱吗?

【问题讨论】:

    标签: php email smtp phpmailer bluehost


    【解决方案1】:

    这是你的问题:

    2019-09-11 22:27:33 连接失败。错误 #2:stream_socket_client(): Peer certificate CN=*.bluehost.com' did not match expected CN=smtp.gmail.com' [/home2/.../includes/PHPMailer/src/SMTP.php line 326]

    您要求连接到smtp.gmail.com,但您最终连接到了bluehost.com 主机。这将是因为您的托管服务提供商正在使用他们的防火墙将端口 465 上的出站流量重定向到他们自己的邮件服务器。这会导致证书验证失败,因为证书名称不匹配。从这个意义上说,TLS 完全按照预期工作,检测什么相当于中间人攻击。但是,鉴于您知道这个“攻击者”是谁,这并不是特别有用。 Bluehost 可能有一些安排,允许您通过他们自己的邮件服务器发送电子邮件,如果您切换到它,证书名称将匹配。

    请记住,即使您确实解决了这个问题,还有另一个问题在等着您:gmail 具有严格的 SPF 和 DMARC 设置,这意味着您无法从 gmail.com 地址发送电子邮件,除非您通过 gmail 发送自己的服务器。如果您在 gmail 中使用自己的域名,您至少可以将 Bluehost 的 SPF 记录添加到您自己的域名中,这将允许从那里发送。

    从错误消息链接的the troubleshooting guide 中涵盖了这种确切情况。

    【讨论】:

    • 嗨同步,谢谢你的回答。由于我在共享主机上,除了切换到 bluehost 并放弃 g 套件之外,有没有其他可能的方法来解决这个问题? (我不信任 bluehost)
    • 我建议询问 bluehost 支持 - 默认情况下通常会阻止出站 SMTP,但可能会根据要求允许。
    猜你喜欢
    • 2018-01-10
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2011-09-06
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多