YII2 高级版本 发送163邮件
1 配置yii2-swiftmailer 扩展
编辑common/config/main-local.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// 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,//true的话,会在runtime/mail创建一个邮件文件,用于测试;false的话实时发送;
'transport' => [
'class' => 'Swift_SmtpTransport', //使用的类
'host' => 'smtp.163.com', //邮箱服务一地址
'port' => '25', //服务器端口
'username' => 'xxxxxxx@163.com',//你的邮箱地址
'password' => 'xxxxxxxxxxxxx', //自己填写邮箱密码,开通SMTP时的授权密码
'encryption' => 'tls', //加密方式
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['xxxxxxx@163.com'=>'xxxxxxxx']
],
],
2 在控制器中编写发送程序
$res = Yii::$app->mailer->compose()
//->setFrom('xxxxx.163.com') //注1
->setTo('1273567815@qq.com')
->setSubject('测试邮件')
->setTextBody('测试邮件')
->setHtmlBody('测试邮件')
->send();
注1:添加setFrom()函数时,如果是使用163邮箱发送的话,会产生Expected response code 250 but got code "554" 的错误。解决方法:可以将setFrom()函数一行注释或删除。
3 开通163邮箱的SMTP
POP/SMTP服务 显示 已关闭|开启 时,点击 开启 ,然后得到授权密码。