31

I'm using mailgun to send mails thought Laravel 5.2. It configured on config/services.php like that:

    'mailgun' => [
        'domain' => env('mailgun_domain','mydomain.com'),
        'secret' => env('mailgin_secret','my-secret-key-132152345423')
    ],

But, I need change that settings in run time, before call Mail::send, to use the correct service parameters. It must be changed many times during runtime.

I cannot configure it by .env file, because all data will be get from database, where the user setups the domain and secret.

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
  • Do you know what the settings will be or are they always changing/dynamically pulled from another source? – Jared Eitnier Aug 15 '16 at 18:35
  • The code must change the "mailgun" domain and secret before send a mail. That data will be on database. I need change it many times during a execution. – Tiago Gouvêa Aug 15 '16 at 18:38
  • The solution proposed by @alexey-mezenin work well for the first change, after that, it doesn't change anymore.. just the first value remains. – Tiago Gouvêa Aug 15 '16 at 18:39
  • Yes, I understand what you need to do. It's a pretty unique scenario. Let me think about it. – Jared Eitnier Aug 15 '16 at 18:44
  • When sending mails on behalf of your user, you should never user their credentials as global settings!!! What if the app dispatches other mails during the same request, e.g. notify the admin about an event… @JaredEitnier s answer is the way to go. – jsphpl Aug 21 '16 at 22:54

3 Answers3

59

You can set config values dynamically at runtime with config() helper:

config(['services.mailgun' => $arrayWithNewSettings]);
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 2
    Thanks @alexey-mezenin, when I set it in runtime at first time, it work well. But when I change it, if I got the config value, it's ok, but when Mail use it, just the first value are there.. just the first config stay there. I need force service to reload config... – Tiago Gouvêa Aug 10 '16 at 19:24
  • 1
    @astroanu before downvoting you could actually read the documentation https://laravel.com/docs/5.3/configuration#accessing-configuration-values – Alexey Mezenin Oct 30 '16 at 17:39
  • The question asks about "changing" config value at run time, not substituting if not exists. To set a config value use Config::set($key, $value). your solution is the equalant of Config::get('services.mailgun', $arrayWithNewSettings); – astroanu Oct 31 '16 at 09:32
  • 5
    @astroanu, `Config::set($key, $value)` is the same as `config(['services.mailgun' => $arrayWithNewSettings]);`. To get the value you're using `config('services.mailgun');`. Read the documentation again. – Alexey Mezenin Oct 31 '16 at 11:30
  • 2
    i guess it does then. didn't see. – astroanu Oct 31 '16 at 12:27
1

you can check my answer here it's working on Laravel 8. You need to change some piece of code as I mentioned below.

config(
    [
        'mail.driver' => 'mailgun',
        'services.mailgun.domain' => 'your domain.com',
        'services.mailgun.secret' => 'your mail gun secrets'
    ]
);
(new Illuminate\Mail\MailServiceProvider(app()))->register();
Raj Omer Mustafa
  • 81
  • 1
  • 1
  • 5
  • 1
    `config()` params are `array|null|string $key mixed` Not needs 3 instances of `config()` – abkrim Mar 21 '23 at 09:01
0

I think if you follow this example you'll be on the right track.

\Illuminate\Mail\TransportManager.php has a method createMailgunDriver() which is pulling services from a hard location that is not changeable by default.

You'll need to write your own service provider, extend the MailServiceProvider and roll your own transport so that you can pull your settings from the db like you want to.

Community
  • 1
  • 1
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123