Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp 3 error trying to send email with gmail

I'm trying to send a verification email using Gmail but i get this 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)

I have followed the Configuring Transports guide.

Email::configTransport('gmail', [
  'host' => 'ssl://smtp.gmail.com',
  //'host' => 'smtp.gmail.com',
  'port' => 465,
  'username' => '[email protected]',
  'password' => 'password',
  'className' => 'Smtp',
  'log'=>true,
  //'tls' => true
]); 

$correo = new Email();
$correo
  ->transport('gmail')
  ->template('registro_exito')
  ->emailFormat('html')
  ->to('[email protected]')
  ->from('[email protected]')
  ->viewVars([
    'nombre_sitio_secundario'=>$nombre_sitio_secundario,
    'usuario_id'=>$usuario_id,
    'token'=>$token
  ])
  ->send(); 

And this is the complete error log:

2015-09-24 20:09:39 Error: [Cake\Network\Exception\SocketException] 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)
Request URL: /faindit/usuarios/registro
Stack Trace:
#0 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Network\Email\SmtpTransport.php(204): Cake\Network\Socket->connect()
#1 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Network\Email\SmtpTransport.php(159): Cake\Network\Email\SmtpTransport->_connect()
#2 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Network\Email\Email.php(1301): Cake\Network\Email\SmtpTransport->send(Object(Cake\Network\Email\Email))
#3 C:\xampp\htdocs\faindit\src\Controller\Component\CorreoComponent.php(65): Cake\Network\Email\Email->send()
#4 C:\xampp\htdocs\faindit\src\Controller\UsuariosController.php(14): App\Controller\Component\CorreoComponent->registroExito(1, 'something@gm...')
#5 [internal function]: App\Controller\UsuariosController->registro()
#6 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Controller\Controller.php(411): call_user_func_array(Array, Array)
#7 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(114): Cake\Controller\Controller->invokeAction()
#8 C:\xampp\htdocs\faindit\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\UsuariosController))
#9 C:\xampp\htdocs\faindit\webroot\index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#10 {main}

Worth to mention that openssl es enabled on php and also i have enabled the "access for less secure apps" on Gmail configs.

Thanks for helping!.

like image 203
hug0 Avatar asked Sep 23 '15 20:09

hug0


People also ask

How can I send email in cakephp 3?

use Cake\Mailer\Email; After you've loaded Email , you can send an email with the following: $email = new Email('default'); $email->from(['[email protected]' => 'My Site']) ->to('[email protected]') ->subject('About') ->send('My message');

Why SMTP Gmail is not working?

In Google Mail, you must allow "less secure" apps access in order for your SMTP settings to work. There are two places this setting must be enabled: The first is here: https://myaccount.google.com/ under “Connected apps & sites.” Once enabled in both places, you're good to go!

Can I use Gmail to send SMTP email?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.


1 Answers

Ok i found the "error". Actually the code fine, the issue is with OpenSSL in php 5.6 that verifies every encrypted connection by default BUT my local Lampp doesn't count with a ssl certificate and that couses the error.

The solution is to avoid the verification, so the configTransport code would be like this:

Email::configTransport('gmail', [
  'host' => 'ssl://smtp.gmail.com',
  'port' => 465,
  'username' => '[email protected]',
  'password' => 'password',
  'className' => 'Smtp',
  'log' => true,
  'context' => [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    ]
  ]
]); 

I took as reference a PHPMailer answer but it was a little challenging knowing how to apply it to Cakephp3.

Hope this information is going to be helpful for somebody else.

like image 115
hug0 Avatar answered Oct 09 '22 13:10

hug0