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

zhaogood's avatar

How to set Symfony's Mailer SMTP config dynamically?

Hi guys, After I upgraded Laravel to 9, I don't know how to make our mail function work again. We let users fill in their email server SMTP details(host, port, encryption, username, and password) and we store them in the database. When a user sends an email from our system, it uses the user's SMTP details and it works for SwiftMailer. After the Laravel 9 upgrade, I don't know how to do it with Symfony's Mailer. Can anyone please help? Thank you.

0 likes
5 replies
DivDax's avatar

Same here. Did you found a solutions?

zhaogood's avatar

@DivDax yes. Before you send the mail, just reset the symforny transport:

    $factory = new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory();

    $transport = $factory->create(new \Symfony\Component\Mailer\Transport\Dsn(
        $scheme, $host, $user, $password, $port, $options
    ));
    app('mailer')->setSymfonyTransport($transport);
3 likes

Please or to participate in this conversation.