【问题标题】:How to configure MAILER_URL in .env file of Symfony 4 to send e-mails via sendmail with Swift_Mailer?如何在 Symfony 4 的 .env 文件中配置 MAILER_URL 以使用 Swift_Mailer 通过 sendmail 发送电子邮件?
【发布时间】:2018-07-23 16:25:26
【问题描述】:

我正在开发一个 Symfony 4 应用程序,使用 Swift_Mailer 发送电子邮件。 由于在我的情况下不可能使用 SMTP(不要问为什么……)我必须使用类似 sendmail 的东西。

默认情况下,Swift Mailer 的配置是在 Symfony 的 .env 文件中以 URL 表示法在名为 MAILER_URL 的选项中完成的。默认值为“null://localhost”,根本不发送邮件。

我所能找到的所有值都是 Gmail 或一般 SMTP 的示例,如 Symfony docs 以及 Composer 生成的示例 .env 中所述。

.env 的默认内容:

# …

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

# …

我不知道什么,因此我的问题是:

我需要做什么才能让sendmail 使用它?

我已经尝试过类似的方法:

MAILER_URL=sendmail://localhost

MAILER_URL=sendmail://my-domain.com

但到目前为止没有成功。

使用控制台命令时

bin/console swiftmailer:email:send

我什至收到“[OK] 1 封电子邮件已成功发送”。结果,但实际上没有发送电子邮件。
(……顺便说一句,没有假脱机。bin/console swiftmailer:spool:send 返回“发送了 0 封电子邮件”。) 邮件传递似乎被中断了,因为邮件不会到达我的邮件帐户,而且我的垃圾邮件是空的。

另一方面,直接调用sendmail 命令确实有效。 (虽然我的测试邮件到达了我的垃圾邮件,但仍然:邮件已发送。)

同样,我必须如何在 Symfony 4 的 .env 中为 Swift_Mailer 配置 MAILER_URL 才能使用 sendmail

有可能吗?

【问题讨论】:

  • 我在 symfony 2 上找到了this。尝试适应 .env 变量!

标签: php email sendmail swiftmailer symfony4


【解决方案1】:

好的,符号已经是正确的。所以这个对使用sendmail有效:

MAILER_URL=sendmail://my-domain.com

我的实际问题是邮件假脱机处于活动状态。我从 swiftmailer.yaml 配置中注释掉了 spool 条目。

顺便说一下,查看 Symfony Profiler 对我帮助很大。

…我的邮件仍然没有到达,但我确信这与MAILER_URL 无关。所以,我的问题得到了解答。

【讨论】:

  • 一旦我编辑“switftmailer.yaml”,所有带有联系表格的页面都会变成空白。有人能详细说明怎么做吗?
  • @kghbln 也许您应该为此提出新问题。对我来说,这听起来可能是……嗯……任何东西。例如,无效的 YAML 语法。或任何其他类型的错误配置。然而,空白页通常只是 500 个内部服务器错误。这些通常是记录的。 → 请注意您的错误日志。我希望,这会有所帮助。
  • 感谢您的回答。是的,我应该问一个新问题。我花了很多时间尝试将 Symfony 与 Postfix 集成。服务器日志是令人恼火的空,我刚刚从 yaml 文件中删除了该行,这是其中的最后一行。我有点绝望,因为我相信这可能是一个最多一个小时的任务,到目前为止我花了很多时间:(现在已经有足够的感叹了。:)
【解决方案2】:

试试这个配置:

swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
transport: 'sendmail'
command: '/usr/sbin/sendmail -oi -t'

文件路径:

/my_sf4_project/config/packages/swiftmailer.yaml

我正在使用 Symfony 4.3.1 和 SwiftMailer 6.2.0。您可以在 SwiftMailer 捆绑文档中阅读:

/**
 * Start the standalone SMTP session if running in -bs mode.
 */

然后:

/**
 * Set the command to invoke.
 *
 * If using -t mode you are strongly advised to include -oi or -i in the flags.
 * For example: /usr/sbin/sendmail -oi -t
 * Swift will append a -f<sender> flag if one is not present.
 *
 * The recommended mode is "-bs" since it is interactive and failure notifications
 * are hence possible.
 *
 * @param string $command
 *
 * @return $this
 */

带有这些注释的文件路径:

供应商/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php

在更改为 '/usr/sbin/sendmail -oi -t' 之前,我收到了来自 SwiftMailer 的错误:

app.ERROR:刷新电子邮件队列时发生异常:预期响应代码为 220,但响应为空 [] []

更改 sendmail 参数后,我现在可以成功发送邮件了。

【讨论】:

  • 当然我的 .env 配置是:MAILER_URL=sendmail://my-domain.com
【解决方案3】:

在 yaml 中评论 spool 为我解决了问题

【讨论】:

  • 一旦我编辑“switftmailer.yaml”,所有带有联系表格的页面都会变成空白。有人能详细说明怎么做吗?
【解决方案4】:

不确定我是否正确解决了您的问题,但就我而言,我使用的是 Gmail 帐户,这是我的 .env 文件的第 23 行

MAILER_URL=gmail://myadress@gmail.com:myemailpassword@localhost

您是否已经通过composer require symfony/swiftmailer-bundle 安装了 SwiftMailer? (对不起我的英语)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2016-09-22
    • 2016-02-21
    • 1970-01-01
    • 2013-12-22
    • 2019-03-01
    • 2018-07-05
    相关资源
    最近更新 更多