yii2邮件配置教程,报Expected response code 250 but got code "553"原因

时间:2021-10-23 13:58:52

main.php(或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,//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'

这样就能成功发送邮件了。

yii2邮件配置教程,报Expected response code 250 but got code "553"原因

上面那个是配置supportEmail参数发出的邮件,下面这个是注释掉setFrom发出的邮件。

具体邮件内容的配置在common/mail目录下。

关于163邮箱的授权码:登录-》设置-》POP3/SMTP/IMAP-》按提示开启SMTP即可。