php – 无法从自定义Symfony2命令发送电子邮件,但可以从应用程序的其他地方

我写了一个自定义控制台命令来查询我的数据库,生成一个报告并将其发送到一个地址;但是我似乎无法成功发送电子邮件.我可以在我的应用程序其他地方的正常控制器中发送电子邮件,如果我手动创建和配置 Swift_Mailer实例,而不是通过容器获取,我也可以从我的控制台命令中发送电子邮件.

这是我的控制台命令的简化版本:

<?PHP

namespace Foo\ReportBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExpiryCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('report:expiry')
            ->setDescription('Compile and send e-mail listing imminent expiries');
    }

    protected function execute(InputInterface $input,OutputInterface $output)
    {
        /* ... */
        $message = \Swift_Message::newInstance()
            ->setSubject('Expiry report')
            ->setFrom('DoNotReply@domain.com')
            ->setTo('recipient@domain.com')
            ->setBody($body);

        $mailer = $this->getContainer()->get('mailer');

/*      This works...
        $transport = \Swift_SmtpTransport::newInstance('smtp.domain.com',25)
            ->setUsername('username')
            ->setPassword('password');
        $mailer = \Swift_Mailer::newInstance($transport);
*/
        $result = $mailer->send($message);
        $output->writeln($result);
    }
}

Swiftmailer配置为通过我的app / config / config.yml文件中的SMTP发送(delivery_address:dev@domain.com也在app / config / config_dev.yml中设置):

swiftmailer:
    transport: smtp
    host: smtp.domain.com
    username: username
    password: password
    spool:
        type: memory

当运行命令时,它将打印1到命令行,我认为这意味着它是成功的.但是我同时监视我的邮件服务器的日志,甚至没有连接.

要确认我的配置正在加载到邮件程序中,我将内存更改为文件,并且在运行命令时将邮件正在假脱机到文件系统,我可以使用PHP应用程序/控制台swiftmailer在命令行上成功刷新假脱机:阀芯:发送.

有没有人有任何想法,这里发生了什么,或任何建议,我如何可以进一步调试?我的app / logs / dev.log文件中没有任何内容.我正在使用Symfony 2.1.3-DEV.

从挖掘一些Symfony和SwiftMailer代码,我可以看到在发送响应后发生的kernel.terminate事件上刷新内存假脱机.我不知道它是否适用于命令,但是我可能错了.

尝试在命令结尾添加代码,看看它是否有帮助:

$transport = $this->container->get('mailer')->getTransport();
if (!$transport instanceof \Swift_Transport_SpoolTransport) {
    return;
}

$spool = $transport->getSpool();
if (!$spool instanceof \Swift_MemorySpool) {
    return;
}

$spool->flushQueue($this->container->get('swiftmailer.transport.real'));

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...