Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Add option to disable memory spool
Browse files Browse the repository at this point in the history
Conflicts:
	src/Silex/Provider/SwiftmailerServiceProvider.php
  • Loading branch information
davedevelopment authored and fabpot committed Dec 13, 2014
1 parent b0edcc3 commit bbb63a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
14 changes: 10 additions & 4 deletions doc/providers/swiftmailer.rst
Expand Up @@ -10,6 +10,8 @@ will attempt to send emails through SMTP.
Parameters
----------

* **swiftmailer.use_spool**: A boolean to specify whether or not to use the
memory spool, defaults to true.
* **swiftmailer.options**: An array of options for the default SMTP-based
configuration.

Expand Down Expand Up @@ -97,13 +99,17 @@ The Swiftmailer provider provides a ``mailer`` service::
Usage in commands
~~~~~~~~~~~~~~~~~

The Swiftmailer provider sends the emails using the ``KernelEvents::TERMINATE``
By default, the Swiftmailer provider sends the emails using the ``KernelEvents::TERMINATE``
event, which is fired after the response has been sent. However, as this event
isn't fired for console commands, your emails won't be sent.

For that reason, if you send emails using a command console, make sure to
flush the message spool by hand before ending the command execution. To do so,
use the following code::
For that reason, if you send emails using a command console, it is recommended
that you disable the use of the memory spool (before accessing ``$app['mailer']``)::

$app['swiftmailer.use_spool'] = false;

Alternatively, you can just make sure to flush the message spool by hand before
ending the command execution. To do so, use the following code::

$app['swiftmailer.spooltransport']
->getSpool()
Expand Down
4 changes: 3 additions & 1 deletion src/Silex/Provider/SwiftmailerServiceProvider.php
Expand Up @@ -24,13 +24,15 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
public function register(Application $app)
{
$app['swiftmailer.options'] = array();
$app['swiftmailer.use_spool'] = true;

$app['mailer.initialized'] = false;

$app['mailer'] = $app->share(function ($app) {
$app['mailer.initialized'] = true;
$transport = $app['swiftmailer.use_spool'] ? $app['swiftmailer.spooltransport'] : $app['swiftmailer.transport'];

return new \Swift_Mailer($app['swiftmailer.spooltransport']);
return new \Swift_Mailer($transport);
});

$app['swiftmailer.spooltransport'] = $app->share(function ($app) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Silex/Tests/Provider/SwiftmailerServiceProviderTest.php
Expand Up @@ -27,6 +27,22 @@ public function testSwiftMailerServiceIsSwiftMailer()
$this->assertInstanceOf('Swift_Mailer', $app['mailer']);
}

public function testSwiftMailerIgnoresSpoolIfDisabled()
{
$app = new Application();

$app->register(new SwiftmailerServiceProvider());
$app->boot();

$app['swiftmailer.use_spool'] = false;

$app['swiftmailer.spooltransport'] = function () {
throw new \Exception("Should not be instantiated");
};

$this->assertInstanceOf('Swift_Mailer', $app['mailer']);
}

public function testSwiftMailerSendsMailsOnFinish()
{
$app = new Application();
Expand Down

0 comments on commit bbb63a5

Please sign in to comment.