1
votes
  $mail = \Swift_Message::newInstance()
            ->setSubject($message->getSubject())
            ->setFrom($message->getEmail())
            ->setBody($message->getBody());

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

Mails are not sent when using swiftmailer. Yet, when I go step by step in the debugger, it seems to send the email and returns sent=1. However I don't receive anything in my mailbox (gmail). I use my gmail account for sending emails, as shown below:

parameters:
mailer_transport: gmail
mailer_host: ~ 
mailer_user: [email protected]
mailer_password: my-password
delivery_address: [email protected]



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

I've checked apache error log, nothing. I've run php app/console swiftmailer:spool:send just in case, but no luck.

What could prevent emails from being sent ?

4
The hint on swiftmailer console command pointed me in the right direction.Paolo Stefan

4 Answers

2
votes

You need either to:

1) Remove the spool line in your swiftmailer configuration, this one:

spool: { type: memory }

2) Trigger the spool queue to be sent:

php app/console swiftmailer:spool:send

But I think you are looking for option 1)

2
votes

u can try this one...

Parameter.yml

mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: 'xxxxxxxxxxxx'

config.yml

    swiftmailer:
       transport: gmail
       host:      smtp.gmail.com
       username:  '[email protected]'
       password:  'Password'
1
votes

The ->setTo part was missing ! That solved it.

  $mail = \Swift_Message::newInstance()
            ->setSubject($message->getSubject())
            ->setFrom($message->getEmail())
            ->setTo("[email protected]")
            ->setBody($message->getBody());
-1
votes

First, when you set spool: { type: memory } it means that you actually want to send the emails manually by running the spool command: php app/console swiftmailer:spool:send. So removing that spool line is going to help.

Second, you need to properly configure your mailing host. For development purposes, I strongly recommend that you use a service such as SendGrid and configure it as detailed in this Symfony2 Mailing Setup Tutorial

The tutorial also contains details on how to properly setup a mailing service in Symfony2 and how to build mailing templates.

Third, if you want to stick with Gmail, then the correct data is:

mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: [email protected]
mailer_password: 'your_gmail_password'
mailer_port: 587