How do you customize variables in Laravel default emails?
Asked Answered
C

1

7

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?

Casta answered 13/12, 2018 at 22:34 Comment(2)
I'm trying to find where the definition for @lang('Regards') is stored. It isn't exposed in my code base currently. It may be helpful if anyone can add that info as a comment or additional answer here. I found this question by Googling @lang('Regards') with double-quotes around it. I'd like to add something like @lang('Team') adjacent to the Regards definition.Chancroid
But it's worth noting that resources/lang is a folder location, and a person may be able to add any customizations there. I just haven't learned enough about that yet to say how to do that.Chancroid
A
8

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');
}
Anesthetize answered 13/12, 2018 at 22:51 Comment(2)
Okay very helpful! I'm looking to overwrite the sendEmailVerificationNotification template. So I'm guessing I should overwrite that method in app/User.php and create a new mailable with that markdown flag. I'll probably copy some of the functionality from Illuminate\Auth\Notifications\VerifyEmailCasta
Yes, just overwrite the sendEmailVerificationNotification method in your User model and you should be good.Anesthetize

© 2022 - 2024 — McMap. All rights reserved.