symfony - Swiftmailer//Twig 电子邮件未正确呈现

标签 symfony twig swiftmailer

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

这是我正在使用的 Controller :

 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()));
}

以及电子邮件使用的 Twig 模板:

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

{% block title "Email de teste" %}

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

我做错了什么?

最佳答案

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

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

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

关于symfony - Swiftmailer//Twig 电子邮件未正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12372400/

相关文章:

php - 如何创建可重用的 Twig 组件

php - Symfony 关联实体集合的高级过滤

symfony2 验证继承

php - 是否可以在 $_GET | 中设置/插入新变量$_POST | $_ session |来自 TWIG 的 $_COOKIE?

Symfony2 : establish a model relationship by id

php - 在 Twig 模板中设置多维数组中单个对象的值

php - Symfony2发送邮件时延迟flashbag消息

php - Swiftmailer 发送后删除附件

php - Swiftmailer 仅发送来自 BCC 的 999 封电子邮件

javascript - 如何在ajax请求中与formData一起发送附加数据