Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Mailer Dynamic Transport from Database

I would like to specify the SMTP server settings (host, port, password, etc.) through an interface and stored them in a database table so that the user can change this without developer intervention.

How can I accomplish this with the mailer component?

like image 982
boonkerz Avatar asked Nov 04 '25 18:11

boonkerz


1 Answers

Two scenarios:

First: You have multiple configurations on database and you want to choose how to send email from a list of providers

You can implement a transport (Symfony\Component\Mailer\Transport\TransportInterface) which retrieves the correct SMTP settings from db, passing the provider name or id in a message header (remember to delete the header before sending).

Or you can use Mailer as a standalone component, instead of relaying on the one integrated (autoconfigurated and autowired) in the framework. In this case you have to instantiate a new Symfony\Component\Mailer\Mailer class with the appropriate transport when you want to send the mail.

Second: You have only one provider on database and you just want to be able to change SMTP settings at runtime

You can relay on framework integration, implementing your own transport which retrieves the settings from db and create a delegated transport to effectively send the messages.

like image 155
Alessandro Chitolina Avatar answered Nov 07 '25 16:11

Alessandro Chitolina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!