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