I am using cakephp 3.0. I am trying to send mail using cakephp through gmail SMTP Server. I am trying to do it from my localhost so I don't have ssl. This is the configuration I've done in app.php:
'EmailTransport' => [
'gmail'=>[
'className'=>'Smtp',
'host'=>'smtp.gmail.com',
'port'=>587,
'timeout' => 60,
'username'=>'myemail#gmail.com',
'password'=>'mypassword',
'tls' => true,
]
]
This is where I've created my email profile :
'Email' => [
'gmail' => [
'transport' => 'gmail',
'from' => 'myemail#gmail.com'
]
],
This is the code from my custom mailer class :
$mail
->to($email)
->profile('gmail')
->subject($this->subject)
->emailFormat('html')
->template('welcome')
->viewVars([
$name=>$name,
$code=>$code
]);
I've already allowed access to less secure apps in my gmail account. This is the error cakephp is throwing :
SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS
Attaching a complete snapshot of the error :
Found the solution. Added these parameters in the transport config to bypass ssl authentication:
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
Worked like a charm.
Related
I am working on yii2. Using my localhost on XAMP I am trying to send an email. While sending the email I am getting the bellow error in POSTMAN.
"name": "Exception",
"message": "Connection could not be established with host smtp.gmail.com [ #0]",
"code": 0,
"type": "Swift_TransportException",
Bellow is my common/config/main-local
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport'=>[
'class'=>'Swift_SmtpTransport',
'host'=>'smtp.gmail.com',
'username'=>'MY GMAIL ID',
'password'=>'Password',
'port'=>'465', //587 also tried this with ssl and tls
'encryption'=>'ssl' //tls also tried with port 465,26 and 587
],
'viewPath' => '#common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
//'useFileTransport' => false,
],
Below is my sending request
Yii::$app->mailer->compose()
->setFrom('MY ID')
->setTo('SEND ID')
->setSubject('New Message')
->setTextBody('HI')
->setHtmlBody('<b>HTML content</b>')
->send();
I have searched each and every question(s) but couldn't able to solve the issue. What should I do now ?
Any help would be highly appreciated.
Make sure you have enabled less secure apps for your account first.
Then you should use the following settings for port:587 and encryption:tls and it will work, I use these setting for my local computer at home to send emails from localhost.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport'=>[
'class'=>'Swift_SmtpTransport',
'host'=>'smtp.gmail.com',
'username'=>'MY GMAIL ID',
'password'=>'Password',
'port' => '587' ,
'encryption' => 'tls' ,
],
'viewPath' => '#common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
//'useFileTransport' => false,
],
The comment of Muhammad is helpful. I don't have enough reputation to add this comment to his post.
After enabling access to less secure apps on gmail, WAIT for some time for the change to take effect, otherwise, you will continue to get errors about 'TLS not available' as it happened to me.
To put everything together,
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxxx#gmail.com',
'password' => 'your password',
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [ 'ssl' => [ 'allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false, ], ]
],
'viewPath' => '#common/mail',
],
* IMPORTANT *
Disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.
Make sure you have enabled less secure apps for your account first
Check Gmail through other email platforms
Edit \common\config\main-local.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'yourname#gmail.com',
'password' => 'yourpass',
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
],
],
I've a PHP application that's running with Yii Framework and it's using the YiiMail extension that's is based in Swiftmailer.
My application was working perfectly yesterday, but today the follow error was launched:
fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
My Yii app config:
'mail' =>
array('class' => 'application.extensions.yii-mail.YiiMail',
'transportType' => 'smtp',
'transportOptions' => array(
'host' => 'smtp.gmail.com',
'username' => '**#gmail.com',
'password' => '***',
'port' => '465',
'encryption'=>'tls' ),
'viewPath' => 'application.views.mail',
'logging' =>false,
'dryRun' => false
)
ANSWER: A FAST SOLUTION
My app is running in Windows, so I did a fast configuration to solve this problem at moment.
I did a configuration with sendmail and it enable into my php.ini file.
Ps: The main problem is if you have many apps running in the same php. How don't this problem to me, it's is stand alone application, I just did.
Something like this:
sendmail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port = 587
#default_domain = gmail.com it's is not necessary
auth_username= your gmail#gmail.com
auth_password=your password
php.ini
[mail function]
sendmail_path = "path to sendmail installation"
SMTP = smtp.gmail.com
smtp_port = 587
If your current config was previously working, then suddenly stopped. Consider looking into the following:
Generating an APP password for a gmail account
Enabling Less SecureApps settings for your gmail account
I have encountered a similar problem before, enabling Less Secure Apps, then when I set 2 Step Verification for my google account, it stopped working.
Then generating an APP password, did the trick!
This is my working smtp settings in my Yii2 project:
common/config/main-local.php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=bd-sys',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => '******#gmail.com',
'password' => '******',
'port' => 587,
'encryption' => 'tls',
],
],
],
];
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.
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.
I'm trying to send email via cakephp3.
This is my app email config:
'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,
],
],
And this is controller :
public function index() {
$this->autoRender = false;
$email = new Email('default');
if (
$email->from(['mymail#gmail.com' => 'My Site'])
->to('mymail#gmail.com')
->subject('About')
->send('My message')) {
print 'ok';
}
}
And now, if i run this function, result is printed 'ok' on monitor.
But no mail is on my testing email box, even span, nothing.
My question is, why cakephp tells me that sending was done, but no mail is present on my box ?
Thank You.
Your configuration is incorrect. cakePHP attempts to send the e-mail via smtp to your localhost.
Most likely you do not have an MTA (ie. exim, dovecot) installed locally and the request gets dropped. This should be visible as an error in you logs (if enabled).
An easy solution is to change the configuration to a working email service for testing, for example Gmail.
Example:
Email::configTransport('gmail', [
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => 'my#gmail.com',
'password' => 'secret',
'className' => 'Smtp',
'tls' => true
]);
Note that the host and port point to Gmail. This example is indicative and was taken from the official documentation.
More information on configuring email transports in cakePHP 3.0 here: http://book.cakephp.org/3.0/en/core-libraries/email.html#configuring-transports