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

2

8

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

Flock answered 13/11, 2012 at 7:56 Comment(2)
I can't see any reason why you'd be getting those headers. Does your template do anything strange like output headers itself?Burck
I am getting it also. Did you fix it?Insole
D
31

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

Delly answered 12/9, 2013 at 13:17 Comment(0)
I
2

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();
Intangible answered 26/6, 2015 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.