Commit bbb63a5d authored by Dave Marshall's avatar Dave Marshall Committed by Fabien Potencier

Add option to disable memory spool

Conflicts:
	src/Silex/Provider/SwiftmailerServiceProvider.php
parent b0edcc39
...@@ -10,6 +10,8 @@ will attempt to send emails through SMTP. ...@@ -10,6 +10,8 @@ will attempt to send emails through SMTP.
Parameters 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 * **swiftmailer.options**: An array of options for the default SMTP-based
configuration. configuration.
...@@ -97,13 +99,17 @@ The Swiftmailer provider provides a ``mailer`` service:: ...@@ -97,13 +99,17 @@ The Swiftmailer provider provides a ``mailer`` service::
Usage in commands 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 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. 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 For that reason, if you send emails using a command console, it is recommended
flush the message spool by hand before ending the command execution. To do so, that you disable the use of the memory spool (before accessing ``$app['mailer']``)::
use the following code::
$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'] $app['swiftmailer.spooltransport']
->getSpool() ->getSpool()
......
...@@ -24,13 +24,15 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface ...@@ -24,13 +24,15 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
public function register(Application $app) public function register(Application $app)
{ {
$app['swiftmailer.options'] = array(); $app['swiftmailer.options'] = array();
$app['swiftmailer.use_spool'] = true;
$app['mailer.initialized'] = false; $app['mailer.initialized'] = false;
$app['mailer'] = $app->share(function ($app) { $app['mailer'] = $app->share(function ($app) {
$app['mailer.initialized'] = true; $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) { $app['swiftmailer.spooltransport'] = $app->share(function ($app) {
......
...@@ -27,6 +27,22 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -27,6 +27,22 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('Swift_Mailer', $app['mailer']); $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() public function testSwiftMailerSendsMailsOnFinish()
{ {
$app = new Application(); $app = new Application();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment