Cakephp 3 error trying to send email with gmail - php

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' => 'user#gmail.com',
'password' => 'password',
'className' => 'Smtp',
'log'=>true,
//'tls' => true
]);
$correo = new Email();
$correo
->transport('gmail')
->template('registro_exito')
->emailFormat('html')
->to('email#gmail.com')
->from('another_email#gmail.com')
->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!.

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' => 'user#gmail.com',
'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.

Send a verification email using gmail in CakePhp 3
Follow these steps to send verification code using gmail
1. open your your project/config/app.php
2. In your app.php, replace(hide) this code
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
write this code in your app.php
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'gmail'=> [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'abc#gmail.com', //your gmail address
'password' => 'abcd123', //your gmail password
'className' => 'Smtp',
'log' => true,
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
],
],
3. Now open your Controller file and add this code
use Cake\Mailer\Email;
use Cake\Network\Exception\SocketException;
4. Write this code on function of your controller which is in used
$msg="Your Password is generate";
$email = new Email('default');
$email
->transport('gmail')
->from(['abcx.com' => 'abcx.com'])
->to($to)
->subject($subject)
->emailFormat('html')
->viewVars(array('msg' => $msg))
->send($msg);
now you can send email from your controller by using this code
5. if this error generate(SMTP server did not accept the password.) then do this process
i.> If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page.
ii.> Allow access to your Google account
As a security precaution, Google may require you to complete this additional step when signing into a new device or application.
To allow access, click the Continue button below.
iii.> Account access enabled
Please try signing in to your Google account again from your new device or application.
6. run your application

Related

Getting error while trying to send email in localhost php

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
app.php
'EmailTransport' => [
'default' => [
//**'className' => MailTransport::class,
/*
* The following keys are used in SMTP transports:
*/
'host' => 'ssl://smtp.gmail.com',
'port' => 567,
//'timeout' => 30,
'username' => 'abc#gmail.com',
'password' => 'abc',
'className' => 'Smtp',
// 'client' => null,
'tls' => true,
//'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'abc#gmail.com',
],
],
Controller class
public function mail()
{
$session = $this->request->session();
$id = $session->read('req_id');
$email = new Email();
$email->transport('default');
$email->from(['NO-REPLY.formcr1#abc.com.au' => 'abc REPLY']);
$email->sender(['NO-REPLY.formcr1#abc.com.au' => 'abc NO-REPLY']);
$email->to('abc#gmail.com'); /** This must be changed to abc's confirmed email */
$email->subject('abc Request Number : '.$id);
//THIS PATH NEEDS TO BE CHANGED DURING DEPLOYMENT
$path = 'C:/xampp/htdocs/request_form/webroot/pdfresults/';
$email->attachments($path. 'abc Cost Estimate Request Information_'.$id.'_'.'v.3online'.'.pdf');
$email->send('Please look for the attachment to see the form. Cheers!');
}
enter image description here
email credential are correct. and tried turning off the firewalls as well but still not working
The error means exactly what it says - the service you’re trying to connect to doesn’t exist or is not responding.
This is explained two ways. Either the service you’re connecting to is indeed down, or you’re trying to connect to the wrong server or port.
In this case, it’s the latter. You’re trying to connect to an implicit TLS SMTP service on a port not associated with that service.
Change this:
'host' => 'ssl://smtp.gmail.com',
'port' => 567,
To
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
Or
'host' => 'tls://smtp.gmail.com',
'port' => 587,

Cannot send mail with yii2 swiftmailer

I get error when trying send mail with Yii::$app->mailer->compose() function. This error appears when trying to connect smtp server, so I provide error message and mailer YII2 configuration
Expected response code 250 but got code "535", with message "535-5.7.8
Username and Password not accepted.
Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials 59sm3639427wrc.23 -> gsmtp"
Here is params from config/common.php file:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'port' => '587',
'encryption' => 'tls',
'username' => 'myemailbox#yahoo.com',
'password' => 'myemailpass',
'host' => 'smtp.mail.yahoo.com',
],
],
I already enabled "Allow apps that use less secure sign in" function in Yahoo account settings. Trying "app password" option but got the same result.
Before using yahoo smtp I tried it the same way with google smtp. Error message still refers to the https://support.google.com page. Is it possible Apache cached login and pass to smtp server?
Of course I checked google support page and followed instructions included https://accounts.google.com/DisplayUnlockCaptcha page.
try changing port
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'port' => '465',
'encryption' => 'tls',
'username' => 'myemailbox#yahoo.com',
'password' => 'myemailpass',
'host' => 'smtp.mail.yahoo.com',
],
],
I resolved this case, but didn`t detect what exactly was the cause. Maybe there was some sort of cache of config error.
So I put this direct setting right before compose function:
\Yii::$app->mailer->setTransport([
'class' => 'Swift_SmtpTransport',
'port' => '587',
'encryption' => 'tls',
'username' => 'myemailbox#yahoo.com',
'password' => 'myemailpass',
'host' => 'smtp.mail.yahoo.com',
]);
And it started to work.

cant send an email on cakephp3

I can't get cakephp3 to send emails. In cakephp2 I could do this no problem. I am using the latest WAMP, and cakephp3.3 on Windows 7. I tried to follow the directions but it looks like I am getting something basic wrong. Do I also need to configure Wamp as I checked the php.ini-development file but there is no smtp entry to change
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)
controller
public function singleTutorEmail(){
$email = new Email();
$email->transport('gmail3');
$to='jjxxx#gmail.com';
$subject='testing';
$message='hello, dfsfsdfsdf sdfsdf';
$email->from(['jjxxx#gmail.com' => 'test'])
->to($to)
->subject( $subject)
->send($message);
}
in app.php
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 465,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'gmail3' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'jjxxx#gmail.com',
'password' => 'xxx',
'client' => null,
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
],
],
http://book.cakephp.org/3.0/en/core-libraries/email.html
Sending Mail using CakePHP 3.0
I had the same issue when making an HTTPS request using Cake\Network\Http\Client
To resolve the problem, I had to download "cacert.pem" file from https://curl.se/ca/cacert.pem
And then I updated php.ini by adding the following line:
openssl.cafile = <PATH_TO_CACERT_PEM>/cacert.pem
Don't forget to restart the web-server.
If the above thing doesn't make it work, try to update the OpenSSL library installed on your system.
I really hope this works for you too.

yii2 Failed to authenticate on SMTP server with username using 2 possible authenticators

I am using swift mailer in yii2, i am using correct smtp setting still not able to send email.
I got this error - Failed to authenticate on SMTP server with username "example#example.com" using 2 possible authenticators
my swift mailer settings are:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'myHostName',
'username' => 'myUsername',
'password' => 'myPassword',
'port' => '465',
'encryption' => 'ssl',
],
],
I searched a lot on internet but found no solution, some people suggested that it would work by making 'useFileTransport' => false, but it is still not working.

Lravel Mail not sending -- Connection could not be established with host smtp.gmail.com [Connection timed out #110]

I just developed a laravel web application with mail system.
i got error like
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
controller
Mail::send('timesheet/emailtemplate',array('data'=>$query),function($message)
{
$message->to('example#gmail.com')->cc('expalecc#gmail.com')->subject('Work Report on - ');
});
email template file : emailtemplate.blade.php
<h2>hai</h2>
mail.php (config)
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => null, 'name' => null),
'encryption' => 'ssl',
'username' => 'myemail#example.com',
'password' => 'mypassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
instead of using smtp use Mandrill Driver it is simpler and quicker than use smtp server. by default laravel comes with Mandrill Driver. mandrill required Guzzle 4 HTTP library.. for geting that into your project add
"guzzlehttp/guzzle": "~4.0"
to your composer.json file (in your project root directory)
inside app/config/mail.php
change this line
'driver' => 'mandrill',
go to https://mandrillapp.com/settings
and sign up and generate api key
create an app/config/services.php configuration file if one does not already exist for your project and add below configurations and generated api key
return array(
'mailgun' => array(
'domain' => '',
'secret' => '',
),
'mandrill' => array(
'secret' => 'enter your mandrill api key here',
),
'stripe' => array(
'model' => 'User',
'secret' => '',
),
);
check this out this will be help full
Youtube video for setting up Mandrill for laravel

Categories