来自 Symfony Command 的 Swift 邮件 [英] Swift mail from Symfony Command

查看:24
本文介绍了来自 Symfony Command 的 Swift 邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Symfony 命令从命令行发送 Swift 邮件.虽然我得到以下异常.

I try to send a Swift mail from the commandline using a Symfony command. Though I get the following exception.

Fatal error: Call to undefined method Symfony\Bundle\TwigBundle\Debug\TimedTwigE
ngine::renderView() in ...

一个容器被添加到这个类中,我从 ContainerAwareCommand

A container is added to this class which I got from the command that is made ContainerAwareCommand

函数代码如下:

private function sendViaEmail($content) {
    $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('123@gmail.com')
            ->setTo('123@gmail.com')
            ->setBody(
            $this->container->get('templating')->renderView(
                    'BatchingBundle:Default:email.html.twig', array('content' => $content)
            )
    );
    $this->get('mailer')->send($message);
}

更新发生异常的行是 $this->container->get('templating')->renderView(

正如您在代码中看到的那样,最后一行可能会失败,因为它最终到达那里.

As you can see in the code the last line would probably fail aswell as it finally gets there.

推荐答案

正如错误信息所说,TwigEngine 中没有 renderView 方法.renderView() 是 symfony 基控制器类中的快捷方式:

As the error message said, there is no renderView method in the TwigEngine. renderView() is a shortcut in the symfony base controller class:

namespace Symfony\Bundle\FrameworkBundle\Controller

class Controller extends ContainerAware
{
    /**
     * Returns a rendered view.
     *
     * @param string $view       The view name
     * @param array  $parameters An array of parameters to pass to the view
     *
     * @return string The rendered view
     */
    public function renderView($view, array $parameters = array())
    {
        return $this->container->get('templating')->render($view, $parameters);
    }
}

在那里您可以看到使用 模板 服务呈现视图的正确方法.

There you can see the correct method to render a view with the templating service.

$this->container->get('templating')->render(
    'BatchingBundle:Default:email.html.twig', array('content' => $content)
)

这篇关于来自 Symfony Command 的 Swift 邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