Display Laravel Notification (MailMessage) with markdown after sent
Asked Answered
B

1

1

I'm saving every email I send to an entity into the database by creating a function storeEmail and make an insert of MailMessage class into EmailMessage model. Everything works fine, and the main goal is to display the message exactly as it was, when the recipient received it and retrieve all the messages I sent as a User, to a page. To be much easier to retrieve a render of each specific Message in foreach loop, I think is better to fetch it from the Model.

This is my Notification 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. I'm using Illuminate\Notifications\Messages\MailMessage
  2. My class extends Illuminate\Notifications\Notification
  3. I'm saving (new MailMessage) in the $email->mail = $mail;

I tried to dd($email->mail); and I get this:

 ^ 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 ▶"

How can I display the Mail Notification, as it was when I sent it ? What is the optimal solution for that ? Thanks, in advance

EDITED

Managed to render MailMessage using this code works :

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

But this is not exactly what I wanted, because every time I need to find:

  1. Which Notification class used on every email so I can render it.
  2. Variables for each Notification class.
Bracketing answered 27/1, 2020 at 15:42 Comment(4)
You can render them in the browser laravel.com/docs/master/mail#rendering-mailablesHaply
Yes I know, but how to render the message from EmailMessage Model ?? Because, it is easier to access the message from the Model instead of searching the Notification class and pass the necessary variables every time.Bracketing
In the docs they have return (new App\Mail\InvoicePaid($invoice))->render(); so in your example, try just passing in $email or $email->mail in place of their $invoice. Correct the mailable path too of causeHaply
No, it doesn't work because $invoice is a variable used in InvoicePaid classBracketing
E
2

In order to accomplish this:

1. You can create a accessor.

2. Use Markdown's render method.

3. Pass in render method the mail's markdown you saved in storeEmail.

You can see an example above :

use \Illuminate\Mail\Markdown;

public function getRenderAttribute(){
    $markdown = new Markdown(view());
    return $markdown->render($this->mail['markdown'], $this->mail);
}
Execration answered 30/1, 2020 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.