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('[email protected]')->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.

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']) Commented Apr 7, 2023 at 6:58
  • 1
    I believe this is what you are looking for stackoverflow.com/questions/45146260/… Commented Apr 7, 2023 at 6: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 Commented Apr 7, 2023 at 7: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 Commented Apr 7, 2023 at 7:55

2 Answers 2

0

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

the reference discussion is from this Laracast

Sign up to request clarification or add additional context in comments.

2 Comments

Option with swift mailer seems interesting, but swift mailer is not supported in Laravel 9 :(
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.
0

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('[email protected]')->subject($request->subject)->html('email body');

            $mailer->send($mailable);
        } 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.