【问题标题】:how to send custom email notifications with laravel?如何使用 laravel 发送自定义电子邮件通知?
【发布时间】:2020-02-01 10:31:35
【问题描述】:

我想通过电子邮件将数据库中的帖子标题和正文发送给所有用户,并且电子邮件已发送,但默认文本显示在电子邮件中。 数据库中的标题未通过电子邮件发送。

通知类

class RepliedToThread extends Notification
{
use Queueable;
public $event;


public function __construct($event)
{
    $this->event=$event;
}

public function via($notifiable)
{
    return ['database','broadcast', 'mail'];
}

public function toDatabase($notifiable)
{
    return[
        'event'=>$this->event,
        'user'=>auth()->user()
    ];
}
public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'event'=>$this->event,
        'user'=>auth()->user()
    ]);
}
public function toMail($notifiable)
{
    return new MailMessage([
        'data' => [
            'event'=>$this->event,
            'user'=>auth()->user()
        ]

    ]);

}

public function toArray($notifiable)
{
    return [
        //
    ];
}
}

控制器代码:

  public function store(Request $request)
{
    $this->validate($request, [
        'title'=>'required',
        'body'=>'required',
        'color' =>'required',
        'start_date' =>'required',
        'end_date' =>'required',

    ]);
    $event = new Event;
    $event->title = $request->title;
    $event->body = $request->body;
    $event->color = $request->color;
    $event->start_date = $request->start_date;
    $event->end_date = $request->end_date;
    $event->save();
    $user = User::where('id', '!=', auth()->user()->id)->get();
   \Notification::send($user,new RepliedToThread(Event::latest('id')->first()));
    return redirect()->route('events.index');
}

电子邮件已发送,但默认文本显示在电子邮件中,数据库中的帖子标题未在电子邮件中发送。

我想通过电子邮件将帖子标题和正文从数据库发送给所有用户。

【问题讨论】:

    标签: mysql laravel


    【解决方案1】:

    Laravel 通知

    在 Laravel 通知中:

    public function __construct(array $event)
    {
        $this->event = $event;
    }
    public function toMail($event)
    {
        return (new MailMessage)
                    ->line($this->event->title)                    
                    ->line($this->event->body);
    }
    

    在控制器中:

    Notification::send($user,new RepliedToThread($event));
    

    通知布局模板存储在这里/vendor/laravel/framework/src/Illuminate/Notifications/resources/views

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 2017-08-01
      • 2020-04-16
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多