Commit 3e3e881e authored by Fabien Potencier's avatar Fabien Potencier

feature #1064 Add option to disable memory spool (davedevelopment)

This PR was squashed before being merged into the 2.0.x-dev branch (closes #1064).

Discussion
----------

Add option to disable memory spool

Thought this might do for #1054

Commits
-------

0501be91 Add option to disable memory spool
parents 82dd7d5a 0501be91
......@@ -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.
......@@ -100,13 +102,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()
......
......@@ -28,13 +28,15 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
public function register(Container $app)
{
$app['swiftmailer.options'] = array();
$app['swiftmailer.use_spool'] = true;
$app['mailer.initialized'] = false;
$app['mailer'] = 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'] = function ($app) {
......
......@@ -27,6 +27,22 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$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();
......
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