I want to send an email with a variable name. I have created the action as follow:
$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()));
Here I want to transmit the data $product->getName() with the mail. In index.html.twig I have defined as follow:
Id of the product 5<br/>
Name of the product {{name}} <br/>
Description Fast Internet browsing speed
when I am executing this action then I am getting the error undefined variable name in index.html.twig. What should I do, if i want to set this value before sending the mail.
If I make hard code for email body then there is no issue.
Doctrine’s method
find()should return collection of results, not a single object.You should probably use
findOneByXXX().