Search code examples
smtpspecial-characterssymfony4credentialsmailer

How do I handle special chars in the credentials of the Symfony Mailer component in SMTP transport?


Context

In Symfony 4.3 a new emailer was introduced.

See here:

For the SMTP transport it is established that the DSN in the ENV var has this format:

MAILER_DSN=smtp://user:[email protected]

Question

How do I handle special characters in the user and password when writing the DSN?

Let's suppose that the credentials are:

username: [email protected]
password: the:password:is:difficult

Setting like this most likely will fail:

MAILER_DSN=smtp://[email protected]:the:password:is:[email protected]

How should I encode/escape things? What would be a correct DSN for it?


Solution

  • You can urlencode the values.

    There's a test in the Mailer component (Tests/Transport/DsnTest.php) indicating that it should work with encoded values.

    enter image description here

    So you can use an online encoder to convert your values (e.g.https://www.urlencoder.org/)

    Your string:

    MAILER_DSN=smtp://[email protected]:the:password:is:[email protected]
    

    Become:

    MAILER_DSN=smtp://alice%40example.com:the%3Apassword%3Ais%[email protected]