34

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.

5
  • Do you know what the settings will be or are they always changing/dynamically pulled from another source? Commented Aug 15, 2016 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. Commented Aug 15, 2016 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. Commented Aug 15, 2016 at 18:39
  • Yes, I understand what you need to do. It's a pretty unique scenario. Let me think about it. Commented Aug 15, 2016 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
    Commented Aug 21, 2016 at 22:54

3 Answers 3

63

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

config(['services.mailgun' => $arrayWithNewSettings]);
5
  • 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... Commented Aug 10, 2016 at 19:24
  • 1
    @astroanu before downvoting you could actually read the documentation laravel.com/docs/5.3/… Commented Oct 30, 2016 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
    Commented Oct 31, 2016 at 9: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. Commented Oct 31, 2016 at 11:30
  • 2
    i guess it does then. didn't see.
    – astroanu
    Commented Oct 31, 2016 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();
1
  • 1
    config() params are array|null|string $key mixed Not needs 3 instances of config()
    – abkrim
    Commented Mar 21, 2023 at 9:01
0
+50

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.

4
  • Checking now.. I'm not right if it will work and how to implement it.. :( Commented Aug 15, 2016 at 20:39
  • @TiagoGouvêa what issue you are facing in implementing the above solution ?
    – jaysingkar
    Commented Aug 19, 2016 at 22:52
  • @TiagoGouvêa I checked the answer, what you can do is check the answer , try to implement it by creating each file and let me know the step u r stuck in
    – jaysingkar
    Commented Aug 21, 2016 at 15:50
  • I decided to take another solution.... "less glamorous" but efficient, without use the services. Thanks for all replies! Commented Aug 23, 2016 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.