How to configure symfony mailer when every sender require authentication before sending message

420 Views Asked by At

In my app i have many sender and the smtp server require authentication for every sender before sending message.

Note: my senders are stored in database with their password

How i can implement this in symfony mailer to retrieve password and authenticate before send message ?

Thank you

Is this feature is implemented in symfony mailer?

1

There are 1 best solutions below

0
Lou On

It is possible to achieve that. You will need to create a service where you get the password of the user, you will have to use depency injectio (Entity manager) and call for this password.

Then, you need to configure the mailer properly to set your user infos, it is located in config/packages/mailer.yaml.

framework:
    mailer:
        dsn: 'smtp://localhost'
        # Default options for all senders are here

        # User specific options
        senders:
            # User 1
            user1:
                transport: 'smtp'
                host: 'your-smtp-server'
                username: '[email protected]'
                password: '%env(USER1_PASSWORD)%'
                # Other options you might need

finally, you need what is called a 'Factory' (here the doc about using a factory to create services : https://symfony.com/doc/current/service_container/factories.html).

It will be a function where you use the MailerInterface to get the password of your user and set it in the transport option.

Don't forget to register your mailer factory service in your config/service.yaml file.