【问题标题】:Symfony : Testing email sending in commandSymfony:在命令中测试电子邮件发送
【发布时间】:2016-10-09 18:44:07
【问题描述】:

早上好。

我想测试在我的 Symfony 命令中发送电子邮件。

我的电子邮件是通过\Swift_Mailer 发送的

$this->mailer->send($message);

我尝试使用这个:

http://symfony.com/doc/current/cookbook/email/testing.html

但是 $this->client->getProfile()Symfony Response 在 Symfony 命令中不可用。

Argument 1 passed to Symfony\Component\HttpKernel\Profiler\Profiler::loadProfileFromResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given,

现在我如何验证我的电子邮件是否正确发送?

【问题讨论】:

  • 请提供一些您目前拥有的代码示例...
  • if ($this->mailer->send($message))

标签: php symfony email command


【解决方案1】:

在测试命令时,您应该有 $kernel 变量。从那里你得到$kernel->getContainer() 的容器。使用容器,您可以访问消息记录器:$container->get('swiftmailer.mailer.default.plugin.messagelogger')。那是。现在你有一个Swift_Plugins_MessageLogger 的实例,它有一个#getMessages,它返回发送的消息。

【讨论】:

  • 注意:此解决方案要求您在配置中设置swiftmailer.logging: true
【解决方案2】:

您可以将 swiftmailer 的 spool 类型放入文件中,然后对该路径中的文件进行计数。

在 symfony4 中可能是:

测试/swiftmailer.yaml

 swiftmailer:
    spool:
        type: file
        path: 'path/to/emailTestFolder'

测试文件

$mails = new \FilesystemIterator('path/to/emailTestFolder', \FilesystemIterator::SKIP_DOTS);
$this->assertSame(1, iterator_count($mails));

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 2012-01-29
    • 2019-04-09
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2015-04-16
    • 2023-03-10
    相关资源
    最近更新 更多