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()メソッドを呼び出して、本文のContent-typeを指定する必要があります。

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

詳細については、http ://swiftmailer.org/docs/messages.html#setting-the-body-contentを参照してください。

于 2012-09-11T14:59:48.243 に答える