Question

I'm developing one application with symfony2 In one side of application I'm sending emails, everything ok with this. But now I create one command to run in crontab, but this dont send emails. this is my command: use Doctrine\ORM\EntityManager; use Symfony\Component\Templating\EngineInterface;

class Sender { protected $em; protected $twig; protected $mailer; public function __construct($em, \Twig_Environment $twig, \Swift_Mailer $mailer) { $this->em = $em; $this->twig = $twig; $this->mailer = $mailer; }

public function runSender() {
    $proj = $this->em->createQuery ...
    $maillist = $this->em->createQuery ...
$templateFile = "projectprojBundle:MailList:emailNew.html.twig";
$templateContent = $this->twig->loadTemplate($templateFile);
$body = $templateContent->render(array('proj' => $proj));

    foreach ($maillist as $m) {
    $message = \Swift_Message::newInstance()->setSubject('New projects')
    ->setFrom('...')->setTo($m['email'])
    ->setContentType('text/html')
    ->setBody(trim($body));
    $this->mailer->send($message);
    } } }

everything is ok with the queries, i tested. and if i can send from other classes why i cant here?

Was it helpful?

Solution

Add this at the end of the execution of your command:

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

$spool->flushQueue($transport);

You have to extend the ContainerAwareCommand class to have access to the service container in your command.

OTHER TIPS

Probably your spool settings in config.yml

Use spool: { type: memory } to send e-mails instantly

# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top