Skip to content

Commit 767fbb9

Browse files
committed
Added special SwiftMailerHandler that flushes mail queue if necessary
1 parent f669c95 commit 767fbb9

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
276276
$handler['level'],
277277
$handler['bubble'],
278278
));
279+
$definition->addMethodCall('setTransport', array(new Reference('swiftmailer.transport.real')));
279280
break;
280281

281282
case 'native_mailer':

Handler/SwiftMailerHandler.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\Handler;
13+
14+
use Monolog\Handler\SwiftMailerHandler as BaseSwiftMailerHandler;
15+
16+
/**
17+
* Extended SwiftMailerHandler that flushes mail queue if necessary
18+
*
19+
* @author Philipp Kräutli <[email protected]>
20+
*/
21+
class SwiftMailerHandler extends BaseSwiftMailerHandler
22+
{
23+
protected $transport;
24+
25+
/**
26+
* @param \Swift_Transport $transport
27+
*/
28+
public function setTransport(\Swift_Transport $transport)
29+
{
30+
$this->transport = $transport;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected function send($content, array $records)
37+
{
38+
parent::send($content, $records);
39+
40+
$this->flushQueue();
41+
}
42+
43+
/**
44+
* Flushes the mail queue if necessary
45+
*/
46+
private function flushQueue()
47+
{
48+
$transport = $this->mailer->getTransport();
49+
if (!$transport instanceof \Swift_Transport_SpoolTransport) {
50+
return;
51+
}
52+
53+
$spool = $transport->getSpool();
54+
if (!$spool instanceof \Swift_MemorySpool) {
55+
return;
56+
}
57+
58+
if (null == $this->transport) {
59+
throw new \Exception('No transport available to flush mail queue');
60+
}
61+
62+
$spool->flushQueue($this->transport);
63+
}
64+
}

Resources/config/monolog.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parameter key="monolog.handler.firephp.class">Symfony\Bridge\Monolog\Handler\FirePHPHandler</parameter>
2020
<parameter key="monolog.handler.chromephp.class">Symfony\Bridge\Monolog\Handler\ChromePhpHandler</parameter>
2121
<parameter key="monolog.handler.debug.class">Symfony\Bridge\Monolog\Handler\DebugHandler</parameter>
22-
<parameter key="monolog.handler.swift_mailer.class">Monolog\Handler\SwiftMailerHandler</parameter>
22+
<parameter key="monolog.handler.swift_mailer.class">Symfony\Bundle\MonologBundle\Handler\SwiftMailerHandler</parameter>
2323
<parameter key="monolog.handler.native_mailer.class">Monolog\Handler\NativeMailerHandler</parameter>
2424
<parameter key="monolog.handler.socket.class">Monolog\Handler\SocketHandler</parameter>
2525
<parameter key="monolog.handler.pushover.class">Monolog\Handler\PushoverHandler</parameter>

0 commit comments

Comments
 (0)