Yii2发送邮箱总结

修改配置文件,普通版在(config/web.php)。高级版默认配置在/common/config/main-local.php

'components' => [

    'mailer' => [
  'class' => 'yii\swiftmailer\Mailer',
   'useFileTransport' => false, //这里一定要改成false,不然邮件不会发送
   'transport' => [
  'class' => 'Swift_SmtpTransport',
  'host' => 'smtp.163.com', //每种邮箱的host配置不一样
  'username' => '18903181076@163.com', //发件人邮箱
  'password' => 'xgslagfpomsxuseq', //授权码
  'port' => '25',
   'encryption' => 'tls',
    ],
   'messageConfig'=>[
  'charset'=>'UTF-8',
  'from'=>['18903181076@163.com'=>'nickname'] //发件人昵称
  ],
  ],
  ], 控制器发送邮件(自定义消息) $mail = Yii::$app->mailer->compose();
$mail->setTo('****@qq.com'); //接收人邮箱
$mail->setSubject("test"); //邮件标题
$mail->setHtmlBody("发送内容发送内容发送内容"); //发送内容(可写HTML代码)
if ($mail->send()){
echo "成功";
}else{
echo "失败";
} 如果发送页面 $mail = Yii::$app->mailer->compose("email"); //在mail文件夹下创建email页面文件,在文件内编辑内容
$mail->setTo('****@qq.com'); //接收人邮箱
$mail->setSubject("test"); //邮件标题
if ($mail->send()){
echo "成功";
}else{
echo "失败";
}
上一篇:Vue异步更新Dom和$nextTick


下一篇:hdu2669与hdu1576(扩展欧几里德)