如何处理 SMTP 传输中 Symfony Mailer 组件凭据中的特殊字符?

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

上下文

在 Symfony 4.3 中引入了一个新的电子邮件程序。

看这里:

对于 SMTP 传输,已确定 ENV var 中的 DSN 具有以下格式:

MAILER_DSN=smtp://user:pass@smtp.example.com

问题

写DSN时如何处理用户名和密码中的特殊字符?

假设凭据是:

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

这样设置很可能会失败:

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

我应该怎么encode/escape东西?它的正确 DSN 是什么?

您可以对值进行 urlencode。

Mailer 组件 (Tests/Transport/DsnTest.php) 中有一个测试表明它应该使用编码值。

因此您可以使用在线编码器来转换您的值(例如https://www.urlencoder.org/

你的字符串:

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

成为:

MAILER_DSN=smtp://alice%40example.com:the%3Apassword%3Ais%3Adifficult@smtp.example.com

接受的答案是正确的。但是不需要对用户名和密码进行编码或转义。我成功地尝试了一个包含 @ 字符的用户名以及一个包含 :=|,.

的密码

所以这是一个有效的配置:

MAILER_DSN=smtp://alice@example.com:the:pa=|,ssword@smtp.example.com:587