在 Symfony 5.3 项目中,我使用 Mailer (..\Symfony\Component\Mailer\MailerInterface) 发送邮件。为了开发,我需要带有 composer 的“symfony/google-mailer”,并将其设置在 .env.local 中。假设用户名是“example@gmail.com”,密码是“0000”。
在 .env.local 我指定
MAILER_USER='example@gmail.com'
MAILER_PASSWORD='0000'
MAILER_DSN=gmail://MAILER_USER:MAILER_PASSWORD@default
导致错误
Failed to authenticate on SMTP server with username "MAILER_USER" using the following authenticators: "LOGIN", "PLAIN", "XOAUTH2". Authenticator "LOGIN" returned "Symfony\Component\Mailer\Exception\TransportException: Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted.
如文档中所述,我将用户名(“@”)中的一个特殊字符更改为它的 URL 编码形式,例如
MAILER_USER='example%40gmail.com'
然后导致错误
Email "example%40gmail.com" does not comply with addr-spec of RFC 2822.
(显然 URL 编码没有像预期的那样工作(因为它没有被反转?))我尝试在 services.yaml 的参数中加载环境变量并使用参数来代替 - 但这也导致了第一个错误。
如果我将身份验证信息直接写入 MAILER_DSN env var,它就可以正常工作,没有问题,比如
MAILER_DSN=gmail://example@gmail.com:0000@default
所以这似乎是一个我无法从文档中弄清楚的语法问题。有什么提示吗?