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).

After having read other posts from Stack Overflow and tried everything I am not able to make it work. I've also set "allow less secure apps" to "true" in the correct g suite account.

Here is the error I keep getting:

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

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:

$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? Are there other more reliable solutions that are able to reach the inbox?

1 answer

  • answered 2019-09-12 08:23 Synchro

    Here's your problem:

    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]

    You're asking to connect to smtp.gmail.com, but you're ending up connected to a bluehost.com host. This will be because your hosting provider is redirecting outbound traffic on port 465 to their own mail server using their firewall. 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. 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.

    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. 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.

    This exact situation is covered in the troubleshooting guide that's linked from the error message.