This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
SwiftmailerExtension.php
148 lines (126 loc) · 5.43 KB
/
SwiftmailerExtension.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
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
/**
* SwiftmailerExtension is an extension for the SwiftMailer library.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SwiftmailerExtension extends Extension
{
/**
* Loads the Swift Mailer configuration.
*
* Usage example:
*
* <swiftmailer:config transport="gmail">
* <swiftmailer:username>fabien</swift:username>
* <swiftmailer:password>xxxxx</swift:password>
* <swiftmailer:spool path="/path/to/spool/" />
* </swiftmailer:config>
*
* @param array $configs An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('swiftmailer.xml');
$container->setAlias('mailer', 'swiftmailer.mailer');
$configuration = new Configuration($container->getParameter('kernel.debug'));
$config = $this->processConfiguration($configuration, $configs);
if (null === $config['transport']) {
$transport = 'null';
} elseif ('gmail' === $config['transport']) {
$config['encryption'] = 'ssl';
$config['auth_mode'] = 'login';
$config['host'] = 'smtp.gmail.com';
$transport = 'smtp';
} else {
$transport = $config['transport'];
}
if (isset($config['disable_delivery']) && $config['disable_delivery']) {
$transport = 'null';
}
if ('smtp' === $transport) {
$loader->load('smtp.xml');
}
if (in_array($transport, array('smtp', 'mail', 'sendmail', 'null'))) {
// built-in transport
$transport = 'swiftmailer.transport.'.$transport;
}
$container->setAlias('swiftmailer.transport', $transport);
if (false === $config['port']) {
$config['port'] = 'ssl' === $config['encryption'] ? 465 : 25;
}
foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode') as $key) {
$container->setParameter('swiftmailer.transport.smtp.'.$key, $config[$key]);
}
// spool?
if (isset($config['spool'])) {
$type = $config['spool']['type'];
$loader->load('spool.xml');
if ($type === 'file') {
$loader->load('spool_file.xml');
}
$container->setAlias('swiftmailer.transport.real', $transport);
$container->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool');
$container->setAlias('swiftmailer.spool', 'swiftmailer.spool.'.$type);
foreach (array('path') as $key) {
$container->setParameter('swiftmailer.spool.'.$type.'.'.$key, $config['spool'][$key]);
}
}
$container->setParameter('swiftmailer.spool.enabled', isset($config['spool']));
// antiflood?
if (isset($config['antiflood'])) {
$container->setParameter('swiftmailer.plugin.antiflood.threshold', $config['antiflood']['threshold']);
$container->setParameter('swiftmailer.plugin.antiflood.sleep', $config['antiflood']['sleep']);
$container->getDefinition('swiftmailer.plugin.antiflood')->addTag('swiftmailer.plugin');
}
if ($config['logging']) {
$container->getDefinition('swiftmailer.plugin.messagelogger')->addTag('swiftmailer.plugin');
$container->findDefinition('swiftmailer.data_collector')->addTag('data_collector', array('template' => 'SwiftmailerBundle:Collector:swiftmailer', 'id' => 'swiftmailer'));
}
if (isset($config['sender_address']) && $config['sender_address']) {
$container->setParameter('swiftmailer.sender_address', $config['sender_address']);
$container->getDefinition('swiftmailer.plugin.impersonate')->addTag('swiftmailer.plugin');
} else {
$container->setParameter('swiftmailer.sender_address', null);
}
if (isset($config['delivery_address']) && $config['delivery_address']) {
$container->setParameter('swiftmailer.single_address', $config['delivery_address']);
$container->getDefinition('swiftmailer.plugin.redirecting')->addTag('swiftmailer.plugin');
} else {
$container->setParameter('swiftmailer.single_address', null);
}
}
/**
* Returns the base path for the XSD files.
*
* @return string The XSD base path
*/
public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}
/**
* Returns the namespace to be used for this extension (XML namespace).
*
* @return string The XML namespace
*/
public function getNamespace()
{
return 'http://symfony.com/schema/dic/swiftmailer';
}
}