laratusk/laravel-mailgun-multi

Send emails from multiple Mailgun domains in one Laravel app — automatic transport reconfiguration based on sender domain.
1,036 1
Install
composer require laratusk/laravel-mailgun-multi
Latest Version:v4.3.0
PHP:^8.2
License:MIT
Last Updated:Aug 5, 2026
Links: GitHub  ·  Packagist
Maintainer: laratusk

Multiple Mailgun Domains in one Laravel app

PHP Version Laravel License

Send emails from multiple Mailgun domains in a single Laravel application — automatic transport reconfiguration based on the sender's domain. No changes required in calling code.

// Without this package: calling code must select the right mailer manually
Mail::mailer('mailgun-acme')->to('j.doe@example.net')->send($mailable);

// With this package: just send — transport is reconfigured automatically
Mail::to('j.doe@example.net')->send($mailable);

Requirements

  • PHP >= 8.2
  • Laravel 10, 11, 12, or 13

Installation

composer require laratusk/laravel-mailgun-multi

Laravel's auto-discovery will register the service provider automatically.

How it works

This package listens to the Illuminate\Mail\Events\MessageSending event, which Laravel dispatches just before sending each email. The listener reads the from address, extracts the sender domain, and reconfigures the Mailgun transport accordingly.

This works seamlessly for both direct and queued messages — no changes to your Mailables or controllers needed.

Usage

Basic setup

Ensure your config/services.php has a mailgun entry:

'mailgun' => [
    'domain'   => env('MAILGUN_DOMAIN'),
    'secret'   => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],

If your sender address is sales@acme.app, the package will automatically use mg.acme.app as the Mailgun domain.

Per-domain configuration

Add a domains key to override settings for specific sender domains:

'mailgun' => [
    'domain'   => env('MAILGUN_DOMAIN'),
    'secret'   => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    'domains'  => [
        'acme.com' => [
            'domain'   => 'mg.acme.com',
            'secret'   => 'acme-mailgun-secret',
            'endpoint' => 'api.eu.mailgun.net',
        ],
        'awesome.app' => [
            // Only override the secret; domain and endpoint fall back to global defaults
            'secret' => 'awesome-secret',
        ],
    ],
],

If a domain is not listed, the Mailgun domain defaults to mg.{sender-domain}. Missing secret or endpoint values fall back to the global mailgun config.

Optional: enable domain switch logging

'mailgun' => [
    // ...
    'log_domain_switches' => true, // logs debug info on each transport reconfiguration
],

Custom resolver

If the default config-based resolution does not fit your use case, implement the MailgunSenderPropertiesResolver contract:

use Laratusk\LaravelMailgunMulti\Contracts\MailgunSenderPropertiesResolver;
use Laratusk\LaravelMailgunMulti\DataObjects\MailgunSenderProperties;

class MyCustomResolver implements MailgunSenderPropertiesResolver
{
    public function resolve(string $senderDomain): MailgunSenderProperties
    {
        // Fetch config from database, cache, or any source
        return new MailgunSenderProperties(
            domain: 'mg.' . $senderDomain,
            secret: 'my-secret',
            endpoint: 'api.mailgun.net',
        );
    }
}

Then bind it in a service provider:

use Laratusk\LaravelMailgunMulti\Contracts\MailgunSenderPropertiesResolver;

$this->app->bind(MailgunSenderPropertiesResolver::class, MyCustomResolver::class);

Testing

composer test

Run all quality checks (format + static analysis + tests):

composer quality

Credits

This package is a maintained fork of skitlabs/laravel-mailgun-multiple-domains by Jurre Vriezinga, which is no longer actively maintained.

Contributing

Contributions are welcome. Please open an issue or pull request on GitHub.

License

The MIT License (MIT). Please see License File for more information.

Related Packages

daanra/mailgun

Laravel package for sending mail via the Mailgun API with support for Laravel 7

13,240 2
ritey/mailgun

A laravel 5 wrapper for sending mail via the Mailgun API in Laravel style

25 1
omnimail/omnimail

PHP Library to send email across all platforms using one interface.

36,872 331
larablocks/pigeon

A more flexible email message builder for Laravel 5 including chained methods, r...

3,706 14

Version History

Version Released PHP Laravel License
v4.3.0 ^8.2 ^10.0|^11.0|^12.0|^13.0 MIT
v4.2.0 ^8.2 ^10.0|^11.0|^12.0|^13.0 MIT
v4.1.0 ^8.2 ^10.0|^11.0|^12.0|^13.0 MIT
v4.0.1 ^8.2 ^10.0|^11.0|^12.0 MIT
v4.0.0 ^8.2 ^10.0|^11.0|^12.0 MIT