Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • https://symfony.com/blog/new-in-symfony-4-3-mailer-component
  • https://symfony.com/doc/4.4/mailer.html.

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?

like image 363
Xavi Montero Avatar asked Dec 06 '19 14:12

Xavi Montero


2 Answers

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]
like image 143
Filipe Silva Avatar answered Oct 10 '22 15:10

Filipe Silva


The accepted answer is correct. But there is no need to encode or escape neither the username nor the password. I successfully tried a username containing a @ character as well as a password containing : and = and | and ,.

So this is a valid config:

MAILER_DSN=smtp://[email protected]:the:pa=|,[email protected]:587
like image 34
Michael Käfer Avatar answered Oct 10 '22 17:10

Michael Käfer