6

我正在尝试使用 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()));
}

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

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

{% block title "Email de teste" %}

{% block content%}
<H1>Este é um render de um email de teste!</H1>
{% endblock%}

我究竟做错了什么?

4

1 回答 1

16

您必须通过像这样调用 setBody() 方法来指定正文的内容类型。

$message->setBody($messageBody, 'text/html');

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

于 2012-09-11T14:59:48.243 回答