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

Xav*_*ero 7 smtp credentials special-characters mailer symfony4

语境

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

看这里:

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

MAILER_DSN=smtp://user:pass@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

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

让我们假设凭据是:

username: alice@example.com
password: the:password:is:difficult
Run Code Online (Sandbox Code Playgroud)

像这样的设置很可能会失败:

MAILER_DSN=smtp://alice@example.com:the:password:is:difficult@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

我应该如何编码/转义东西?什么是正确的 DSN?

小智 7

您可以对值进行 urlencode。

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

在此处输入图片说明

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

你的字符串:

MAILER_DSN=smtp://alice@example.com:the:password:is:difficult@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

变得:

MAILER_DSN=smtp://alice%40example.com:the%3Apassword%3Ais%3Adifficult@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

  • 您绝对不应该将密码粘贴到在线工具中。 (2认同)