0

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?

5
  • Are you using spool memory? If yes this is the problem. See stackoverflow.com/questions/13122096/… Commented Nov 15, 2013 at 10:11
  • yes im using spool memory. and it works in other classes. but here its not working, i dont know why Commented Nov 15, 2013 at 10:18
  • 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. Commented Nov 15, 2013 at 10:21
  • when im working in web Commented Nov 15, 2013 at 10:25
  • when i change the spool to file, i cant sent email, even from web Commented Nov 15, 2013 at 10:27

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And I got this error: Call to a member function getKernel() on a non-object
@BrunoRamalho Make sure you don't make in call to these methods in the configure method. All the code has to be called from the execute method or one of sub methods of this one.
0

Probably your spool settings in config.yml

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

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

1 Comment

im using spool type memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.