Xavi Montero
Xavi Montero

Reputation: 10724

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?

Upvotes: 14

Views: 8055

Answers (2)

Michael Käfer
Michael Käfer

Reputation: 1796

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

Upvotes: 2

Filipe Silva
Filipe Silva

Reputation: 156

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]

Upvotes: 14

Related Questions