PHP code example of skitlabs / laravel-mailgun-multiple-domains

1. Go to this page and download the library: Download skitlabs/laravel-mailgun-multiple-domains library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

skitlabs / laravel-mailgun-multiple-domains example snippets


 declare(strict_types=1);

// app/Mail/ImportantMessage.php
class ImportantMessage extends \Illuminate\Mail\Mailable
{
    public function build() : self
    {
        return $this
            ->subject('Important message')
            ->from('[email protected]') // The sender is often determined _inside_ the mailable
            ->view('...', []);
    }
}

/** @var \App\Mail\ImportantMessage $mailable */

// Without this package, the calling code has to select the right mailer 
\Illuminate\Support\Facades\Mail::mailer('mailgun-acme.tld')->to('[email protected]')->queue($mailable);

// This package will handle the mailer configuration for you. So the above is as simple as;
\Illuminate\Support\Facades\Mail::to('[email protected]')->queue($mailable);

 declare(strict_types=1);

// config/services.php

return [
    // ... Other services

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
        'domains' => [
            'example.net' => [
                'domain' => 'custom-mg-domain.example.net',
                'secret' => 'overwrite-the-secret-or-null',
                'endpoint' => 'overwrite-the-endpoint-or-null',
            ],
            'awesome.app' => [
                'secret' => 'only-change-the-secret-for-this-domain',
            ],
        ],
    ],
];

 declare(strict_types=1);

use Illuminate\Support\ServiceProvider;
use SkitLabs\LaravelMailGunMultipleDomains\Contracts\MailGunSenderPropertiesResolver;

// app/Providers/AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // ...

        $this->app->bind(MailGunSenderPropertiesResolver::class, static function () : MailGunSenderPropertiesResolver {
            return new \Acme\CustomSenderPropertiesResolver();        
        });
    }
}

 declare(strict_types=1);

use Illuminate\Support\Facades\Config;
use SkitLabs\LaravelMailGunMultipleDomains\Contracts\MailGunSenderPropertiesResolver;
use SkitLabs\LaravelMailGunMultipleDomains\Listeners\ReconfigureMailGunOnMessageSending;

Config::set('mail.default', 'custom-mailer-name');
Config::set('mail.mailers.custom-mailer-name', [
    'transport' => 'mailgun',
]);

/** @var MailGunSenderPropertiesResolver $resolver */
$handler = new ReconfigureMailGunOnMessageSending($resolver, 'custom-mailer-name');