【问题标题】:Symfony2 + swift mailer + command : email body displayed into the consoleSymfony2 + swift mailer + 命令:电子邮件正文显示在控制台中
【发布时间】:2012-07-09 10:56:02
【问题描述】:

我在我的 Symfony 应用程序中创建了一个新命令

在此命令中,我调用了一个服务,该服务发送电子邮件 作为 FOS 包中的邮件程序

我的问题是 电子邮件正文显示到控制台以发送电子邮件。

我想说msg mailer 方法命令之外可以正常工作。

这是我的命令

class ReminderCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->container = $this->getApplication()->getKernel()->getContainer();            

        # the mailer service works fine out the command
        $mailer = $this->container->get('mailer');
        $mailer->sendMsg(...);          


        $text = ' => mail sent';

        $output->writeln($text);
    }
}

请帮忙

谢谢

山姆

【问题讨论】:

    标签: symfony swiftmailer


    【解决方案1】:

    我认为您可能使用了内存假脱机。

    当使用内存假脱机时,你必须知道,由于 symfony 处理控制台命令的方式,电子邮件不会自动发送。您必须自己处理刷新队列。使用以下代码在控制台命令中发送电子邮件:

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

    参考: http://symfony.com/doc/2.0/cookbook/console/sending_emails.html

    【讨论】:

    • 是的,就是这样!如果您通过命令行发送具有相同配置的电子邮件,则只能手动刷新电子邮件队列。谢谢你的信息! +1
    【解决方案2】:

    我找到了解决办法。

    以这种方式使用服务:

        $this->container->get('mailer')->sendMsg(...)
    

    不是

        # the mailer service works fine out the command
        $mailer = $this->container->get('mailer');
        $mailer->sendMsg(...);
    

    现在可以了

    玩得开心,希望它可以帮助一些人

    山姆

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-13
      • 2014-02-23
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多