Adam Lambert
Adam Lambert

Reputation: 1421

Laravel markdown mail - multiple templates. How to define path for @component('mail::*')

As detailed in config/mail.php, the default markdown mail folder is in views/vendor/mail

'markdown' => [
    'theme' => 'default',

    'paths' => [
        resource_path('views/vendor/mail'),
    ],
],

I have created a new template in views/templates/1/mail. Change the path in the config and this works fine.

How can I use both the default vendor/mail folder as well as my new template for different notifications?

Adding my path to the mail config kind of works, but this is more to add additional files to the current template and requires that all files have unique names across every folder.

I can of course namespace the entire template path inside each blade file through to the HTML folder, but is there a more elegant way to hook up the mail::xxxx helper?

@component('templates/1/mail/html.layout')

Upvotes: 2

Views: 3226

Answers (1)

Gaurav Dave
Gaurav Dave

Reputation: 7474

To use mail::button type format, you have to create hint for that keyword, which is nowhere to be found in Laravel documentation.

As an alternate solution, this is how I'm using multiple layout for the Laravel notification:

  1. Clone you existing mail (project/resources/views/vendor/mail) folder and customize the new layout. Inside mail folder you can customize the complete design. Files under this folder, provides blocks level changes to your email body.

  2. Next, You have to clone email.blade.php file under notifications (project/resources/views/vendor/notifications), and customize it as per your need. This single file is the skeleton of your email.

  3. Modify your email notifications to add view() or markdown() as per your need. In your Notification's 'toMail' function, chain the below line:

    ->view('vendor.notifications.custom_layout')
    

So, it will look like this:

return (new MailMessage)
            ->subject(__('emails/registration.subject'))
            ->greeting(__('emails/registration.failed.greetings', ['recipient' => $this->learner->username]))
            ->line(__('emails/registration.line_1'))
            ->line(__('emails/registration.line_2', ['username' => $this->username]))
            ->action(__('action.log_in'), "https://{$loginURL}")
            ->view('vendor.notifications.custom_layout');
  1. All the file naming convention will change from mail::message to vendor.custom_layout.html.message

This is how your custom file (under notifications folder) will look like:

@component('vendor.custom_layout.html.message')
{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level == 'error')
# @lang('Whoops!')
@else
# @lang('Hello!')
@endif
@endif

{{-- Intro Lines --}}
@foreach ($introLines as $line)
{!! $line !!}

@endforeach

{{-- Action Button --}}
@isset($actionText)
@component('vendor.custom_layout.html.button', ['url' => $actionUrl])
    {{ $actionText }}
@endcomponent
@endisset

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{!! $line !!}
@endforeach

{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
{{ __('label.thank_you') }}<br>{{ __('label.xyz_team') }}
@endif

{{-- Subcopy --}}
@isset($actionText)
@component('vendor.custom_layout.html.subcopy')
{!! __('emails/common.footer_notice', ['actionText' => $actionText]) !!}
[{{ $actionUrl }}]({!! $actionUrl !!})
@endcomponent
@endisset
@endcomponent

These are the only changes which are required to make a customized view for your emails.

Upvotes: 3

Related Questions