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


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'

這樣就能成功發送郵件了。

 

上面那個是配置supportEmail參數發出的郵件,下面這個是注釋掉setFrom發出的郵件。

具體郵件內容的配置在common/mail目錄下。

關於163郵箱的授權碼:登錄-》設置-》POP3/SMTP/IMAP-》按提示開啟SMTP即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM