Symfony2 Swiftmailer Not Sending

Go To StackoverFlow.com

19

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('no-reply@mysite.com')
            ->setTo('email@mysite.com')
            ->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());
}
2012-04-03 20:20
by Shawn Northrop
If you want the profiler to show sent mails you have to remove the redirect - Hubert Perron 2012-04-03 20:23
you've setup a mail server - Flukey 2012-04-03 22:51
I have not set up a mail server... Any suggestions on servers / documentation for this? I am running ubuntu - Shawn Northrop 2012-04-04 02:29
@ShawnNorthrop How is it going to send emails if it hasn't got a mail server configured? Easiest suggestion: Configure swiftmail to send emails via gmails servers - Flukey 2012-04-04 12:58
The emails should be going out through godaddy. I have set this up as documented for godaddy smtp. I was unsure if i needed a mail server locally on top of this - Shawn Northrop 2012-04-04 19:00
Part of my problem is I see no logs or ways to debug this. I'm not sure if the connection / request to godaddy is ever being mad - Shawn Northrop 2012-04-04 19:02
My problem was trying to send an email from within a test which uses the test config, which defines disable_delivery: true by defaul - Pierre de LESPINAY 2017-06-13 13:03


10

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

2012-09-19 07:28
by Miloš
Good tutorial, the Gmail is working for me but I've not been able to use the default localhost config - Pierre de LESPINAY 2014-04-22 10:11
It's working on my local server but not on the distant server (online), what should I do ?? - krachleur 2016-03-14 17:40
Check your distant server parameters for the swiftmailer. Maybe you have custom params for the dev and prod environments - Miloš 2016-03-15 08:01
This link does not work anymor - DrewT 2016-09-02 16:55
This can be reused instead: http://symfony.com/doc/current/email.htm - Miloš 2016-09-22 08:14
Thanks for the reference. It's just too bad that things described on the Symfony website only work 20% of the time - Kafoso 2018-07-09 16:22


24

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%
2013-10-09 22:18
by Marcos Labad
This works for me as I'm using gmail transpor - Rocco 2013-11-21 09:50
Thanks I could not see the error (Connection could not be established with host - arsenik 2015-06-28 10:07


13

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.

2013-04-11 06:51
by smentek


5

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
2012-04-05 12:32
by Dieter
Nope, I checked and I have not disabled. This is also happening on productio - Shawn Northrop 2012-04-05 23:49
Another long shot, but I often have no errors showing up in my Sf2 logs, which do show up in /var/log/apache2/error.log, see anything there? (Sorry, as I said, long shot - Dieter 2012-04-06 09:38
After removing the redirect, I am seeing that an email is being sent through the profiler.. I am still not receiving it though. Nothing in the apache, php, or symfony log - Shawn Northrop 2012-04-09 00:47
Sorry m8, not a clue then. Maybe try changing to gmail sending instead of smtp, so you can at least see if everything works like that. If it does, check out the smtp settings on both end, you and GoDaddy - Dieter 2012-04-10 08:23
Ill check it out, thanks for the hel - Shawn Northrop 2012-04-12 03:44


5

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.

2016-08-10 06:58
by Chintan Panchal
Replacing the quick & dirty die() with return new Response() worked for me. Thanks - Mr. B. 2016-09-05 17:50
Thanks for saving my tim - AbdulBasit 2017-01-09 12:16


0

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

please consider adding this in your config.yml

fos_user:
    from_email:
            address:        noreply@yourdomain.com
            sender_name:    yourname

If you don't do this, fos user bundle will send the email with noreply@exemple.com and OVH flag this as spam.

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

2014-06-06 16:19
by 0x1gene


0

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('username@xx.eu')
            ->setTo('xxx@stackoverflow.eu')
            ->setBody('message');
$this->get('mailer')->send($messageObject);
2015-04-28 07:55
by websky