Gmail SMTP in CakePHP not working? Here is a possible fix!

While working in a CakePHP application in my local computer I usually use Gmail SMTP in CakePHP to send emails from my application which are of course test emails. Previously it worked just fine with the basic settings (as specified in first code snippet below) but down the road some times later (I am not sure when as I had returned to debug this application after a while) it had stopped to work. When I tried a first email it showed:

error- stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed stream_socket_client(): Failed to enable crypto stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error)

Gmail SMTP in CakePHP

Obviously Gmail SMTP in CakePHP had stopped working while using email transport to gmail! My email transport settings looked like this:

'EmailTransport' => [
//Gmail SMTP in CakePHP
'gmail' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'xxx@xxxxx.com',
'password' => 'xxxxx',
]
]

Ok. Here is the explaination for this in this thread :

PHP 5.6 verifies SSL certificates by default, and if your cert doesn’t match, it will fail with this error. The correct solution is to fix your SSL config. So possibly my PHP version update had made it to stop working.

Solution to fix Gmail SMTP in CakePHP problem:

Thanks to the efforts of jagguy at this forum

All you have to do is, to add ssl configuration to the transport settings (in bold), as follows:

'EmailTransport' => [
..........
'gmail' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'xxx@xxxxx.com',
'password' => 'xxxxx',
<span style="font-weight: bold;">'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]</span>
]
]

Leave a Reply

Your email address will not be published. Required fields are marked *