Yii2使用swiftmailer发送邮件的方法

Yii2使用swiftmailer发送邮件的方法。具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'mail' => [
    'class' => 'yiiswiftmailerMailer',
    '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',
     ],
  ],
],

控制器

1
2
3
4
5
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();
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论