【问题标题】:symfony2 email not sent in commandsymfony2 电子邮件未在命令中发送
【发布时间】:2013-11-28 16:31:24
【问题描述】:

我正在使用 symfony2 开发一个应用程序 在应用程序的一侧,我正在发送电子邮件,一切正常。 但现在我创建了一个在 crontab 中运行的命令,但这不发送电子邮件。 这是我的命令: 使用 Doctrine\ORM\EntityManager; 使用 Symfony\Component\Templating\EngineInterface;

类发件人{ 受保护的$em;受保护的$twig;受保护的$mailer; 公共函数 __construct($em, \Twig_Environment $twig, \Swift_Mailer $mailer) { $this->em = $em; $this->twig = $twig; $this->mailer = $mailer; }

public function runSender() {
    $proj = $this->em->createQuery ...
    $maillist = $this->em->createQuery ...
$templateFile = "projectprojBundle:MailList:emailNew.html.twig";
$templateContent = $this->twig->loadTemplate($templateFile);
$body = $templateContent->render(array('proj' => $proj));

    foreach ($maillist as $m) {
    $message = \Swift_Message::newInstance()->setSubject('New projects')
    ->setFrom('...')->setTo($m['email'])
    ->setContentType('text/html')
    ->setBody(trim($body));
    $this->mailer->send($message);
    } } }

查询一切正常,我测试过。 如果我可以从其他班级发送,为什么我不能在这里?

【问题讨论】:

  • 您是否在使用假脱机内存?如果是,这就是问题所在。见stackoverflow.com/questions/13122096/…
  • 是的,我正在使用假脱机内存。它适用于其他类。但在这里它不起作用,我不知道为什么
  • 您能否澄清一下它适用于 cli 中的其他类或当您在 web 中工作时。这是这里的一个要点。检查我发布的链接。
  • 当我在网络上工作时
  • 当我将 spool 更改为文件时,我无法发送电子邮件,即使是来自网络

标签: symfony mailer


【解决方案1】:

可能是您在 config.yml 中的假脱机设置

使用spool: { type: memory } 即时发送电子邮件

# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }

【讨论】:

  • im 使用 spool 类型内存
【解决方案2】:

在你的命令执行结束时添加:

$spool = $this->mailer->getTransport()->getSpool();
$transport = $this->getContainer()->get('swiftmailer.transport.real');

$spool->flushQueue($transport);

您必须扩展 ContainerAwareCommand 类才能在您的命令中访问服务容器。

【讨论】:

  • 我得到了这个错误:调用非对象上的成员函数 getKernel()
  • @BrunoRamalho 确保不要在 configure 方法中调用这些方法。所有代码都必须从execute 方法或此方法的子方法之一调用。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 2016-09-02
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多