PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

PhpSslOpensslPhpmailer

Php Problem Overview


I am using PHPMailer on PHP 5.6, the increased security around certificated in PHP 5.6 is certainly fun.

I am trying to send a test message to a domain hosted on dreamhost, the error that comes back from PHPMailer is: Could not connect to SMTP host.

That error is not right though, I have logging enabled and here is what is actually going on.

> Connection: opening to mx1.sub4.homie.mail.dreamhost.com:25, > timeout=30, options=array ( ) Connection: opened S: 220 > homiemail-mx32.g.dreamhost.com ESMTP > > C: EHLO s81a.ikbb.com > > S: 250-homiemail-mx32.g.dreamhost.com 250-PIPELINING 250-SIZE 40960000 > 250-ETRN 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 8BITMIME > > C: STARTTLS > > S: 220 2.0.0 Ready to start TLS > > C: QUIT > > S: SMTP ERROR: QUIT command failed: Connection: closed

I could not understand why PHPMailer just gives up, issuing a QUIT command when it should start sending the message. I got another clue from another log:

PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php

If I use some custom options to prevent validation of the cert they are using I can get it to continue. Here is what I have:

		$mail->SMTPOptions = array (
	    'ssl' => array(
	        'verify_peer'  => false,
	        'verify_peer_name'  => false,
	        'allow_self_signed' => true));

If I put the SMTPOptions in there and skip the peer verification, message goes OK - with no warning in PHP at all.

How can I trap that error, so I know there is an issue but still send the message?

Php Solutions


Solution 1 - Php

I had the same problem and I found the answer in the PHPMailer documentation.

PHP 5.6 certificate verification failure

In a change from earlier versions, PHP 5.6 verifies certificates on SSL connections. If the SSL config of the server you are connecting to is not correct, you will get an error like this:

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one. Failing that, you can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.

Sometimes this behaviour is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.

Solution 2 - Php

Solution for WHM/cPanel(s) : Disable SMTP Restriction by following below process:

a) Open WHM and search for SMTP restriction, make sure it's disable.(You can go through Home »Security Center »SMTP Restrictions directly as well)

enter image description here

b) Or Same thing can be done via Tweak Settings (Directly go for Home »Server Configuration »Tweak Settings or you can click on tweak setting link shown in upper image)

enter image description here

Solution 3 - Php

For those of you using cPanel, I tried the [SMTP check code][1] from the examples folder in PHPMailer and I got this same error:

PHP Warning: stream_socket_enable_crypto(): Peer certificate  CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php

I realized that it was not an error related to PHPMailer, so I searched for similar errors related to CentOS and I found this link that shed some light: [Issue sending mails through 3rd party][2]. You have to take a look at "SMTP Restrictions" in cPanel.

[1]: https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp_check.phps "SMTP check" [2]: https://forums.cpanel.net/threads/issue-sending-mails-through-3rd-party.554811/ "Issue sending mails through 3rd party"

Solution 4 - Php

For PHP 5.6 use the following. Adding "tls://" is the key.

$mail->Host = gethostbyname('tls://smtp.gmail.com');

See: http://php.net/manual/en/context.ssl.php

Solution 5 - Php

Disable SMTP Restriction in WHM

Solution 6 - Php

I had a similar problem after I've upgraded to PHP 5.6 on my WordPress machine. The WP Mail SMTP by WPForms (wp-mail-smtp) plugin were configured to use localhost as SMTP Host. I've changed it to the FQHN (Fully Qualified Host Name) as it is defined in the SSL cert. After this change it is working fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob GunsukaView Question on Stackoverflow
Solution 1 - PhpJesús AmieiroView Answer on Stackoverflow
Solution 2 - PhpAnant Kumar SinghView Answer on Stackoverflow
Solution 3 - PhpgeorchView Answer on Stackoverflow
Solution 4 - PhpChrisbotView Answer on Stackoverflow
Solution 5 - PhpKaran ChunaraView Answer on Stackoverflow
Solution 6 - PhpSergey V.View Answer on Stackoverflow