0

我想发送一封带有变量名的电子邮件。我创建了如下操作:

$product = $this->getDoctrine()
            ->getRepository('EnsNewBundle:Products')
            ->find(2);



        $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('ucerturohit@gmail.com')
        ->setTo('ucerturohit@gmail.com')
        ->setContentType("text/html")
        ->setBody($this->renderView('EnsNewBundle:Default:index.html.twig'));
    $this->get('mailer')->send($message);

    return $this->render('EnsNewBundle:Default:index.html.twig',array('name'=>$product->getName()));

这里我想$product->getName()用邮件传输数据。在index.html.twig我定义如下:

Id of the product 5<br/>
Name of the product {{name}} <br/>
Description Fast Internet browsing speed

当我执行此操作时,我收到错误消息undefined variable name in index.html.twig。如果我想在发送邮件之前设置此值,我该怎么办。如果我为电子邮件正文制作硬代码,那么就没有问题。

4

2 回答 2

0

Doctrine 的方法find()应该返回结果集合,而不是单个对象。

您可能应该使用findOneByXXX().

于 2012-08-07T11:16:27.693 回答
0

我只添加了这个东西,问题就解决了。

 ->setBody($this->renderView('EnsNewBundle:Default:index.html.twig',array('name1'=>$product->getName())));
于 2012-08-07T11:21:29.920 回答