Skip to content
Advertisement

Use default Laravel email layout for my email templates

I have a Laravel mailable like this:

...
public function build()
{

    return $this->subject('Ticket Created ['.$this->ticket->ticket_code.']')
        ->view('emails.support.ticketcreated')->with([
            'ticket' => $this->ticket
        ]);
}

Then I have a template for the email in my views directory:

<div class="card">
    <div class="card-header bg-dark">
        Ticket Created [{{ $ticket->ticket_code }}]
    </div>
    <div class="card-body">
        <p>Thanks for creating a ticket!  We will get back to you as soon as possible</p>
        <p><a href="{{ $ticket->url }}">To view your ticket click here</a></p>
    </div>
</div>

I want to wrap this in the default Laravel email template (so put all the above into the $slot section in the layout)

I’ve published the Laravel email templates using:

php artisan vendor:publish --tag=laravel-mail

And I can see the layout file that I want to use under:

resources/views/vendor/mail/html/layout.blade.php

The problem is I can’t seem to use this layout.

I’ve tried the following (but all have failed):

  1. Wrapping the email template with but then it just tries to use my app layout (not the mail layout).

  2. Using @extends('vendor.mail.html.layout') and @section('slot')

  3. Tried adding ->layout(‘vendor.mail.html.layout’); to the end of the view() in mailable build() (just gives error method not found)

How can I use this layout for my emails. Note that I don’t want to create a separate duplicate layout as then I’ll have two layouts to manage.

Advertisement

Answer

Default Email Template Layout In Laravel Here is the official link of : Writing Markdown Messages enter image description here

Here is my Mail Controller

<?php

namespace AppMail;

use AppModelsUser;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

class NewUserRegistration extends Mailable
{
    use Queueable, SerializesModels;
    protected $user;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->to($this->user->email,$this->user->first_name.' '.$this->user->last_name)
                    ->cc('ccemail@ccemail.com','CC USER NAME')
                    ->from(env('MAIL_FROM_ADDRESS'),env('MAIL_FROM_NAME'))
                    ->subject('subject_of_your_email')
                    ->markdown('emails.registration',[  // This is very important line
                        'first_name'    => $this->user->first_name,
                        'last_name'     => $this->user->last_name,
                    ]);
    }
}

This is another controller from where i am executing my email

$getUserData = User::where('id',$userId)->first();
Mail::to($getUserData)->send(new NewUserRegistration($getUserData));
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement