3

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.

David B
  • 400
  • 2
  • 15
  • may [this](https://stackoverflow.com/a/51095799/7574023) can help you. also there are many other useful answers – boolfalse Jan 08 '21 at 00:57

2 Answers2

2

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 App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

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));
Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

Exacly same issue starting laravel 5.6 trough 7 to 9. I dont know if the documentation is not clear or what but i've never seemd to properly use their templates and ended up setting new ones from scratch. And here we are 6 ears later i'm hitting the same wall. Sorry couldnt help but meybe someone will submit a valid guide for this here. Cheers.

user2634449
  • 114
  • 1
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33200797) – ahuemmer Nov 21 '22 at 13:27