mardi 28 janvier 2020

Display Laravel Notification (MailMessage) with markdown after sent

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.


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire