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

Thyrosis's avatar

Sending mail from queue with custom mailer config

Hi everyone!

TL;DR: how do I get a queue worker to pull settings from database and using those settings as mail config to send out a queued mail?

--

I'm working on a multi tenant application and I've included a way to send out emails using a tenants custom mail config.

Nothing too spiffy, just a settings table in the database, a tenant can set the mailhost, username, password and then before an email is sent out, I pull in the correct settings to the config.

                config($model->account->getMailConfig());
                Mail::to($model->account->getSetting('adminEmailAddress'))->send(new NewCommentNotificationToAdmin($model, $model->post));

Now, this works perfectly. However, I wanted to change the "send" to "queue". No biggy, right? So why do all jobs suddenly fail?

It makes sense, because the queue worker is an application wide worker and just uses the .env settings. So storing a mail in a job and having it processed by the queue, stops me from using the custom mail config.

I know I can call Mail::mailer('differentMailer')->queue(), but that only works if the differentMail SMTP settings are set up in the mailers array in config/mail.php - which they're not. They're stored in a database.

So, the question is: how do I get the queue worker smart enough to pull settings from database and using those settings as mail config to send out a queued mail? Or, if needs be, how can I store the settings in the job itself, so that the worker can use them?

Thanks for any smart or creative ideas!

0 likes
3 replies
Thyrosis's avatar
Thyrosis
OP
Best Answer
Level 17

Right, finally figured this out.

The AI's answer (now hidden) pointed me to a custom driver, which set me on a very difficult path to no success. Which is probably due to my lack of knowledge, rather than the answer...

In the end, I found this thread which provided me with the solution.

Long story short: just pass the config into the mailable itself so it gets stored in the job. Set the Mail::SymfonyTransport inside the Mailable's build function, which is run during job-execution.

//Update Later on I realised that the Mailable's build method is called at time of sending. So instead of having to pass the config in the mailable, I now pull it live in the build method.

// Sending the notification malil to the queue
Mail::to($model->account->getSetting('adminEmailAddress'))->queue(new NewCommentNotificationToAdmin($model));

// The build method in the mailable getting the config before sending
    public function build()
    {
        // Build a custom transport based on the sender's account config
        Mail::setSymfonyTransport(Mail::createSymfonyTransport($this->comment->account->getMailConfig()));

        return $this->markdown('emails.comments.notificationToAdmin')->subject('New comment on '.$this->post->title);
    }
1 like
BilalTariq01's avatar

you can dynamically set SMTP details in config before sending mail

	config([
                'mail.mailers.smtp.host' => $whitelabel['smtp_host'],

                'mail.mailers.smtp.port' => $whitelabel['smtp_port'],

                'mail.mailers.smtp.encryption' => $whitelabel['smtp_encryption'],

                'mail.mailers.smtp.username' => $whitelabel['smtp_username'],

                'mail.mailers.smtp.password' => $whitelabel['smtp_password'],

                'mail.from.address' => $whitelabel['from_email'],

                'mail.from.name' => $whitelabel['sender_name'],

                'mail.mailers.smtp.timeout' => 10, // Timeout in seconds

                'mail.default' => 'smtp', // Make sure to set the default mailer to smtp
            ]);

Please or to participate in this conversation.