laravel 重置密码时发送邮件失败 553错误 - 小众知识

laravel 重置密码时发送邮件失败 553错误

2020-08-19 16:37:00 苏内容
  标签: laravel//mail
阅读:5035

错误信息如下

Swift_TransportException in AbstractSmtpTransport.php line 383:Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user
"

 通常搜索到的解决方案如下:

修改:

在.env配置中添加

MAIL_FROM_NAME=yourtest@163.com
MAIL_FROM_ADDRESS=yourtest@163.com

 yourtest@163.com为你注册的邮箱账号和配置的MAIL_USERNAME相同


如果你的.env配置文件确实没有改的话,那么这里需要改一下。

但小编改了之后仍旧不行,后来试了单独调用Mail::send,已经可以发送邮件了,那问题肯定不出在这里,经过排查,小编按照教程修改了ResetPassword拓展了Notification


        return (new MailMessage) 

                ->from('admin@abc.org', 'admin')

                ->subject('重置密码') 

                ->line('您收到此电子邮件是因为我们收到了您帐户的密码重置请求.') 

                ->action('重置密码', url('password/reset', $this->token)) 

                ->line('如果您未请求重置密码,则无需采取进一步措施.'); 


竟然是这里不注意,里面的from内容覆盖了全局的MAIL_FROM_ADDRESS,修改其内容或去掉既可


Laravel里我们可以使用php artisan make:auth来生成一套默认的登陆注册重置邮箱的Authentication System,但是如何修改系统发送给用户的重置密码邮件的样式和内容呢?

Default Password Reset View

虽然默认的邮件样式很美观,但是不免全部是英文,我们至少可以添加进一些中文提示,方便用户查看。

首先我们需要明确的是:

  1. Laravel 默认的 Notification Class是ResetPassword,位于Illumintate/Auth/Notifications中。
  2. 我们不应该直接修改位于ResetPassword里的代码,因为如果更新package可能导致覆盖。

我们先来看一下ResetPassword:

<?phpnamespace Illuminate\Auth\Notifications;use Illuminate\Notifications\Notification;use Illuminate\Notifications\Messages\MailMessage;class ResetPassword extends Notification{
    public $token;    public function __construct($token)
    {
        $this->token = $token;
    }    public function via($notifiable)
    {
        return ['mail'];
    }    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('You are receiving this email because we received a password reset request for your account')
            ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false)))
            ->line('If you did not request a password reset, no further action is required.');
    }
}

可以看到,ResetPassword拓展了Notification这个类,所以我们需要做的就是新建一个Notification类,来完成我们自定义邮件内容的修改:

$ php artisan make:notification ResetPasswordNotification1

输入以上artisan命令,我们会发现在App\Notifications文件夹下多出了一个名为ResetPasswordNotification.php的文件,打开它,我们可以看到其内容跟ResetPassword很相似。我们只需要修改关键的代码即可:

<?phpnamespace Illuminate\Auth\Notifications;use Illuminate\Bus\Queueable;use Illuminate\Notifications\Notification;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Messages\MailMessage;class ResetPasswordNotification extends Notification{
    use Queueable;    public $token;    public function __construct($token)
    {
        $this->token = $token;
    }    public function via($notifiable)
    {
        return ['mail'];
    }    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('这里可以放我们需要添加的内容')
            ->line('You are receiving this email because we received a password reset request for your account')
            ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false)))
            ->line('这里可以放我们需要添加的内容')
            ->line('If you did not request a password reset, no further action is required.');
    }
}

可以看到,我们可以以line作为单位来添加我们需要的信息。


那么信息内容搞定了,怎么样修改邮件样式呢?首先我们需要能够修改信息的Blade模板:

$ php artisan vendor:publish --tag=laravel-notifications1

以上命令把包裹里的模板发布到resources/views/vendor/notifications文件夹中,这样我们只需要修改resources/views/vendor/notifications/email.blade.php就可以了。

最后一步,我们在User模型里添加:

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */public function sendPasswordResetNotification($token){
    $this->notify(new ResetPasswordNotification($token));
}

这样,我们就可以使用ResetPasswordNotification来进行邮件的发送了。

模板的修改很简单,这里就不赘述了,完成后,我们可以看到新的邮件内容:
Reset Password Improved View


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1