Tuesday, April 24, 2018

Yii2 swiftmailer

Yii2 send email using swiftmailer

Default config in Yii2

'components' => [
                'mailer' => [
                    'class' => 'yii\swiftmailer\Mailer', 
                    'transport' => [
                        'class' => 'Swift_SmtpTransport',
                        'host' => 'localhost',
                        'username' => 'username',
                        'password' => 'password',
                        'port' => '587',
                        'encryption' => 'tls',
                    ],
                ],
            ]

Send email usage:
After mailer component settings, you can use below example to send an email

$mail = Yii::$app->mailer->compose()
    ->setFrom('from@xyz.com')
    ->setTo('to@xyz.com')
    ->setSubject('Subject')
    ->setTextBody('Text')
    ->setHtmlBody('Html data');

     $mail->send();

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete

  2. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Hire Yii Developers in India

    ReplyDelete

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search