YII2 - In test, send email nowhere - yii2

I have a test and live environment. In test, I don't ever want to send the mailer mails. I have the mailer setup as a component in web.php like this
'mailer' => [
'class' => 'nickcv\mandrill\Mailer',
'apikey' => 'xxxxxxxxxxxxxxxxxxxx',
'useMandrillTemplates' => true,
'templateLanguage' => nickcv\mandrill\Mailer::LANGUAGE_HANDLEBARS,
],
Is there a way to set up the mailer component so it sends nowhere?
Thanks in advance.

Because mailer you are using extends yii\mail\BaseMailer you can simply set its $useFileTransport to true and the mail will be saved in file instead.
'mailer' => [
'class' => 'nickcv\mandrill\Mailer',
'apikey' => 'xxxxxxxxxxxxxxxxxxxx',
'useMandrillTemplates' => true,
'templateLanguage' => nickcv\mandrill\Mailer::LANGUAGE_HANDLEBARS,
'useFileTransport' => true,
],
Another option is creating a mock of mailer and using it instead of real mailer.
If you use codeception framework and its Yii2 module, the mailer component should be replaced automatically if you have email part enabled in codeception configs.

======== Configure Apache to send the emails from localhost code ========
Changes for C:\xampp\php\php.ini file:
search for [mail function]:
[mail function]
SMTP=localhost to SMTP=smtp.gmail.com
smtp_port=587
sendmail_from=me#example.com to sendmail_from=your email address
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
mail.log = syslog
Changes for C:\xampp\sendmail\sendmail.ini:
search for [sendmail]:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=same as "sendmail_from" in php.ini file
auth_password=generated password for the email account
force_sender= same as "auth_username" in this file
=========== mailer component in Yii application ===============
'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' => 'your email address (for testing on localhost use same as in ini files)',
'password' => 'generated password',
'port' => 587,
'encryption' => 'tls',
],
], //end of mailer

Actually, I think it would be nice to receive somewhere your test emails for debugging and QA.
Personally, in dev environment, I would recommend setting up and configuring mailhog
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => '1025',
],
],
It is straightforward to install following the steps described here: https://github.com/mailhog/MailHog
You can then go to http://localhost:8025 and see your inbox

Related

yii2 : Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to smtp.gmail.com:587

i try to send an email in yii2 project in server but showing error Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)
my config is this
'mailer' => [ //Your default mailer
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'email#gmail.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
and my code is
Yii::$app->mailer->compose()
->setFrom('info#gmail.com')
->setTo('text#gmail.com')
->setSubject('Subject')
->setTextBody('Plain text content')
->setHtmlBody("Hello")
->send();
I guess you have a problem with your SMTP server.
You may try to use another one.
For debugging propose, I suggest trying to send an email with Mail Tramp.
They have a simple way to get the right configuration.
It won't sent any email instead just. It will just show you which email has been send.

how to get mail from contact page yii2 advanced

I am using yii2 advance . I want the contact form to send email to admin. but Its not working what should do? I tried in following way=>
'adminEmail' => 'admin#example.com', //wrote admin email id here
also changes
'useFileTransport' => false,
what is exact steps to make it active can any one solve this?
In your config.php you should also configure a proper transport eg for a transport based of gmail account you could use
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
.....
'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
//
//
/* configurazione servizio email da usare in locale (localhost sviluppo) */
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'your_gmail_username#gmail.com',
'password' => 'your_gmail_password',
'port' => '587',
'encryption' => 'tls',
],

Is my yii2 smtp configuration correct?

