Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony : Testing email sending in command

Good morning.

I would like to test sending email in my Symfony Command.

My email is send with \Swift_Mailer by

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

I try to use this:

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

But $this->client->getProfile() and Symfony Response is not available in Symfony Command.

Argument 1 passed to Symfony\Component\HttpKernel\Profiler\Profiler::loadProfileFromResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given,

Now how can i verify that my email is send correctly?

like image 531
V. Chataignier Avatar asked Aug 02 '26 19:08

V. Chataignier


2 Answers

When testing commands you should have $kernel variable. From that you get container with $kernel->getContainer(). With container you have access to message logger: $container->get('swiftmailer.mailer.default.plugin.messagelogger'). That's is. Now you have an instance of Swift_Plugins_MessageLogger which have #getMessages which returns sent messages.

like image 175
FreeLightman Avatar answered Aug 05 '26 08:08

FreeLightman


You can put the spool type of swiftmailer to file and then count the files in that path.

In symfony4 it could be:

test/swiftmailer.yaml

 swiftmailer:
    spool:
        type: file
        path: 'path/to/emailTestFolder'

Test file

$mails = new \FilesystemIterator('path/to/emailTestFolder', \FilesystemIterator::SKIP_DOTS);
$this->assertSame(1, iterator_count($mails));
like image 23
Diguin Avatar answered Aug 05 '26 09:08

Diguin