Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you customize variables in Laravel default emails?

I followed this answer to publish the default email templates in my application with:

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

This works great, but clearly there is some configuration options, such as:

{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level === 'error')
# @lang('Whoops!')
@else
# @lang('Hello!')
@endif
@endif

{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
@lang('Regards'),<br>{{ config('app.name') }}
@endif

Right now my emails are sending "Hello!" and "Regards" from the else section but clearly there is a way to set these defaults for email templates using variables. How do I set the $greeting and $salutation variables when sending emails?

like image 309
Connor Leech Avatar asked Dec 13 '18 22:12

Connor Leech


1 Answers

The template you posted is the default template for notification mails. When creating such notification with for example:

php artisan make:notification InvoicePaid --markdown=mail.invoice.paid

A new InvoicePaid class is created at app/Notifications/InvoicePaid.php. This class contains a toMail() method with the following contents:

return (new MailMessage)->markdown('mail.invoice.paid');

The MailMessage class extends the SimpleMessage class. The SimpleMessage class has the methods greeting() and salutation() which you can use to set the greeting or salutation.

For example:

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
           ->greeting("Your custom greeting")
           ->salutation("Your salutation goes here")
           ->markdown('mail.invoice.paid');
}
like image 193
Chris Avatar answered Sep 20 '22 15:09

Chris