Multiple Actions in your Laravel Mailables

Last Updated at : April 23, 2019 By - Usama Muneer | 442

    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.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.

    Telescope gives knowledge into the solicitations coming into your application, exemptions, log sections, database questions, lined employments, mail, notices, reserve activities, planned errands, variable dumps and that's just the beginning.