10

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.

Ryan B
  • 108
  • 1
  • 1
  • 13
Spialdor
  • 1,475
  • 5
  • 20
  • 46
  • I think you should not extend the Mailable in that way. Why don't you just create a proper notification, like "php artisan make:notification WelcomeNotification" -- https://laravel.com/docs/5.6/notifications#mail-notifications – jannej Jul 04 '18 at 20:47

4 Answers4

10

Publish notifications from vendor and you can send it in markdown

    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->markdown('vendor.notifications.email', $this->message->data());
    }
Nikolay
  • 190
  • 2
  • 9
  • 1
    Or without publishing: return $this->markdown('notifications::email', $this->message->data()); Great tip btw – Sergiu Nov 18 '20 at 09:59
10

I used this in Laravel 8, not sure if it is still compatible with previous versions of Laravel. The point is to use the html method in the Mailable.

<?php

namespace App\Mail;

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

class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->subject('My Subject')
            ->html((new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!')
                ->render()
            );
    }
}

Ifan Iqbal
  • 3,053
  • 5
  • 28
  • 31
  • 1
    I needed to call `render()` method on the mail message for it to work, otherwise laravel throws error. `(new MailMessage)->line(...)->render()`, but your answer lead me in the right direction. thanks – Arun A S Mar 08 '21 at 07:55
  • This is the way to do it, I needed a way to use Laravel's built in notifications email templates to maintain consistency, they should make this a standalone engine to generate emails, that you can eventually attach to Notifications. – manu144x Jun 13 '21 at 23:22
9

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.

christiaan
  • 348
  • 2
  • 11
0

You don't have to use markdown, just because you dont want to upgrade laravel. https://laravel.com/docs/5.6/notifications#mail-notifications

specify the view

    return $mail->view('emails.default');
Harry Bosh
  • 3,611
  • 2
  • 36
  • 34