Change Laravel mailer config on-the-fly

If you dependency inject Mailer into your class, it will have the original mailer config specified in your .env file. If you change the config like $this->config->set('services.sparkpost.secret', $mailConfig->key); $this->config->set('mail.driver', 'sparkpost');, the changes won't be applied.

To update the mailer with the new config, you need to reconfigure the mailer transport:

$this->container['swift.transport'] = $this->container->share(function ($app);
    return new TransportManager($app);

$mailer = new \Swift_Mailer($this->container['swift.transport']->driver());