修改配置文件,普通版在(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 "失败";
}

 

相关文章:

  • 2021-05-14
  • 2021-10-22
  • 2021-05-02
  • 2021-09-05
  • 2021-08-21
  • 2021-10-29
  • 2022-03-10
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2022-12-23
  • 2021-12-29
  • 2022-02-07
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案