发送后显示 Laravel 带有标记的通知(MailMessage)

Display Laravel Notification (MailMessage) with markdown after sent

我通过创建函数 storeEmail 并将 MailMessage class 插入到 EmailMessage 模型中,将我发送给实体的每封电子邮件保存到数据库中.一切正常,主要 目标 是在收件人收到消息时按原样显示消息并检索我作为 User 发送到页。为了更容易地在 foreach 循环中检索每个特定消息的渲染,我认为最好从模型中获取它。

这是我的通知class:

class SimpleEmail extends Notification
{
    use Queueable;

    private $link;
    private $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($link)
    {
        $this->link = $link;
        $this->user = Auth::user();
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {   
        $mail = (new MailMessage)
            ->from($this->user->email, $this->user->name)
            ->subject('My Dummy Subject')
            ->greeting('To: '.$notifiable->email)
            ->action('Action Button', url($this->link))
            ->line('Thank you for reading my message')
            ->salutation('Friendly, '.$this->user->name);

        $this->storeEmail($mail,$notifiable);
        return $mail;
    }

    public function storeEmail($mail,$notifiable){
        $email = new EmailMessage;
        $email->sender_type = 'App\User';
        $email->sender_id = $this->user->id;
        $email->mail = $mail;
        $email->save();
        $notifiable->email_messages()->save($email);
    }
}

Note:

  1. 我正在使用 Illuminate\Notifications\Messages\MailMessage
  2. 我的 class 扩展 Illuminate\Notifications\Notification
  3. 我正在将 (new MailMessage) 保存在 $email->mail = $mail;

我尝试 dd($email->mail); 并且我得到了这个:

 ^ array:20 [▼
  "view" => null
  "viewData" => []
  "markdown" => "notifications::email"
  "theme" => null
  "from" => array:2 [▶]
  "replyTo" => []
  "cc" => []
  "bcc" => []
  "attachments" => []
  "rawAttachments" => []
  "priority" => null
  "callbacks" => []
  "level" => "info"
  "subject" => "My Dummy Subject"
  "greeting" => "To: Dohn John"
  "salutation" => "Friendly, Nikolas Diakosavvas"
  "introLines" => array:2 [▶]
  "outroLines" => array:1 [▶]
  "actionText" => "Action Button"
  "actionUrl" => "http://my-example-url.com ▶"

如何显示邮件通知,就像我发送邮件时一样?最佳解决方案是什么? 提前谢谢

已编辑

设法使用此代码呈现 MailMessage 有效

$email = EmailMessage::first();
return (new \App\Notifications\SimpleEmail('my-link', $email->recipient->assignto))->toMail($email->recipient);

但这并不是我想要的,因为每次我都需要找到:

  1. 每封电子邮件都使用了哪个通知 class,以便我可以呈现它。
  2. 每个通知的变量 class。

为了完成这个:

1. 你可以创建一个accessor.

2.使用Markdownrender方法。

3. 传入 render 方法 mail 的 markdown 你保存在 storeEmail.

你可以看上面的例子:

use \Illuminate\Mail\Markdown;

public function getRenderAttribute(){
    $markdown = new Markdown(view());
    return $markdown->render($this->mail['markdown'], $this->mail);
}