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

前端 未结 3 603
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 10:45

I have written a custom console command to query my database, produce a report and e-mail it to an address; however I can not seem to successfully send the e-mail. I can sen

相关标签:
3条回答
  • 2020-12-30 11:20

    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);
    
    0 讨论(0)
  • 2020-12-30 11:35

    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 }
    
    0 讨论(0)
  • 2020-12-30 11:37

    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'));
    
    0 讨论(0)
提交回复
热议问题