Laravel 5.4^ - How to customize notification email layout?

前端 未结 8 931
生来不讨喜
生来不讨喜 2020-12-01 08:07

I am trying to customize the HTML email layout that is used when sending notifications via email.

I have published both the mail and notification views.

相关标签:
8条回答
  • 2020-12-01 08:40

    I wrote an article on how to create a notification and modify your template including the header and footer.

    It includes the explanation on how the Laravel components work and how to pass your data to a new email template.

    https://medium.com/@adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c

    The most important part is placing the following code inside your email template:

    @component('mail::layout')
        {{-- Header --}}
        @slot('header')
            @component('mail::header', ['url' => config('app.url')])
                Header Title
            @endcomponent
        @endslot
    {{-- Body --}}
        This is our main message {{ $user }}
    {{-- Subcopy --}}
        @isset($subcopy)
            @slot('subcopy')
                @component('mail::subcopy')
                    {{ $subcopy }}
                @endcomponent
            @endslot
        @endisset
    {{-- Footer --}}
        @slot('footer')
            @component('mail::footer')
                © {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
            @endcomponent
        @endslot
    @endcomponent
    

    You can check the medium article in case you want more details on how the components work and how to properly pass the data.

    0 讨论(0)
  • 2020-12-01 08:42

    I ended up just using a custom view rather than trying to get the built in Laravel ones to work.

    I added the following use statement to my Notification class

    use Illuminate\Support\Facades\View;
    use Illuminate\Support\HtmlString;
    use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
    

    Then in the toMail method:

    public function toMail($notifiable)
        {
            $view_file = 'emails.teamInvitation';
            $view = View::make($view_file, ['sender' => $this->sender, 'invitationToken' => $this->invitationToken, 'team' => $this->team ]);
    
            $view =  new HtmlString(with(new CssToInlineStyles)->convert($view));
    
            return (new MailMessage)
                ->subject('PreSource Invitation From ' . $this->sender->name )
                ->view('emails.htmlBlank', ['bodyContent' => $view]);
        }
    

    emails.teamInvitation is my actual email template.

    I compile the view in to a string, and then convert the stylesheets to be inline.

    emails.htmlBlank is a view file but all it does is echo out bodyContent. This is necessary because the MailMessage->view method expects a view file, and not an HtmlString.

    0 讨论(0)
提交回复
热议问题