Yii2-usuario: Debugging on transport layer - yii2

I cannot send emails out in Yii2-usuario, e.g. when a password recovery is requested. Email is not working at all.
There is just a flash message saying
Unable to send recovery message to the user
I think that this is a transport problem. There are tons of things that can go wrong here: firewall issues, tls-/security-settings, self-signed certificates, wrong ports, hostname-typos, ...
How to debug transport layer? How to enable debugging to trace connection to the email service?

Found the error. In config/web.php the mailer was configured as:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'viewPath' => '#app/mail',
'htmlLayout' => 'layouts/main-html',
'textLayout' => 'layouts/main-text',
instead of
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'viewPath' => '#app/mail',
'htmlLayout' => '#app/mail/layouts/main-html',
'textLayout' => '#app/mail/layouts/main-text',
The layout paths were wrong. So the Mailer could not render the messages and failed sending out emails since Yii2-usuario uses formatted emails.
Everything works now.

Related

YII2 - In test, send email nowhere

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

yii2 swift mail is going to spam

my config is
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'cpanelhost',
'username' => 'cpanel mail username',
'password' => 'cpanelpassword',
'port' => '465',
'encryption' => 'tls',
]
and my model,
$message = Yii::$app->mailer->compose('#app/mail/new', ['status' => $model->status,'note' =>
$model->description,'process' => $model->job_process_id])
->setFrom(['example#gmail.com' => 'title'])
->setTo($Jobmodel->email)
->setSubject('Status Change');
$message->getSwiftMessage()->getHeaders()->addTextHeader('name', 'value');
$message->send();
this mail is going to spam.iam using php 7.2. i change config mail => to mailer but still going to spam .if i delete that config not prevent mail,actually that config is not require for send mail,mail is going without use the config but its going to spam.
your email go into spam is not because of Swiftmailer, is because of your email hosting provider.
You may consider using 3rd party email service like Mailgun, SendGrid, Mandrill, SparkPost etc...
Activate their trial and use the SMTP settings like user and password (e.g. API key) provided by them, follow their documentation.
However, it will not guarantee your email not going to SPAM as well, it depends on many factors, e.g. your email content, also the receiver's email service provider's SPAM rules.

Yii2 SwiftMailer .eml files created but with wrong names & file_put_contents Permission denied

The problem is when I'm trying to "send" the email it's failing with the message
file_put_contents(/some/path//frontend/runtime/debug/mail/20180413-165142-0679-0633.eml):
failed to open stream: Permission denied
The file is being created but with a slightly different name, for example 20180413-165142-0666-8857.eml (I can see it by time). I have no idea what is going on. Any suggestions?
Config file is simple:
common/main-local.php
'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,
],
And sending by this:
Yii::$app->mailer->compose('order', ['order' => $this])
->setTo(Yii::$app->params['adminEmail'])
->setFrom(Yii::$app->params['adminEmail'])
->setSubject('New order #' . $this->id)
->send();
And 'adminEmail' => 'admin#example.com',
Thanks for your attention.

Yii2 mail catcher [duplicate]

This question already has an answer here:
Override Yii2 Swiftmailer Recipient
(1 answer)
Closed 5 years ago.
During the development and testing, I want the Yii2 mailer (swiftmailer) not to send emails to actual addresses, but replace it with developers` emails. Is there any setting in config to do it?
Just set the useFileTransport to true in component configuration for the development environment and all emails will not be sent but will be saved as files so you can easily test everything.
You should set configs for prod and dev environment.
Path for dev config (if you have advanced application template): yourProject/environments/dev/common/config/main-local.php and for prod: yourProject/environments/prod/common/config/main-local.php.
You may use EmailTarget class for logs.
Example of config for dev environment to reach the clue:
return [
'bootstrap' => ['log'],
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
],
'log' => [
'targets' =>
[
[
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'except' => ['yii\web\HttpException:404'],
'message' => [
'to' => ['example#mail.ru'],
'from' => ['yourproject#mail.ru'],
'subject' => ' Errors ',
]
],
],
],
],
];
And similarly for prod environment but with different emails.
If you don't want to use EmailTarget class you may just set your emails for dev in params config here: yourProject/environments/dev/common/config/params-local.php.
And path for prod: yourProject/environments/prod/common/config/params-local.php
Params config example:
return [
'sendToEmails' => ['email1#mail.ru', 'email2#mail.ru']
];
And then you may use the variable in your code this way: Yii::$app->params['sendToEmails'] to get the array of emails to send messages to.
Don't forget to do php init command in your project after compliting your configs.
You may see the detailed docs about environments here.

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