yii2邮件配置教程,报Expected response code 250 but got code "553"原因
main.php(或main-local.php)或者web.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,//false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', //每种邮箱的host配置不一样 'username' => 'zhong_mail_test', 'password' => '**********',//163邮箱的授权码 'port' => '25', 'encryption' => 'tls', ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['zhong_mail_test@163.com'=>'zhong-mail'] ], ],
发送邮件的代码如下:
return Yii::$app ->mailer ->compose( ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user] ) ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot']) ->setTo($this->email) ->setSubject('Password reset for ' . Yii::$app->name) ->send();
如就按照上述配置会报:
Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user
"
这是因为有些邮件服务器要求from和username必须一致,163邮箱就是这样。
为避免这个问题,我们可以将发件代码中的setFrom设置去掉,或者在param.php(或param-local.php)中配置supportEmail参数:
'supportEmail' => 'zhong_mail_test@163.com'
这样就能成功发送邮件了。