Swift mail from Symfony Command

拜拜、爱过 提交于 2019-12-23 08:46:07

问题


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 ...

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

The code of the function lookslike this:

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

Update The line where the exception happens is $this->container->get('templating')->renderView(

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


回答1:


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


来源:https://stackoverflow.com/questions/18895782/swift-mail-from-symfony-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!