Search code examples
laravelemail

Laravel use MailMessage in Mailable


I'm trying to send to my users an email when they register, I created a mailable class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;

public $user;
public $message;
/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($user)
{
    $this->user = $user;

    $this->message = (new MailMessage)
        ->greeting('Bonjour '.$user->name)
        ->line('Nous vous remercions de votre inscription.')
        ->line('Pour rappel voici vos informations :')
        ->line('Mail: '.$user->email)
        ->line('Password: '.$user->password);
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('message')->with([
        'slot' => $this->message
    ]);


}

But if I use a custom template I have to do all the css and Html, I see that the forgot password email uses a MailMessage, and all the templates are already made with the MailMessage template.

Is there any way for me to use the same template or directly create a MailMessage template but with my custom content?

Thanks.


Solution

  • You are mixing 2 separate Laravel concepts, Notifications and Mailers. Notifications can be Mailers, but Mailers can not be Notifications.

    The MailMessage class is a notification message, but cannot be the message for a Mailable. To send a MailMessage mailer you should extend the Notification class:

    <?php
    
    namespace App\Notifications;
    use Illuminate\Notifications\Notification;
    use Illuminate\Notifications\Messages\MailMessage;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Bus\Queueable;
    
    class WelcomeNotification extends Notification implements ShouldQueue
    {
        use Queueable, SerializesModels;
    
        public $user;
    
        public function __construct($user)
        {
            // The $notifiable is already a User instance so not really necessary to pass it here
            $this->user = $user;
        }
    
        public function via($notifiable)
        {
            return ['mail'];
        }
    
        public function toMail($notifiable)
        {
    
            return (new MailMessage)
                ->greeting('Bonjour '.$this->user->name)
                ->line('Nous vous remercions de votre inscription.')
                ->line('Pour rappel voici vos informations :')
                ->line('Mail: '.$this->user->email)
                ->line('Password: '.$this->user->password);
        }
    
    }
    

    Also, see Laravel's ResetPassword notification as example.

    To send the notification to a user:

    $user->notify(new WelcomeNotification($user));
    

    In this manner you can create generic mail messages using the default mail notification template.