5 months ago

Dynamically updating Mailgun configuration

Posted 5 months ago by booni3

I have a situation where we need to change the mailgun confiig (domain & secret) on the fly. We need it to be able to be changed both during a normal request and during queue jobs/commands.

I have a solution in place for normal request, but need some suggestions on how this could be done across the whole app.

The configuration should be loaded into the method via variable/request opposed to getting it from the database.

The following middleware works for sending a normal request but I cannot use this within my queues (in L5.8 currently).


namespace App\Http\Middleware;

use Closure;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\TransportManager;

class MailSwitcherMiddleware extends TransportManager

    public function __construct()
        $this->app = app();

     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)
        \Config::set('services.mailgun.domain', $request->maingun_domain);

        $this->app->extend('mailer', function(Mailer $mailer, $app){
            $swift = new \Swift_Mailer($this->createMailgunDriver());
            return $mailer;

        return $next($request);


Please sign in or create an account to participate in this conversation.