SwiftMailer e-mail library

Many web applications need to send notifications and confirm client actions by e-mail for security reasons. The Yii2 framework provides a wrapper, yiisoft/yii2-swiftmailer, for the established library SwiftMailer.

Getting ready

Create a new application by using composer, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.

Both basic and advanced applications contain this extension out of the box.

How to do it…

Now we will try to send any kind of e-mails from our own application.

Sending plain text e-mails

  1. Set the mailer configuration into the config/console.php file:
    'components' => [ // ... 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => true, ], ...

Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.