Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email from command line in symfony2

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?

like image 241
nasy Avatar asked Sep 13 '12 14:09

nasy


People also ask

What is swift mailer?

- Let's start by taking a general look at what Swift Mailer is and what it does. It's a PHP library that you incorporate into your own applications to send email. It's rich in features designed to overcome the shortcomings of the PHP mail function. Swift Mailer was originally created in 2005 by Chris Corbyn.

What is Symfony mailer?

Symfony's Mailer & Mime components form a powerful system for creating and sending emails - complete with support for multipart messages, Twig integration, CSS inlining, file attachments and a lot more. Get them installed with: $ composer require symfony/mailer.

How do I know what version of Symfony I have?

If you have file system access to the project Look inside the file for a line like: const VERSION = '5.0. 4'; that's the Symfony version number.


1 Answers

It's because renderView is method of class Controller. Instead of that try:

$this->getContainer()->get('templating')->render(...); 
like image 186
Cyprian Avatar answered Oct 20 '22 04:10

Cyprian