[英]Send email from command line in symfony2
我需要從symfony2中的命令類渲染一個twig模板。
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('x@x.com')
->setTo('x@gmail.com')
->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
出現以下錯誤:
致命錯誤:調用未定義的方法
IT\\bBundle\\Command\\CronCommand::renderView()
我該如何渲染視圖?
這是因為renderView是類Controller的方法。 而不是嘗試:
$this->getContainer()->get('templating')->render(...);
更改
$this->renderView()
至
$this->getContainer()->get('templating')->render()
也許,不完全是你問的問題,但肯定 - 重要。
請記住,如果您想通過Command調用發送電子郵件,則需要flushQueue。
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.