【问题标题】:Symfony 3 and Mailhog: mails not being cached by mailhogSymfony 3 和 Mailhog:邮件没有被 mailhog 缓存
【发布时间】:2017-08-07 23:47:53
【问题描述】:

在我的开发环境中,我想使用 mailhog 来捕获电子邮件。我已经安装并配置了我的 php.ini 来代替 sendmail 属性。 如果在命令行中我运行这个

php -r "mail(......);" 

邮件被mailhog捕获。问题在于 Symfony 和 Swiftmailer。为了进行测试,我创建了一个非常简单的控制器:

/**
     * @return Response
     */
    public function homeAction() : Response
    {
        mail('some@mail.com', 'tasest', 'aaaa');
        $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('send@example.com')
            ->setTo('recipient@example.com')
            ->setBody(
                'aaaaa',
                'text/html'
            );
        $this->get('mailer')->send($message);
        return $this->render('::base.html.twig');
    }

现在,mail 函数发送的电子邮件被 mailhog 捕获。但不是 SwiftMailer 发送的邮件。

在我的 config_dev 我有这个:

    # Swiftmailer Configuration
swiftmailer:
    transport: "sendmail"

我认为应该足够了。

我错过了什么吗?

P.S.:如果我使用真实地址(而不是收件人@example.com),则会发送和接收电子邮件

更新: 我也试过为smtp配置mailhog,parameters.yml:

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_port: 1025
mailer_user: null
mailer_password: null

config.yml:

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    port:      "%mailer_port%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

但结果是一样的。

【问题讨论】:

  • 问题解决了,缓存有问题
  • 你能解释一下缓存的问题吗?我面临同样的问题,刷新缓存似乎无法解决我的问题。
  • 我删除:spool: { type: memory } 并且它有效

标签: php symfony email development-environment


【解决方案1】:

Symfony 4 也有同样的问题。 如果你删除config/packages/swiftmailer.yaml中的变量url: '%env(MAILER_URL)%',它应该可以工作。

【讨论】:

  • 这在我的情况下不起作用。我需要保留 env MAILER_URL 并将其添加到 .env 文件中。 MAILER_URL=smtp://127.0.0.1:1025
【解决方案2】:

Symfony 中的 Swiftmailer 设置通常会有更完整的配置,尤其是在 config_dev.yml 中,以明确您将其发送到哪里。

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    port:      "%mailer_port%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"

这里,如果Mailhog在1025端口(非常典型),那么开发环境中的parameters.yml会这样设置,填写.yml文件中的参数:

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_port: 1025
mailer_user: null
mailer_password: null

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2022-11-25
    • 2022-09-26
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2022-06-22
    • 2022-10-25
    • 1970-01-01
    相关资源
    最近更新 更多