I need to be able to send email using Mailgun from one domain, switch settings, then send another from another domain. However, updating the settings before sending the second email does not work, the second email still gets sent using the first emails settings.
Initial settings are set in Config.mail
and Config.services
, this all works fine.
// send first email
try {
Mail::send(--stuff here--)
} catch (Exception $e) { ... }
// update config using the sandbox account details
Config::set('mail.username', '[email protected]');
Config::set('mail.password', 'password');
Config::set('services.mailgun.domain', 'domain.mailgun.org');
Config::set('services.mailgun.secret', 'key-secret');
// send second email
try {
Mail::send(--stuff here--)
} catch (Exception $e) { ... }
// Second email has now been sent using the first emails config settings
If I comment out the first email send, then change the settings as above, the second email get's sent correctly from the sandbox account. If I leave the first email in, it gets sent from a domain I have on MailGun.
Does anyone have any experience with this?
Thanks for the answer above. Unfortunately in Laravel 5.4 share()
was removed and this would no longer work. Updated version of the code above that works in 5.4 using a singleton instead of share()
.
config(['mail.driver' => 'mailgun']);
config(['services.mailgun.domain' => mail_domain]);
config(['services.mailgun.secret' => mail_secret]);
$app = \App::getInstance();
$app->singleton('swift.transport', function ($app) {
return new \Illuminate\Mail\TransportManager($app);
});
$mailer = new \Swift_Mailer($app['swift.transport']->driver());
\Mail::setSwiftMailer($mailer);
\Mail::to([email protected])->send(new TrialCreated($params));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With