Multiple Actions in your Laravel Mailables

Imagine you have a scenario where you need to add multiple actions to your Laravel Notification where you can add as many as button components with different URLs.

Imagine you have a scenario where you need to add multiple actions to your Laravel Notification where you can add as many as button components with different URLs. We’ll walk through from setting up a simple Mail notification to a more structured component based markdown based notification.

Simple Mail Notification

Create a simple notification by running the following command.

php artisan make:notification SendTestEmail

In your app\Notifications\SendTestEmail, let us design a simple mail notification.

public function toMail( $notifiable ) {

return ( new MailMessage )
->subject('Notification Subject');
->line('Some Text here');
->action('some action button',url('actionurl')
->line('some more text')
->action('some other action button',url('anotheractionurl'));
}

If you test it out, you’ll see it will only show you a single action button on your email. But why is it so? you’ve used multiple actions here. Well there’s a simple solution to this. We’ll use the markdown mail notifications approach which gives us more freedom to write longer, customised messages.

Markdown Mail Notification

Let’s create another Notification but this time with a markdown.

php artisan make:notification SendAnotherTestEmail --markdown=mails.test

Now, in our app\Notifications\SendAnotherTestEmail, we’ll do something like:

public function toMail( $notifiable ) {

$url = 'someurl.com';
$another_url = 'someotherurl.com';

return ( new MailMessage )
->markdown( 'mails.test', [
'url' => $url,
'another_url' => $another_url
] );
}

Here we have passed a couple of variables to our markdown template.

And in our markdown file, we’ll design our email.

@component('mail::message')
# Test Markdown Email

Some Text here

@component('mail::button', ['url' => $url])
Some Button
@endcomponent

Some other Text here

@component('mail::button', ['url' => $another_url])
Some other Button
@endcomponent
Thanks,<br>
{{ config('app.name') }} Administrator

@endcomponent

Try and test this approach & you’ll get more control for your mail notifications. Also note that markdowns can’t have indents in it, so better be careful while writing markdowns or else it will show raw html in your email.