SwiftMailer调用一个未定义的方法

时间:2015-08-18 10:16:08

标签: php symfony swiftmailer

尝试将SwiftMailer与Symfony一起使用时出错。 我正在按照这里的教程:http://symblog.site90.net/docs/validators-and-forms.html

错误代码是:

FatalErrorException: Error: Call to undefined method Swift_Message::setMessage() in C:\xampp\htdocs\TP\src\TP\MainBundle\Controller\DefaultController.php line 327

我的行动是:

public function newAction()
{
$contacto = new Contacto();
$form = $this->createForm(new ContactoType(), $contacto);
$request = $this->getRequest();

    if ($request->getMethod() == 'POST') {
        $form->bind($request);
        if ($form->isValid()) {
            $message = \Swift_Message::newInstance()

            ->setFrom('enquiries@myweb.com.ar')
             ->setTo($this->container->getParameter('tp_main.emails.contact_email'))


            //this is the line 327 related with the error
 ->setMessage($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)));
//


        $this->get('mailer')->send($message);

        $this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');

            return $this->redirect($this->generateUrl('contacto_new'));
        }
    }

return $this->render('TPMainBundle:Default:contact.html.twig', array(
'form' => $form->createView(),));}

错误说它未定义但是, 我的方法setMessage()在我的类Contacto.php中使用getter和setter定义:

     /**
     * @var string
     *
     * @ORM\Column(name="message", type="text")
     */
    private $message;

     /**
     * Set message
     *
     * @param string $message
     * @return Contacto
     */

public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Get message
     *
     * @return string 
     */
    public function getMessage()
    {
        return $this->message;
    }

根据我自己试图解决的问题,问题可能与SwiftMailer的版本有关吗?

由于

1 个答案:

答案 0 :(得分:1)

方法" setMessage"确实是你的Contacto实体,但是要使用SwiftMailer设置消息的内容,你必须使用setBody();

示例:

    $mail = \Swift_Message::newInstance();
    $mail->setFrom('me@mail.com')
         ->setTo('you@mail.com')
         ->setSubject('Email subject')
         ->setBody('email body, can be swift template')
         ->setContentType('text/html');

    $this->get('mailer')->send($mail);

编辑:将电子邮件内容保存在数据库中并将其发送到Twig模板

我在您的函数中添加了一些代码,以实际将电子邮件内容保存在数据库中。它不是强制性的,但我想您以后会想要在应用程序中检索此信息。无论如何,我没有测试这个,但它应该工作:你的控制器将SwiftMailer消息体设置为Twig模板。您的模板应该能够解析" contacto"实体。

    public function newAction()
    {
        $contacto = new Contacto();
        $form = $this->createForm(new ContactoType(), $contacto);
        $request = $this->getRequest();

        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                // set your "contacto" entity
                $contacto->setMessage($form->get('message')->getData());
                // do the rest of $contact->set{......}

                // save the message in database (if you need to but I would do it)
                $em = $this->getDoctrine()->getManager();
                $em->persist($contacto);
                $em->flush();

                // SEND THE EMAIL
                $message = \Swift_Message::newInstance();
                $message->setFrom('enquiries@myweb.com.ar')
                        ->setTo($this->container->getParameter('tp_main.emails.contact_email'))
                        ->setSubject('YOUR SUBJECT')
                        ->setBody($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)))
                        ->setContentType('text/html');

                $this->get('mailer')->send($message);

                $this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');

                return $this->redirect($this->generateUrl('contacto_new'));
            }
        }

        return $this->render('TPMainBundle:Default:contact.html.twig', array('form' => $form->createView()));
    }