【问题标题】:Laravel custom password emailLaravel 自定义密码邮箱
【发布时间】:2018-08-29 13:55:21
【问题描述】:

我使用 laravel 5.6 并成功将视图作为电子邮件发送。

我使用这个代码:

Mail::to($user->email)->send(new Welcome($user));

我唯一的问题是密码重置。我知道我可以自定义一些模板,但是如何覆盖默认的电子邮件模板并发送我自己的视图?

我尝试编写自己的 ResetPasswordNotification :

<?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPasswordNotification extends ResetPassword
{
/**
 * Build the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    if (static::$toMailCallback) {
        return call_user_func(static::$toMailCallback, $notifiable, $this->token);
    }

    return (new MailMessage)
        ->line('Vous recevez cet email car une demande de modification du mot de passe pour votre compte a été initialisée.')
        ->action('Réinitialiser le mot de passe', url(config('app.url').route('password.reset', $this->token, false)))
        ->line('Si vous n\'êtes pas à l\'origine de cette demande, merci de contacter l\'équipe du site.');
}
}

但我只能翻译电子邮件。我想要的是根据自己的模板发送自己的视图。

有可能吗?

感谢您的帮助。

【问题讨论】:

  • 您可以使用自定义视图返回您自己的可邮寄类,而不是返回MailMessage

标签: laravel


【解决方案1】:

您可以使用MailMessage 定义自己的视图

例子:

return (new MailMessage)->view(
    'your.email.blade', [
        'data' => $data
    ]
);

请参阅https://laravel.com/docs/5.6/notifications#mail-notifications 了解更多信息。

编辑:

由于您使用的是密码重置通知,因此您需要先找到用户。我相信toMail 中的$notifiable 对象应该是CanResetPassword 特征的一个实例,因此您必须使用电子邮件查找用户:

public function toMail($notifiable) {
{
    $email = $notifiable->getEmailForPasswordReset();
    $user = User::where('email', '=', $email)->first();

    return (new MailMessage)->view(
        'your.email.blade', [
            'user' => $user,
        ]
    );
}

【讨论】:

  • 感谢它的工作!是否可以获取用户信息?
  • 好的,我明白了,但我在哪里添加 $user->notify ?我只从 ResetPasswordNotification 复制 toMail
  • 我的用户控制器中有这个通知,但它不知道 $user。
  • 我找到了一个简单的方法,$notifiable 包含用户数据
  • 是的,抱歉,刚刚意识到这是密码重置通知,它不会专门从用户模型调用通知。
猜你喜欢
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 2015-01-22
  • 1970-01-01
  • 2018-12-17
  • 2017-03-24
  • 2017-03-29
相关资源
最近更新 更多