Shawn Northrop
Shawn Northrop

Reputation: 6016

Symfony2 Swiftmailer Not Sending

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());
}

Upvotes: 20

Views: 44388

Answers (7)

Marcos Labad
Marcos Labad

Reputation: 1191

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%

Upvotes: 25

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

You can find here the entire procedure on how to send emails with symfony2. I just tested it at it seems to work fine.

http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email

http://symfony.com/doc/current/email.html

Upvotes: 10

Chintan Panchal
Chintan Panchal

Reputation: 311

Apart from the above solution, I suggest you to remove die or exit from the function where you use the swiftmailer code. This will fix your problem if your code is proper.

Upvotes: 5

websky
websky

Reputation: 3162

config.yml

# Swiftmailer Configuration
swiftmailer:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       smtp.xx.eu
    username:   username
    password:   password 

controller/action

$messageObject = \Swift_Message::newInstance()
            ->setSubject('Subject')
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody('message');
$this->get('mailer')->send($messageObject);

Upvotes: 0

0x1gene
0x1gene

Reputation: 3458

I you have trouble receiving email with ovh, siwftmailer and fosUserBundle,

please consider adding this in your config.yml

fos_user:
    from_email:
            address:        [email protected]
            sender_name:    yourname

If you don't do this, fos user bundle will send the email with [email protected] and OVH flag this as spam.

source: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/emails.md

Upvotes: 0

smentek
smentek

Reputation: 2884

You may have mail spooling set. If it is a case you need to run:

php app/console swiftmailer:spool:send

to send spooled emails.

Check http://symfony.com/doc/master/cookbook/email/spool.html for more.

Upvotes: 13

Dieter
Dieter

Reputation: 1710

I often set the following configuration in config_dev.yml to -prevent- mails being sent during testing, maybe you have done the same and forgot?

If this is in config_dev.yml, set it to false:

swiftmailer:
  disable_delivery:  true

Upvotes: 6

Related Questions