Back

Mailgun, sending from multiple domains.


I'm running a central system with multiple clients/users, each of whom have their own mailgun account/domain/api key.

What is the simplest way to programmatically send mail using each client's mailgun configuration?

I see that Laravel integrates with Mailgun, but it seems that by default you can only send email from the app using global mailgun settings. I'd like to send mail using mailgun configuration stored for each of my users.

Any suggestions?

elite123 replied 2 years ago

Have you tried overriding the config settings at runtime (untested):

\Config::set('services.mailgun.domain', 'your-mailgun-domain');
\Config::set('services.mailgun.secret', 'your-mailgun-key');

http://laravel.com/docs/5.0/configuration

deadlysyntax replied 2 years ago

Will try, thanks.

jarnheimer replied 1 week ago

elite123 said:

Have you tried overriding the config settings at runtime (untested):

\Config::set('services.mailgun.domain', 'your-mailgun-domain'); \Config::set('services.mailgun.secret', 'your-mailgun-key');

http://laravel.com/docs/5.0/configuration

This solution does not work (Laravel 5.4). I found out the hard way.

stayallive replied 1 week ago

The above will only work if you update the configuration before the Mailgun driver is initialized.

Changing it after the driver has loaded it will still send it using the credentials that were in the config at the time the driver loaded.

Randy Mcnab replied 1 week ago

You have to destroy the mailer instance and recreate it. I do this by comparing the email address we send from and if that isn't loaded, then I reload the configuration

if (Config::get('mail.driver') == 'mailgun') {

		$domain = Functions::getDomainFromEmailAddress($from);

		if (Config::get('services.mailgun.domain') != $domain) {
			App::forgetInstance('mailer');
			Mail::clearResolvedInstance('mailer');
			Config::set('services.mailgun.domain', $domain);
		}
	}

Sign in to participate in this thread!


We'd like to thank these amazing companies for supporting us