【问题标题】:Swiftmailer//Twig email not rendering correctlySwiftmailer//Twig 电子邮件未正确呈现
【发布时间】:2012-09-11 14:46:52
【问题描述】:

我正在试验 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%}

我做错了什么?

【问题讨论】:

    标签: symfony twig swiftmailer


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2020-08-01
      • 2011-10-28
      • 2022-01-24
      • 2017-03-06
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多