This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SwiftmailerServiceProvider.php
140 lines (114 loc) · 5.15 KB
/
SwiftmailerServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
/**
* Swiftmailer Provider.
*
* @author Fabien Potencier <[email protected]>
*/
class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Container $app)
{
$app['swiftmailer.options'] = [];
$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($transport);
};
$app['swiftmailer.spooltransport'] = function ($app) {
return new \Swift_Transport_SpoolTransport($app['swiftmailer.transport.eventdispatcher'], $app['swiftmailer.spool']);
};
$app['swiftmailer.spool'] = function ($app) {
return new \Swift_MemorySpool();
};
$app['swiftmailer.transport'] = function ($app) {
$transport = new \Swift_Transport_EsmtpTransport(
$app['swiftmailer.transport.buffer'],
[$app['swiftmailer.transport.authhandler']],
$app['swiftmailer.transport.eventdispatcher']
);
$options = $app['swiftmailer.options'] = array_replace([
'host' => 'localhost',
'port' => 25,
'username' => '',
'password' => '',
'encryption' => null,
'auth_mode' => null,
'stream_context_options' => [],
], $app['swiftmailer.options']);
$transport->setHost($options['host']);
$transport->setPort($options['port']);
$transport->setEncryption($options['encryption']);
$transport->setUsername($options['username']);
$transport->setPassword($options['password']);
$transport->setAuthMode($options['auth_mode']);
$transport->setStreamOptions($options['stream_context_options']);
return $transport;
};
$app['swiftmailer.transport.buffer'] = function () {
return new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory());
};
$app['swiftmailer.transport.authhandler'] = function () {
return new \Swift_Transport_Esmtp_AuthHandler([
new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
]);
};
$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 [];
};
$app['swiftmailer.sender_address'] = null;
$app['swiftmailer.delivery_addresses'] = [];
$app['swiftmailer.delivery_whitelist'] = [];
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
// Event has no typehint as it can be either a PostResponseEvent or a ConsoleTerminateEvent
$onTerminate = function ($event) use ($app) {
// To speed things up (by avoiding Swift Mailer initialization), flush
// messages only if our mailer has been created (potentially used)
if ($app['mailer.initialized'] && $app['swiftmailer.use_spool'] && $app['swiftmailer.spooltransport'] instanceof \Swift_Transport_SpoolTransport) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
}
};
$dispatcher->addListener(KernelEvents::TERMINATE, $onTerminate);
if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
$dispatcher->addListener(ConsoleEvents::TERMINATE, $onTerminate);
}
}
}