How to use the swiftMailer in Yii2

前端 未结 6 569
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 18:01

I can\'t finally understand how to use the swiftMailer extension in Yii2. Judging by that on this subject I didn\'t find questions, the task is trivial, but up to the end I

6条回答
  •  一生所求
    2020-12-07 18:20

    Make sure you have initialised your application in production environment to send emails from your application,else it will be written in to the mailoutput folder.Or manually edit the config file like follows.

    In the components's section of your common/main-local.php

    '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' => 'smtp.gmail.com',
                'username' => 'username@gmail.com',
                'password' => 'password',
                'port' => '587',
                'encryption' => 'tls',
                ],
        ],
    

    In your Controller

        \Yii::$app->mail->compose('your_view', ['params' => $params])
        ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
        ->setTo('to_email@xx.com')
        ->setSubject('This is a test mail ' )
        ->send();
    

    This should work! Hope this will help you!

提交回复
热议问题