Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using swift mailer of symfony 2, got an email but getting unnecessary response in email

getting this response by email "HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: text/html; charset=UTF-8 Date: Tue, 13 Nov 2012 04:56:14 GMT".

here is my code:

 public function sendEmail($subject, $template, $templateParams)
    {
        $userEmail = $this->session->get('email');
        $name = $this->session->get('name');
        $adminEmail = $this->container;
        $templateParams['username'] = $name; 
        $message = \Swift_Message::newInstance()
                ->setSubject($subject)
                ->setFrom($adminEmail)
                ->setTo($userEmail)
                ->setBody($this->templating->render('SocialDonSocialBundle:Email:'.$template,$templateParams), 'text/html'); 
        $this->mailer->send($message);

Also note that this method is belongs to a service namely "Email". I have created a service "Email " which responsible to send emails. Does anybody know what might be the issue??

like image 209
Syed Rehman Avatar asked Nov 13 '12 07:11

Syed Rehman


2 Answers

You need to use renderView() instead of render(), because render() always display the header.

like image 187
user2535540 Avatar answered Sep 28 '22 11:09

user2535540


In newer versions of Symfony2 like version 2.5.*

The solution is to use renderResponse and do a getContent() on it :

$content = $this->container->get('templating')->renderResponse(
        'YOUR_TEMPLATE.twig',
        templateParams)
    )->getContent();

or with the same values like in the question :

$this->templating->renderResponse('SocialDonSocialBundle:Email:'.$template,$templateParams)->getContent();
like image 32
Nico Avatar answered Sep 28 '22 11:09

Nico