【问题标题】:Swift mail from Symfony Command来自 Symfony 命令的 Swift 邮件
【发布时间】:2013-09-19 13:19:16
【问题描述】:

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

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

一个容器被添加到这个类中,我从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(

正如您在代码中看到的那样,最后一行可能会失败以及最终到达那里。

【问题讨论】:

    标签: php symfony swiftmailer


    【解决方案1】:

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

    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);
        }
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2019-11-30
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 2012-01-10
      相关资源
      最近更新 更多