Symfony2 Swiftmailer Not Sending

Shawn Northrop picture Shawn Northrop · Apr 3, 2012 · Viewed 42.4k times · Source

I am having trouble sending emails with symfony2 and swiftmailer. I am also a bit lost on how to debug this issue. Below is the code. First I am creating a form to display. On submit (request->method == post) I then try to send the email. I am getting no errors and it is taking me to the thankyou page, however, I am not receiving any email. I have tested on prod and dev. In dev I have opened the profiler after submission and it shows 0 emails. Any help is appreciated! Thanks!

public function contactAction(Request $request)
{
    $defaultData = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
    $form = $this->createFormBuilder($defaultData)
        ->add('name', 'text')
        ->add('email', 'email')
        ->add('subject', 'text')
        ->add('message', 'textarea')
        ->getForm();

    if($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        $data = $form->getData();
        $message = \Swift_Message::newInstance()
            ->setSubject($data['subject'])
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody($this->renderView('AdaptiveSiteBundle:Default:email.txt.twig', array('name' => $data['name'], 'message' => $data['message'])))
        ;
        $this->get('mailer')->send($message);

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

    return array("form" => $form->createView());
}

Answer

Marcos Labad picture Marcos Labad · Oct 10, 2013

Can you post parameters.yml?

Also ensure that spooling is disabled so the email can be instantly sent. If you have a spool entry under the Swiftmailer configuration, delete it, for example:

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    spool:     { type: memory }

Should be:

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%