pmiranda
pmiranda

Reputation: 8420

Swift Mailer on Symfony 3.4, configuration

This is what I see with php bin/console debug:config swiftmailer:

enter image description here

I can send emails with php bin/console swiftmailer:email:send they are sent ok:

enter image description here

Here is the email in my account:

enter image description here

But I can't do it with my controller on Symfony. I have tested many examples, none one works. I have tested on dev and prod enviroment (here mainly, here is where I need it of course). This is an example of my code:

public function sendEmail(Request $request ) {

    $message = \Swift_Message::newInstance()
    ->setSubject('Some Subject')
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setBody('asdas');

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

    dump($message);
    die();

I can see the full message in the output but no email is sent.

I have tested also this way:

$message = (new \Swift_Message('Wonderful Subject'))
          ->setFrom(['[email protected]' => 'John Doe'])
          ->setTo(['[email protected]' => 'A name'])
          ->setBody('Here is the message itself');

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

Same, nothing happens. What could be wrong?

Upvotes: 1

Views: 2654

Answers (1)

albert
albert

Reputation: 4468

You are using the spool option which delays the sending emails. Disable the spool or remove the die().

https://symfony.com/doc/current/email/spool.html

When you use Swiftmail spool it saves the email to send it during the termination event ( https://symfony.com/doc/current/components/http_kernel.html#the-kernel-terminate-event ) which in your case it is not happening as you are killing the script with die(); in your last line.

kernel.terminate in the Symfony Framework

If you use the memory spooling option of the default Symfony mailer, then the EmailSenderListener is activated, which actually delivers any emails that you scheduled to send during the request.

Upvotes: 3

Related Questions