Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel use MailMessage in Mailable

Tags:

email

laravel

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.

like image 907
Spialdor Avatar asked Jul 04 '18 19:07

Spialdor


People also ask

Can we use PHP mail function in Laravel?

Laravel provides drivers for SMTP, Mailgun, Mandrill, SparkPost, Amazon SES, PHP's mail function, and sendmail , allowing you to quickly get started sending mail through a local or cloud based service of your choice.

How do you check mail is sent or not in Laravel?

If failures() doesn't return anything then it has been successfully sent.


1 Answers

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());
    }
like image 97
Nikolay Avatar answered Nov 09 '22 23:11

Nikolay