Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Sending mail blade template with variables through Mailgun php SDK.

Hi everybody, I'm using Mailgun's php SDK to send mails, after looking through the docs you can apparently send html/text mails but what about laravel's blade templates, my templates have some variables like order details and user info that I want my templates to have, is it possible to do?

So my questions are:

How can I share variables with my blade templates and send them through Mailgun's API.

How can I reference my my blade templates when filling the parameters of my mailgun API call.

I 'd rather not download any composer package like Bogardo since they are outdated.

This is what I have so far:

public static function sendMailGun($order)
    {
        $mailData = [
            'subject' => 'Tus códigos de descuento para '.config('app.name').'.',
            'destination' => $order['purchaser']['email'],
            'order' => $order
        ];

        $mg = Mailgun::create('YOUR_API_KEY');
        //how can I pass $mailData into my views?
        $mg->messages()->send('YOUR_DOMAIN_NAME', [
            'from'    => 'Excited User <mailgun@YOUR_DOMAIN_NAME>',
            'to'      => 'Baz <YOU@YOUR_DOMAIN_NAME>',
            'subject' => $mailData['subject'],
            'html'    => 'I WANT TO REFERENCE emails.discount-mail.blade.php HERE'
        ]);
    }
0 likes
6 replies
mvd's avatar

Hi @gabotronix,

Change your maildriver to Mailgun, it is already installed in Laravel (see config/mail.php) Now you can use the default mail functionlity from Laravel

bobbybouwmann's avatar

You can just render a view and send that along

$html = view('emails.discount-mail.blade.php', $mailData)->render();

$mg->messages()->send('YOUR_DOMAIN_NAME', [
    'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>',
    'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>',
    'subject' => $mailData['subject'],
    'html' => $html,
]);
bobbybouwmann's avatar

As @mvd said, this is basic functionality that is already available. The only reason to use Mailgun directory if you want to use more features than Laravel is offering. if it's just sending html emails, the Laravel default should be good enough for you ;)

Neven's avatar

Hi, A Maill::class exist by default in Laravel. You dont need Mailgun. To transmit a variable you need to do something like this :

Mail::send('emails.discount-mail', compact('mailData'), function ($message) use ($mailData) {
          $message
                ->from('YOUR_EMAIL@YOUR_DOMAIN', 'CONTACT_TITLE')
                ->to($mailData['destination'])
                ->subject($mailData['subject']);
        });     

I hope I helped, cordially

Gabotronix's avatar

@BOBBYBOUWMANN - Noted, do you have any idea how to send batch emails through Mailgun using laravel Mail facade? I guess it would be possible to extend this feature but I haven't found any example or tutorial.

bobbybouwmann's avatar

@gabotronix Just a foreach loop would be good enough I guess.

If you use the Mailgun SDK they probably have a separate endpoint for that? Not sure, but can probably find that in their documentation ;)

Please or to participate in this conversation.