0

我正在使用 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);
    } } }

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

4

2 回答 2

3

在命令执行结束时添加:

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

$spool->flushQueue($transport);

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

于 2013-11-15T10:29:04.153 回答
0

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

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

# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }
于 2013-11-15T10:24:59.797 回答