lio89
lio89

Reputation: 115

SwiftMailer calling an undefined method

I have an error trying to use SwiftMailer with Symfony. I'm following a tutorial from here: http://symblog.site90.net/docs/validators-and-forms.html

The error code is:

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

My action is:

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('[email protected]')
             ->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(),));}

The error says it's undefined but, my method setMessage() is defined in my class Contacto.php with getter and 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;
    }

According on what I read trying to solve it myself, could the problem be related to the versions of SwiftMailer ?

Thanks

Upvotes: 2

Views: 3547

Answers (1)

Simon Duflos
Simon Duflos

Reputation: 131

The method "setMessage" refers indeed to your Contacto entity, but to set the content of a message with SwiftMailer, you have to use setBody();

Example :

    $mail = \Swift_Message::newInstance();
    $mail->setFrom('[email protected]')
         ->setTo('[email protected]')
         ->setSubject('Email subject')
         ->setBody('email body, can be swift template')
         ->setContentType('text/html');

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

EDIT : save the email content in database and send it to Twig template

I added a bit of code in your function, to actually save the email content in database. It's not mandatory but I imagine you'll want to retrieve this information later in your application. Anyway, I didn't test this, but it should work : your controller set the SwiftMailer message body as a Twig template. Your template should be able to parse the "contacto" entity.

    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('[email protected]')
                        ->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()));
    }

Upvotes: 1

Related Questions