1.在config中的web.php中加入如下一段话
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
//'viewPath' => 'views\mail',
'useFileTransport' => false,
//'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com',
'username' => '13020025757@163.com',
'password' => '*******',
'port' => '25',
'encryption' => '',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['13020025757@163.com'=>'admin']
],
],
]
2.控制器里调用
$bool= Yii::$app->mailer->compose() ->setFrom('13020025757@163.com')//这里必须和web.php里的from相同 ->setTo('1234567989@qq.com') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('点我 http://www.setpember.com/yuekao/web/index.php?r=discount/get&d_id='.$arr['dis_id'].'&d_user='.$arr['email']." 领券") ->send(); if($bool){ return $this->success(['discount/index'], '发送成功'); }