0
votes

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?

2
Are you using spool memory? If yes this is the problem. See stackoverflow.com/questions/13122096/…Maksim Kotlyar
yes im using spool memory. and it works in other classes. but here its not working, i dont know whyBrunoRamalho
could you clarify It works in other classes in cli or when you work in web. That a main point here. Check the link I posted.Maksim Kotlyar
when im working in webBrunoRamalho
when i change the spool to file, i cant sent email, even from webBrunoRamalho

2 Answers

3
votes

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.

0
votes

Probably your spool settings in config.yml

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

# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }