2

I need to change Laravel Mail Configuration during the runtime. This is because the configuration needs to be changed several times during runtime, not just once on app initialization.

foreach ($emails as $email) {
    Config::set('mail.mailers.smtp.username', $email->email);
    Config::set('mail.mailers.smtp.password', $email->password);
    Config::set('mail.from.address', $email->email);

    Mail::to('someone@example.com')->send(new DemoMail('title', 'body'));
}

As you can see, I am trying to set new configurations on each iteration, but the configuration is only set once (on the first iteration); all other emails are sent using the design from the first iteration.

I have found solutions using Service Providers. Similar to this one Dynamic mail configuration with values from database [Laravel] The problem is that the provider sets mail configuration when the app is initialized and can't be changed during runtime. So this doesn't solve my problem.

The second idea I came up with is to define multiple mail drivers (configurations), one for each email, and then use the mailer() function to specify which one I want to use on each iteration. The problem with this solution is that I need to create those drivers dynamically; I can't hardcode them.

I searched the internet for the right solution, but couldn't find one.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
itstare
  • 31
  • 4
  • Laravel documentation points that you can set configuration in runtime using config helper if you pass an array key value: `config(['your.config' => 'value'])` – Mahdi Rashidi Apr 07 '23 at 06:58
  • 1
    I believe this is what you are looking for https://stackoverflow.com/questions/45146260/dynamic-mail-configuration-with-values-from-database-laravel – SHEN Apr 07 '23 at 06:59
  • @MahdiRashidi Unfortunately, it doesn't. I just tried setting it like this config([ 'mail.mailers.smtp.username' => $email->email, 'mail.mailers.smtp.password' => $email->password, 'mail.from.address' => $email->email ]); But it only sets it once – itstare Apr 07 '23 at 07:18
  • @SHEN I tried something simillar to this, but wouldn't this approach just set configuration when app is initialized. I need to reset configuration each time loop runs – itstare Apr 07 '23 at 07:55

2 Answers2

1

I have found a solution by using Symfony Mailer in Laravel.

foreach ($emails as $email) {
            $transport = new EsmtpTransport('mail.host', 465);
            $transport->setUsername($email->email);
            $transport->setPassword($email->password);

            $mailer = new Mailer($transport);

            $mailable = (new SymfonyEmail())->from($email->email)->bcc('test@test.com')->subject($request->subject)->html('email body');

            $mailer->send($mailable);
        } 
itstare
  • 31
  • 4
0

@itstare maybe this Laravel News can help you to find the way to work with.

the reference discussion is from this Laracast

SHEN
  • 3
  • 1
  • 1
    Option with swift mailer seems interesting, but swift mailer is not supported in Laravel 9 :( – itstare Apr 07 '23 at 09:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 06:16