Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

altea-software's avatar

data cache in horizon

Hi all, I have a horizon worker that execute a notification job. I need to make the mailer params dynamically loaded from my data. I tried to

config()->set(['mail.mailers.smtp.host' => ...])

but If the horizon worker is running, it does not load the changes.

I tried setting the Symfony transport:

$factory = new EsmtpTransportFactory;
        $transport = $factory->create(new Dsn($params['scheme'], $params['host'], $params['username'], $params['password'], $params['port']));
        app('mailer')->setSymfonyTransport($transport);

But got no better results. I'm sure I intercept when my job needs to send with different params, but could not get it work unless I stop and restart the horizon worker.

Any idea?

Thanks in advance.

0 likes
4 replies
LaryAI's avatar
Level 58

To dynamically change the mailer configuration for a job running in Laravel Horizon, you need to ensure that the changes are applied at runtime, as the configuration is typically cached when the worker starts. Here's a solution that should work:

  1. Use a Custom Mailer Configuration: Instead of trying to modify the global mail configuration, create a custom mailer configuration within your job. This way, you can set the parameters dynamically each time the job runs.

  2. Set Up a Custom Transport: You can create a custom transport for the mailer within your job. This involves creating a new instance of the mailer with the desired configuration.

Here's how you can implement this:

Key Points:

  • Custom Transport: By creating a custom transport, you ensure that each job execution uses the latest configuration.
  • Mailer Instance: You create a new mailer instance with the custom transport, which allows you to send emails with the updated configuration.
  • Avoid Global Config Changes: This approach avoids changing the global configuration, which is beneficial in a multi-worker environment like Horizon.

This solution should allow you to dynamically change the mailer configuration without needing to restart your Horizon workers.

altea-software's avatar

Just in case, the solution posted by the AI does not work.

altea-software's avatar

Solved:

  1. create a job that sends the notification.
  2. In the notification class, purge the Mail before reinstantiating.
Mail::forgetMailers();
Mail::mailer('smtp');

PS: Making the second step without the first, in some way, does not solve the problem.

Please or to participate in this conversation.