简体   繁体   English

Bluehost上Google Suite的PHPMailer SMTP设置

[英]PHPMailer SMTP settings for Google Suite on Bluehost

I'm trying to send emails with PHP using PHPMailer and my Google Suite email (info@mydomain.com). 我正在尝试使用PHPMailer和我的Google Suite电子邮件(info@mydomain.com)使用PHP发送电子邮件。

After having read other posts from Stack Overflow and tried everything I am not able to make it work. 在阅读了Stack Overflow的其他文章并尝试了所有方法之后,我无法使其工作。 I've also set "allow less secure apps" to "true" in the correct g suite account. 我还在正确的g套件帐户中将“允许不太安全的应用程序”设置为“ true”。

Here is the error I keep getting: 这是我不断得到的错误:

With SSL and port 465: 使用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

With TLS and port 587: 使用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

Here's my PHP code: 这是我的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}";

}

I'd just like to send some normal emails to the inbox. 我只想向收件箱中发送一些普通电子邮件。 Is it possible with PHPMailer and Google Suite? PHPMailer和Google Suite可以吗? Are there other more reliable solutions that are able to reach the inbox? 还有其他更可靠的解决方案可以到达收件箱吗?

Here's your problem: 这是您的问题:

2019-09-11 22:27:33 Connection failed. 2019-09-11 22:27:33连接失败。 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] 错误2:stream_socket_client():对等证书CN = *.bluehost.com' did not match expected CN= smtp.gmail.com' *.bluehost.com' did not match expected CN= [/home2/.../includes/PHPMailer/src/SMTP.php第326行]

You're asking to connect to smtp.gmail.com , but you're ending up connected to a bluehost.com host. 您要连接到smtp.gmail.com ,但最终要连接到bluehost.com主机。 This will be because your hosting provider is redirecting outbound traffic on port 465 to their own mail server using their firewall. 这是因为您的托管服务提供商正在使用防火墙将端口465上的出站流量重定向到他们自己的邮件服务器。 This is causing a certificate verification failure because the certificate names to not match. 由于证书名称不匹配,这导致证书验证失败。 In this sense TLS is working exactly as intended, detecting what amounts to a man-in-the-middle attack. 从这个意义上讲,TLS完全按照预期的方式工作,可以检测到什么构成中间人攻击。 However, this isn't especially helpful given that you know who this "attacker" is. 但是,鉴于您知道此“攻击者”是谁,这并不是特别有用。 Bluehost probably has some arrangement to allow you to send email through their own mail server, and the certificate name will match if you switch to it. Bluehost可能已进行了一些安排,使您可以通过自己的邮件服务器发送电子邮件,并且如果您切换到它,则证书名称将匹配。

Bear in mind that even if you do make your way around this problem, there's another one waiting for you: gmail has strict SPF and DMARC settings that mean that you cannot send email from a gmail.com address unless you send through gmail's own servers. 请记住,即使您确实解决了此问题,也有另一个问题在等待您:gmail具有严格的SPF和DMARC设置,这意味着除非您通过gmail自己的服务器发送邮件,否则您无法从gmail.com地址发送电子邮件。 If you're using your own domain name with gmail, you can at least add Bluehost's SPF records to your own which will permit sending from there. 如果您将自己的域名与gmail一起使用,则可以至少将Bluehost的SPF记录添加到自己的域名中,以允许从那里进行发送。

This exact situation is covered in the troubleshooting guide that's linked from the error message. 从错误消息链接的疑难解答指南中涵盖了这种确切情况。

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

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