Send email from command line in symfony2
18,568
Solution 1
It's because renderView is method of class Controller. Instead of that try:
$this->getContainer()->get('templating')->render(...);
Solution 2
Change
$this->renderView()
to
$this->getContainer()->get('templating')->render()
Solution 3
Maybe, not exactly the question you ask, but for sure - important.
Please, do remember that if you want to send emails via Command call, you need to flushQueue.
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
Author by
nasy
Updated on June 07, 2022Comments
-
nasy almost 2 years
I need to render a twig template from a command class in symfony2.
namespace IT\bBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class CronCommand extends ContainerAwareCommand { protected function configure() { $this ->setName('send:emails') ->setDescription('Envio programado de emails'); } protected function execute(InputInterface $input, OutputInterface $output) { $message = \Swift_Message::newInstance() ->setSubject('bla bla') ->setFrom('[email protected]') ->setTo('[email protected]') ->setCharset('UTF-8') ->setContentType('text/html') ->setBody($this->renderView('mainBundle:Email:default.html.twig')); $this->getContainer()->get('mailer')->send($message); $output->writeln('Enviado!'); } }
But when I execute the command
php app/console send:emails
I get the following error:Fatal error: Call to undefined method
IT\bBundle\Command\CronCommand::renderView()
How can I render the view?