3

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?

Irshu
  • 8,248
  • 8
  • 53
  • 65

2 Answers2

4

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.

FreeLightman
  • 2,224
  • 2
  • 27
  • 42
2

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));
Diguin
  • 835
  • 8
  • 17