2012-09-13 21 views
26

में कमांड लाइन से ईमेल भेजें मुझे symfony2 में कमांड क्लास से एक ट्विग टेम्पलेट प्रस्तुत करने की आवश्यकता है।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!'); 
    } 
} 

लेकिन जब मैं आदेश php app/console send:emails मैं निम्नलिखित त्रुटि मिलती है निष्पादित करें:

Fatal error: Call to undefined method IT\bBundle\Command\CronCommand::renderView()

मैं कैसे दृश्य प्रस्तुत कर सकते हैं?

+0

बस एक पैर नोट: यदि आप थोक ईमेल भेजने की योजना बना रहे हैं तो यह ऐसा करने का तरीका नहीं है। अगर यह थोड़ी देर में ठीक है तो यह ठीक रहेगा। – transilvlad

उत्तर

73

को यह क्योंकि renderView वर्ग नियंत्रक की विधि है। इसके बजाय प्रयास करें:

$this->getContainer()->get('templating')->render(...); 
18

बदलें

$this->renderView() 

$this->getContainer()->get('templating')->render() 
8

शायद, बिल्कुल पूछे जाने वाले प्रश्न नहीं, बल्कि निश्चित रूप से - महत्वपूर्ण।

कृपया याद रखें कि यदि आप कमांड कॉल के माध्यम से ईमेल भेजना चाहते हैं, तो आपको फ्लश क्यूयू की आवश्यकता है।

$mailer = $container->get('mailer'); 
$spool = $mailer->getTransport()->getSpool(); 
$transport = $container->get('swiftmailer.transport.real'); 
$spool->flushQueue($transport); 
+0

धन्यवाद दाविद। – olegsv