Sitemap

How to Use Variables, Tags, and Other Features in Laravel Mailgun Email Sending

3 min readMay 9, 2025
Press enter or click to view image in full size

Introduction:
Sending emails through Mailgun with Laravel is easy — but what if you want to personalize your emails using Mailgun variables, track email engagement with tags, or manage campaigns effectively? In this blog, we’ll cover how to integrate advanced Mailgun features with Laravel’s native mail system.

🔧 Prerequisites

Make sure:

  • Laravel is installed (v8+ or v9+ recommended)
  • You have a Mailgun account
  • You’ve verified your domain in Mailgun

📦 Step 1: Install and Configure Mailgun

  1. Install Guzzle if not already installed
composer require guzzlehttp/guzzle

2. Configure .env

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=your-mailgun-private-api-key
MAILGUN_ENDPOINT=api.mailgun.net

3. Configure config/mail.php

'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
// other mailers...
],

'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@yourdomain.com'),
'name' => env('MAIL_FROM_NAME', 'Your App Name'),
],

✉️ Step 2: Create a Mailable

php artisan make:mail OrderShipped

Edit app/Mail/OrderShipped.php:

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class OrderShipped extends Mailable
{
use Queueable, SerializesModels;

public $user;
public $order;

public function __construct($user, $order)
{
$this->user = $user;
$this->order = $order;
}

public function build()
{
return $this->view('emails.orders.shipped')
->subject('Your Order has Shipped!')
->withSymfonyMessage(function ($message) {
// Set Mailgun tags
$message->getHeaders()->addTextHeader('X-Mailgun-Tag', 'order-shipped');

// Set custom variables
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', json_encode([
'user_id' => $this->user->id,
'order_id' => $this->order->id,
]));

// Campaign ID (optional)
$message->getHeaders()->addTextHeader('X-Mailgun-Campaign-Id', 'order-notifications');
});
}
}

🛠️ Step 3: Send the Email

use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;

$user = User::find(1);
$order = Order::find(123);

Mail::to($user->email)->send(new OrderShipped($user, $order));

📊 Step 4: View Analytics in Mailgun

  • Go to Mailgun Dashboard → Logs
  • Filter by tag: order-shipped
  • Use user_id, order_id from custom variables to debug or track emails

✅ Tips

  • Multiple Tags: Use multiple X-Mailgun-Tag headers if needed.
  • Recipient Variables: Use recipient-variables for batch emails (not covered here but available via API).
  • Campaigns: Great for newsletters or product launches.

🧪 Bonus: Testing with Mailgun Sandbox Domain

You can test your integration using the sandboxXXX.mailgun.org domain provided in your Mailgun account. Just remember that emails are not delivered to real recipients—only to those listed as authorized.

🔚 Conclusion

Mailgun is a powerful email API, and Laravel makes it easy to send emails. With the ability to add tags, variables, and campaign tracking, you can unlock a lot of potential in email-based workflows.

Go forth and get more out of your content. Go forth and conquer Medium! (and while you’re at it, follow me on Medium! and feel free to Subscribe)

Found this post useful? Kindly tap the 👏 button below! :)

🌟 Enjoy my blogging content? Support my work by buying me a virtual coffee on BuyMeACoffee! Your contributions help fuel insightful tech articles. Join hands in making coding more accessible and exciting for all. https://www.buymeacoffee.com/arjunamrutiya🚀

--

--

ArjunAmrutiya
ArjunAmrutiya

Written by ArjunAmrutiya

👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!

No responses yet