Php 从symfony2中的命令行发送电子邮件
我需要从symfony2中的命令类呈现一个细枝模板Php 从symfony2中的命令行发送电子邮件,php,symfony,console,twig,Php,Symfony,Console,Twig,我需要从symfony2中的命令类呈现一个细枝模板 namespace-IT\bBundle\Command; 使用Symfony\Bundle\FrameworkBundle\Command\ContainerWareCommand; 使用Symfony\Component\Console\Input\InputArgument; 使用Symfony\Component\Console\Input\InputInterface; 使用Symfony\Component\Console\Inpu
namespace-IT\bBundle\Command;
使用Symfony\Bundle\FrameworkBundle\Command\ContainerWareCommand;
使用Symfony\Component\Console\Input\InputArgument;
使用Symfony\Component\Console\Input\InputInterface;
使用Symfony\Component\Console\Input\InputOption;
使用Symfony\Component\Console\Output\OutputInterface;
类CronCommand扩展ContainerWareCommand
{
受保护的函数配置()
{
美元这个
->setName('发送:电子邮件')
->setDescription(“Envio programado de emails”);
}
受保护的函数执行(InputInterface$input,OutputInterface$output)
{
$message=\Swift\u message::newInstance()
->设置主体(“bla-bla”)
->设置自('x@x.com')
->设置为('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()
如何渲染视图?更改
$this->renderView()
到
$this->getContainer()->get('templating')->render()
这是因为renderView是类控制器的方法。与其这样,不如试试:
$this->getContainer()->get('templating')->render(...);
也许,这不是你要问的问题,但肯定很重要 请记住,如果您想通过命令调用发送电子邮件,您需要刷新队列
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
请注意:如果你计划发送大量电子邮件,这不是一种方式。如果只是偶尔一次就可以了。嗨,这似乎不是所有Symfony版本所必需的。我使用v3.6,使用命令发送电子邮件而不刷新队列。