Swiftmailer // Twig电子邮件无法正确呈现

Clá*_*iro 6 swiftmailer symfony twig

我正在尝试使用symfony2框架,我正在尝试使用swiftmailer和twig发送电子邮件.问题是,在我目前的实现中,电子邮件是用html发送的(你可以看到标签,一切).

这是我正在使用的控制器:

 public function formularioAction()
{   
    $enquiry = new Enquiry();
    $form = $this->createForm(new EnquiryType(), $enquiry);

    $request = $this->getRequest();
    if($request->getMethod() == 'POST'){
        $form->bindRequest($request);

        if($form->isValid()){

            $message = \Swift_Message::newInstance()
                    ->setSubject('Mail from the App')
                    ->setFrom('no-reply@app.com')
                    ->setTo('******@gmail.com')
                    ->setBody($this->render( 'AcmeDemoBundle:Demo:email.html.twig' ));

            $this->get('mailer')->send($message);
            //end of mail sending

            //redirect - it's important to prevent users from reposting the form if
            //they refresh the page
            return $this->redirect($this->generateUrl( '_formulario'));
        }
    }
    return $this->render('AcmeDemoBundle:Demo:formulario.html.twig', array('form' => $form->createView()));
}
Run Code Online (Sandbox Code Playgroud)

电子邮件使用的树枝模板:

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block title "Email de teste" %}

{% block content%}
<H1>Este é um render de um email de teste!</H1>
{% endblock%}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 14

您必须通过调用setBody()方法来指定正文的Content-type.

$message->setBody($messageBody, 'text/html');
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅:http://swiftmailer.org/docs/messages.html#setting-the-body-content