This is my mailer component.
As you can see, because of my test purposes, I'm using email on file
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,
],
This is my log component.
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
// for this target, see http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'categories' => ['yii\db\*', 'email_test'],
'message' => [
'from' => ESHOP_EMAIL,
'to' => DEVELOPER_EMAIL,
'subject' => 'Errore db [' . YII_ENV . ']',
],
],
],
],
In siteControlle->actionIndex() I'm testing log and email component in this way
public function actionIndex()
{
Yii::error("testing mail log", "email_test");
Yii::$app->mailer->compose()
->setFrom([ESHOP_EMAIL => ESHOP])
->setTo( DEVELOPER_EMAIL)
->setSubject("actionIndex executed on time " . date ("H:i:s"))
->setTextBody("Useless body")
->send();
return $this->render('index');
}
As i expect, at every reload of the index page I got 2 .eml files created inside frontend/runtime/mail folder.
So SwiftMailer is working, and even the log system.
Now the problem
I try to remove use of file from mailer component, commenting the row
'useFileTransport' => true,
When I reload the index page, I got the SECOND MAIL, the one manually composed and sent, but I DO NOT RECEIVE the first mail, the one that should be automatically composed and sent by log system using SwiftMailer.
What's wrong?
Sorry to everyone.
The problem was a mistake in the destination address. Using the logger the error caused email cannot be sent, but using the manual sending the destination address error is simply ignored and the email is sent.
I think the default value for 'useFileTransport' is true. Is better set to false and don't comment it
If you don't use a php function for sending eMail you need a transport like this
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//'useFileTransport' => true,
'useFileTransport' => false, //set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'your_mail#gmail.com',
'password' => 'your_password',
'port' => '587',
'encryption' => 'tls',
],
And if you don't use gmail you need the configure the parameter in a proper way.
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 use Windows and Yii 2.0.13.1 and xampp with php7.1.4.
When logging in, I click on reset it link and i'm entering my email and send, I encounter this error:
Swift_TransportException
Process could not be started [The system cannot find the path specified.
]
My common/config/main-local.php is:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'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,
],
Other settings and files are in the default mode.
what is the problem? Please guide me
Seems that in your config is missing the transport config
eg: using gmail.com as transport (you should choose the trasport available for your host)
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'yourmail#gmail.com',
'password' => 'your_password',
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'verify_peer' => false,
'allow_self_signed' => true
],
]
],
],
Everything is right now
I tried to do some changes in common/config/main-local.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mailtrap.io',
'username' => 'example#examplemail.com',
'password' => '***************',
'port' => '2525',
'encryption' => 'tls',
],
],
Maybe some of your transport settings are incorrect, so it cannot send an email. For local server you can just set useFileTransport to true like:
'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' => true,
],
Then check your <app>/runtime/mail/folder, where *.eml mail will be generated. Therefore you can click on your password-recovery link and proceed futher.
P.S. Yii2 advanced template includes email password-recovery feature from the box (if you are implementing it from scratch)
Following are my Configurations:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_MailTransport',
],
'useFileTransport' => False,
],
I have even tried with followings:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => False,
],
I'm using following code to send email:
var_dump(\Yii::$app->mailer->compose()
->setTo($email)
->setFrom(\Yii::$app->params['supportEmail'])
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody('<b>HTML content</b>')
->send());
But its returning false everytime no exception is thrown.
I have used mail() function to send email for same content, its working fine and email is instant. But issue is only with the SwiftMailer.
I couldn't find way to debug it. I have Yii2Debugger enabled, but I can't fine any info in that as well.
Edit:
I Tried with Gmail SMTP, I'm getting
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
I tried their default SMTP, I'm not getting any error with that, but still getting false for it.
We are on Sharing hosting env. DO I need to check any PHP Settings.
try this configuration
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport'=>false,
// 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.
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'yourmail#gmail.com',
'password' => 'yourpassword',
'port' => '465',
'encryption' => 'ssl',
],
],
hope it helps :)
I have a error with yii2, I can't send emails via yii with a email account. If my password is correct :(
This is my code:
web.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'user#hya.com.mx',
'password' => 'passwd',
'port' => '587',
'encryption' => 'tls',
],
],
'log'
Controller.php
Yii::$app -> mailer -> compose()
-> setFrom('users#hya.com.mx')
-> setTo('jhon#hya.com.mx')
-> setSubject('Test')
-> setTextBody('Plain text content')
-> setHtmlBody('It is a test')
-> send();
It looks like you are using Google SMTP server. Google has a new security check that only allows you to send emails from google apps. If you are using any other you will run into such errors. To fix this you can do as follows:
Use default sendmail function by having
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
],
I find the first solution more efficient
Change google setting to allow less secure apps
Follow this link to change you gmail settion https://myaccount.google.com/security
I use the following configuration and work right
is pratically equals to yours but with a difference the username is google mail user and not an noyt google app user
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'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' => true,
'useFileTransport' => false,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'myname#gmail.com',
'password' => 'mypassword',
'port' => '587',
'encryption' => 'tls',
],
],
hope is useful