Commit f1b4cc86 authored by Christian Schmidt's avatar Christian Schmidt

Add support for SwiftMailer plugins

parent 57aea820
......@@ -47,6 +47,16 @@ Parameters
``delivery_addresses``. If set, emails matching any of these patterns will be
delivered like normal, as well as being sent to ``delivery_addresses``.
* **swiftmailer.plugins**: Array of SwiftMailer plugins.
Example usage::
$app['swiftmailer.plugins'] = function ($app) {
return array(
new \Swift_Plugins_PopBeforeSmtpPlugin('pop3.example.com'),
);
};
Services
--------
......
......@@ -71,17 +71,6 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
$transport->setPassword($options['password']);
$transport->setAuthMode($options['auth_mode']);
if (null !== $app['swiftmailer.sender_address']) {
$transport->registerPlugin(new \Swift_Plugins_ImpersonatePlugin($app['swiftmailer.sender_address']));
}
if (!empty($app['swiftmailer.delivery_addresses'])) {
$transport->registerPlugin(new \Swift_Plugins_RedirectingPlugin(
$app['swiftmailer.delivery_addresses'],
$app['swiftmailer.delivery_whitelist']
));
}
return $transport;
};
......@@ -97,13 +86,36 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
));
};
$app['swiftmailer.transport.eventdispatcher'] = function () {
return new \Swift_Events_SimpleEventDispatcher();
$app['swiftmailer.transport.eventdispatcher'] = function ($app) {
$dispatcher = new \Swift_Events_SimpleEventDispatcher();
$plugins = $app['swiftmailer.plugins'];
if (null !== $app['swiftmailer.sender_address']) {
$plugins[] = new \Swift_Plugins_ImpersonatePlugin($app['swiftmailer.sender_address']);
}
if (!empty($app['swiftmailer.delivery_addresses'])) {
$plugins[] = new \Swift_Plugins_RedirectingPlugin(
$app['swiftmailer.delivery_addresses'],
$app['swiftmailer.delivery_whitelist']
);
}
foreach ($plugins as $plugin) {
$dispatcher->bindEventListener($plugin);
}
return $dispatcher;
};
$app['swiftmailer.plugins'] = function ($app) {
return array();
};
$app['swiftmailer.sender_address'] = null;
$app['swiftmailer.delivery_addresses'] = [];
$app['swiftmailer.delivery_whitelist'] = [];
$app['swiftmailer.delivery_addresses'] = array();
$app['swiftmailer.delivery_whitelist'] = array();
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
......
......@@ -36,7 +36,7 @@ class SpoolStub implements \Swift_Spool
public function queueMessage(\Swift_Mime_Message $message)
{
$this->messages[] = $message;
$this->messages[] = clone $message;
}
public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
......
......@@ -89,4 +89,45 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->terminate($request, $response);
$this->assertFalse($app['swiftmailer.spool']->hasFlushed);
}
public function testSwiftMailerSenderAddress()
{
$app = new Application();
$app->register(new SwiftmailerServiceProvider());
$app->boot();
$app['swiftmailer.spool'] = function () {
return new SpoolStub();
};
$app['swiftmailer.sender_address'] = 'foo@example.com';
$app['mailer']->send(\Swift_Message::newInstance());
$messages = $app['swiftmailer.spool']->getMessages();
$this->assertCount(1, $messages);
list($message) = $messages;
$this->assertEquals('foo@example.com', $message->getReturnPath());
}
public function testSwiftMailerPlugins()
{
$plugin = $this->getMockBuilder('Swift_Events_TransportChangeListener')->getMock();
$plugin->expects($this->once())->method('beforeTransportStarted');
$app = new Application();
$app->boot();
$app->register(new SwiftmailerServiceProvider());
$app['swiftmailer.plugins'] = function ($app) use ($plugin) {
return array($plugin);
};
$dispatcher = $app['swiftmailer.transport.eventdispatcher'];
$event = $dispatcher->createTransportChangeEvent(new \Swift_Transport_NullTransport($dispatcher));
$dispatcher->dispatchEvent($event, 'beforeTransportStarted');
}
}
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