To send emails from Yii2 is pretty straightforward, since it now uses Swiftmailer for that purpose.
In your config file just add (it has been already added if you created your project using composer):
'components' => [ ... 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'localhost', // e.g. smtp.mandrillapp.com or smtp.gmail.com 'username' => 'username', 'password' => 'password', 'port' => '587', // Port 25 is a very common port too 'encryption' => 'tls', // It is often used, check your provider or mail server specs ], ], ... ],
If you want to use a Swiftmailer plugin (very useful in many cases, btw), you can do it in your conf file too. Something ike this will work:
'transport' => [ 'class' => 'Swift_SmtpTransport', ... 'plugins' => [ [ 'class' => 'Swift_Plugins_ThrottlerPlugin', 'constructArgs' => , ], ], ],
Send emails ¶
Use this code for sending emails from your application:
Yii::$app->mailer->compose() ->setFrom('firstname.lastname@example.org') ->setTo('email@example.com') ->setSubject('Email sent from Yii2-Swiftmailer') ->send();
Advanced email templates ¶
In some cases you might want to use templates for email rendering, so, in that case you just need to do something like:
Yii::$app->mailer->compose('@app/mail-templates/email01', [/*Some params for the view */]) ->setFrom('firstname.lastname@example.org') ->setTo('email@example.com') ->setSubject('Advanced email from Yii2-SwiftMailer') ->send();
Or, if you want to use one template for HTML rendering and another for text, do something like this:
Yii::$app->mailer->compose(['html' => '@app/mail-templates/html-email-01', 'text' => '@app/mail-templates/text-email-01'], [/*Some params for the view */]) ->setFrom('firstname.lastname@example.org') ->setTo('email@example.com') ->setSubject('Advanced email from Yii2-SwiftMailer') ->send();
Notice that the path to the view can also be relative, like inside a controller (e.g.
['html' => 'html-email-01']).
Difficulty Including Plugin (OpenBuildings swiftmailer-css-inliner)
I tried including the plugin swiftmailer-css-inliner as suggested here. Unfortunately the plugin is ignored (my css style is still not included in the mails (which are sent).
The tutorial suggest, css can be included into the mail template in style tags.
This does not seem to be true at all (as e.g. Ink templates are not formatted).
Is there anybody having similar issues?
Turn off SSL peer verification for PHP 5.6+
Since PHP 5.6+ default setting is to verify SSL peer host & name to TRUE. Unfortunatelly, this causes error when using some web services (e.g. SendGrid) with message "SSL3_GET_SERVER_CERTIFICATE:certificate verify failed". All fixes turn off forcibly peer verifycation, (including official SendGrid library with curl peer verify = false). Set "streamOptions" to workaround the issue:
There is a mistake is should be:
'components' => [
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.