Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app

浪子不回头ぞ 提交于 2019-11-30 07:27:58

From digging some Symfony and SwiftMailer code, I can see that the memory spool is flushed on the kernel.terminate event that occurs after a response was sent. I'm not sure it works for the commands, but I may be wrong.

Try adding this code at the end of your command and see if it helps:

$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'));

Instead of memory spool use file spool. Modify your app/config/config.yml:

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: file, path: %kernel.root_dir%/spool }

Just add this to the end of your execute action:

$container = $this->getContainer();
$mailer = $container->get('mailer'); 
$spool = $mailer->getTransport()->getSpool();    
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!