2

I use Swiftmailer to send emails in my Symfony 2.5 project. Here is my related code

parameters.yml

parameters:
mailer_transport: smtp
mailer_host: mail.domain.com
mailer_username: user@domain.com
mailer_password: password
mailer_auth_mode:  login

config.yml

swiftmailer:
transport: %mailer_transport%
auth_mode:  %mailer_auth_mode%
host:      %mailer_host%
username:  %mailer_username%
password:  %mailer_password%
spool:     { type: memory }

controller:

$message = \Swift_Message::newInstance()
        ->setSubject('mana')
        ->setFrom('***@gmail.com')
        ->setTo('***@yahoo.com')
        ->setBody('this is test');
    $this->get('mailer')->send($message);

I have swiftmailer in appKernel.php

$bundles = array(
        ...
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        ...
    );

The email did not get sent. Can you help me?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
MaNa
  • 55
  • 2
  • 10
  • See [How to send spool from swiftmailer without using command](http://stackoverflow.com/q/12369520/2257664). – A.L Nov 29 '14 at 18:33
  • Tnx @A.L but in fact i want to send email immediately and spool is not important at all! – MaNa Nov 30 '14 at 13:51
  • Your problem probably comes from your SMTP server because your code is ok. – Nek Nov 30 '14 at 14:42

2 Answers2

4

The following code worked in a command but I adapted it for the controller:

$message = \Swift_Message::newInstance()
    ->setSubject('mana')
    ->setFrom('****@gmail.com')
    ->setTo('mana@yahoo.com')
    ->setBody('this is test');
# I removed this line: $this->get('mailer')->send($message);

$mailer = $this->get('mailer');

$mailer->send($message);

$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');

$spool->flushQueue($transport);

Source : Symfony2 documentation.

A.L
  • 10,259
  • 10
  • 67
  • 98
  • @MaNa I'm happy it worked for you. But according to the documentation linked in my answer, this code is not necessary if the spooling is properly configured, I suggest you to follow the guidelines from the documentation. – A.L Nov 30 '14 at 18:44
  • No email was sent in my service using just send I had to use the flushQueue method for them to finally send. When I ran php app/console swiftmailer:spool:send it said 0 emails is that because my spool type is memory and not file? – Kal Jul 10 '15 at 16:08
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);

parameters.yml -default

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
websky
  • 3,047
  • 1
  • 33
  • 31