12

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:pass@smtp.example.com

Question

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

Let's suppose that the credentials are:

username: alice@example.com
password: the:password:is:difficult

Setting like this most likely will fail:

MAILER_DSN=smtp://alice@example.com:the:password:is:difficult@smtp.example.com

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

Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
  • Did you found a solution yet? – Abenil Jan 16 '20 at 03:12
  • 1
    At least the @ within the username is no problem, works in my case without a hassle. – spackmat Mar 11 '20 at 08:39
  • @Abenil actually this `MAILER_DSN=smtp://some.name@gmail.com:mypassword@smtp.gmail.com` has worked for me for a gmail address, but I don't have responded myself here because I don't have a "generic clue for any character". In particular I have not tested the `:` in the password. Just for the "doble @" I can confirm it worked. But, hey! I did not find any official documentation. I just found it working by trial and error. So... I am ready to find it failing at any random time without prior notice. – Xavi Montero Mar 15 '20 at 23:50

2 Answers2

11

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://alice@example.com:the:password:is:difficult@smtp.example.com

Become:

MAILER_DSN=smtp://alice%40example.com:the%3Apassword%3Ais%3Adifficult@smtp.example.com
Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
Filipe Silva
  • 126
  • 2
  • 3
2

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://alice@example.com:the:pa=|,ssword@smtp.example.com:587
Michael Käfer
  • 1,597
  • 2
  • 19
  • 37
  • I agree that the "non-encoded" version works. But... from my humble point of view, as a human-reader of the code, you could have doubts about the precedence; but if you do the encoding there's no doubt about it. I'm a deep defender of that "it's easy to make code readable by the machines, what is difficult is to make it readable by humans". I prefer the encoding because eliminates ambiguation without needing to know the low-level details about "what symbol would have precedence". The "don't make me think" principle applied to human-readers of your code. Clean code rules. – Xavi Montero Apr 06 '20 at 23:29
  • 1
    I agree with you - partly. Still my answer hopefully will help somone in the much more painful phase of trubleshooting, during finding out why the SMTP connection does not work. The encoding is not the cause of errors. – Michael Käfer Apr 07 '20 at 15:28
  • it can work maybe, but it wont work eventually. try `MAILER_DSN=smtp://alice@example.com:the:pa####=|,ssword@smtp.example.com:587` further more, user and pass WILL be url decoded. so you WILL have issues when you happen to not encode something that is supposed to be encoded say `%` etc. Just baaaad answer. See Dsn::fromString – Toskan Jul 07 '23 at 11:30
  • @Toskan You are wrong. I wrote you can use `@` and `:` and `=` and `|` and `,`. But I did not write you can use `#` and `%`. – Michael Käfer Jul 07 '23 at 13:47