yii2学习笔记——调用邮件模块

来源:互联网 发布:openwrt 修改mac wifi 编辑:程序博客网 时间:2024/05/22 12:49

一.修改配置文件,开启mail模块

打开app\config\web.php,向其中config中的components里面增加

'mailer' => [            'class' => 'yii\swiftmailer\Mailer',            'transport' => [                 'class' => 'Swift_SmtpTransport',                 'host' => 'smtp.qq.com',  //每种邮箱的host配置不一样               'username' => 'no-reply@qq.com',                 'password' => '####',                 'port' => '23',                 'encryption' => 'ssl',                             ],               'messageConfig'=>[                 'charset'=>'UTF-8',                 'from'=>['no-reply@qq.com'=>'Wangyang']             ],              // 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.            'useFileTransport' => false,        ],

需要注意的是:
1.smtp协议对于不同的邮箱是不一样的,tencent是smtp.qq.com,163是smtp.163.com
2.password填的不是邮箱密码,应该是你开启邮箱smtp服务的时候,生成的序列码,如果是企业邮箱的话,邮箱密码就可以
3.port按开启smtp服务时官方提供为准
4.有时候ssl加密不行的时候,把’ssl’改成’tls’试试
5.messageconfig中的from要填得和上面的username一样
6.’useFileTransport’ => false不能设定为true,否则只会生成邮件的文件而不会发送

二.调用

        $mail= Yii::$app->mailer->compose();           $mail->setTo($email);  //$email就是收件人的邮件地址        $mail->setSubject("Test");   //邮件的标题        $mail->setHtmlBody('<br>test');    //发布可以带html标签的文本        $mail->send();    //发送邮件
0 0
原创粉丝点击