I have the following smtp configuration, but I'm not sure since sometimes I face ssl timed out while sending an email.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,
],
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#backend/mail',
'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' => 'in-v3.mailjet.com',
'username' => 'a3027c3xxx',
'password' => 'c838779xxx',
'port' => '465',
'encryption' => 'ssl',
],
],
Then I use like this
$message = Yii::$app->mail->compose();
$message->setTo(Yii::$app->params['adminEmail'])
->setFrom(Yii::$app->params['adminEmail'])
->setTo("mymail#gmail.com")
->setSubject('Password Reset')
->setHtmlBody($this->renderAjax('//email/_konten',['hello'=>"To black" ,'konten'=>'this is konten','link'=>Yii::$app->params['baseurl'].'lupapass/chpass?&q=empty','textbutton'=>'Click this link']))
->send();
and the result is sometime I face timed out.
But if I send directly from swiftmailler class like below code it sucessfully send 100 email without any ssl timed out
$transport = \Swift_SmtpTransport::newInstance('in-v3.mailjet.com', 465)
->setUsername('myusername')
->setPassword('s3cr3t')
->setEncryption('ssl');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance('Password Reset')
->setFrom(array('no-reply#myweb.com' => 'My Web'))
->setTo(array('some#one.com'))
->setBody($this->renderAjax('//email/_konten',['hello'=>"To black" ,'konten'=>'this is konten','link'=>Yii::$app->params['baseurl'].'lupapass/chpass?&q=empty','textbutton'=>'Click this link']))
->setContentType("text/html")
;
$result = $mailer->send($message);
If you ask how my ssl timed out here is my question link
How to solve Swift_TransportException Connection to ssl Timed Out error?
So I start thinking is this ssl timed out because my configuration? or different sending method? from first example and second example?
in common\config\main-local.php there is a mailer => [] and mail => [] is this neccessary?
an can you please explain what is useFileTransport really?
Thanks in advance.
PS. I have already use any thirdparty and port configuration but still facing ssl timed out issue.
According to Gmail SMTP config when you are using encryption ssl or tls use port 587 instead of 465

Send message over SMTP using Swiftmailer and Yii2

I need some advice over the following case:
I have configured the config/web.php file as follows
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'myusername',
'password' => 'mypassword',
'port' => '587',
'encryption' => 'tls',
],
],
Also in the Controller:
$message = Swift_Message::newInstance()
->setSubject('...')
->setFrom(['mymail'])
->setTo(['recipient'])
->setBody('......');
$transport = Swift_SmtpTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
I get the Error: 'Connection could not be established with host ' (Swift_TransportException)
Any ideas? Thank you.
Since you have got this configured as Yii component use it properly.
See documentation example:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from#domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
Also: see this SO question.
In your config set it as
'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.
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'yourusername',
'password' => 'yourpassword',
'port' => '587',
'encryption' => 'tls',
]
],
In your controller you can compose the email.
Yii::$app->mailer->compose('#app/mail/layouts/reset',['model' =>$model])
->setTo('to#example.com')
->setFrom('from#example.com')
->setSubject('Subject Here')
->setTextBody('You can either render the layout or declare your body here')
->send();
In the above code I am using layouts. You can use layouts and pass variables to the layout and use them. If you dont want to use layouts that works as well just call the compose method
Yii::$app->mailer->compose()
I did the following and worked:
$transport = Swift_SmtpTransport::newInstance(Yii::$app->components['mailer']['transport']['host'], Yii::$app->components['mailer']['transport']['port']);
$transport->setUsername(Yii::$app->components['mailer']['transport']['username']);
$transport->setPassword(Yii::$app->components['mailer']['transport']['password']);

SMTP server problems swift mailer

i am using yii2 frame work and i am implementing swift mailer in my web.php file.it's working fine.
my config code:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport'=>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
`` 'host' => 'smtp.gmail.com',
'username' => 'testdemomail32#gmail.com',
'password' => 'TECHEDGE',
'port' => '465',
'encryption' => 'ssl',
],
],
but i got problem in when i am implementing same thing in server side i got response in below:
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/answer/14257 k8sm2888892qke.45 - gsmtp
any help thanks..
configure swift mailer like below:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'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' => 'testdemomail32#gmail.com',
'password' => 'TECHEDGE',
'port' => '587',
'encryption' => 'tls',
],
// 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.
],
go to https://www.google.com/settings/security/lesssecureapps and turn on Access for less secure apps