Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

Commit 287d37f

Browse files
committed
feature #141 Support setLocalDomain() and setStreamOptions() (c960657)
This PR was squashed before being merged into the 2.4-dev branch (closes #141). Discussion ---------- Support setLocalDomain() and setStreamOptions() This PR adds support for Swift_Transport_AbstractSmtpTransport::setLocalDomain() and Swift_Transport_EsmtpTransport::setStreamOptions(). Commits ------- ae7831a Support setLocalDomain() and setStreamOptions()
2 parents c6e5fdd + ae7831a commit 287d37f

24 files changed

+299
-60
lines changed

DependencyInjection/Configuration.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,36 @@ private function getMailersNode()
108108
->scalarNode('port')->defaultNull()->end()
109109
->scalarNode('timeout')->defaultValue(30)->end()
110110
->scalarNode('source_ip')->defaultNull()->end()
111+
->scalarNode('local_domain')->defaultNull()->end()
112+
->arrayNode('stream_options')
113+
->ignoreExtraKeys(false)
114+
->normalizeKeys(false)
115+
->beforeNormalization()
116+
->ifTrue(function ($v) { return isset($v['stream-option']); })
117+
->then(function ($v) {
118+
$recurse = function ($array) use (&$recurse) {
119+
if (isset($array['name'])) {
120+
$array = array($array);
121+
}
122+
$n = array();
123+
foreach ($array as $v) {
124+
$k = $v['name'];
125+
if (isset($v['value'])) {
126+
$n[$k] = $v['value'];
127+
} elseif (isset($v['stream-option'])) {
128+
$n[$k] = $recurse($v['stream-option']);
129+
}
130+
}
131+
return $n;
132+
};
133+
return $recurse($v['stream-option']);
134+
})
135+
->end()
136+
->validate()
137+
->ifTrue(function ($v) { return !method_exists('Swift_Transport_EsmtpTransport', 'setStreamOptions'); })
138+
->thenInvalid('stream_options is only available in Swiftmailer 5.4.2 or later.')
139+
->end()
140+
->end()
111141
->scalarNode('encryption')
112142
->defaultNull()
113143
->validate()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\SwiftmailerBundle\DependencyInjection;
13+
14+
use Symfony\Component\Routing\RequestContext;
15+
16+
class SmtpTransportConfigurator
17+
{
18+
private $localDomain;
19+
20+
private $requestContext;
21+
22+
public function __construct($localDomain, RequestContext $requestContext = null)
23+
{
24+
$this->localDomain = $localDomain;
25+
$this->requestContext = $requestContext;
26+
}
27+
28+
/**
29+
* Sets the local domain based on the current request context.
30+
*/
31+
public function configure(\Swift_Transport_AbstractSmtpTransport $transport)
32+
{
33+
if ($this->localDomain) {
34+
$transport->setLocalDomain($this->localDomain);
35+
} elseif ($this->requestContext) {
36+
$transport->setLocalDomain($this->requestContext->getHost());
37+
}
38+
}
39+
}

DependencyInjection/SwiftmailerExtension.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;
1313

1414
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -138,13 +139,15 @@ protected function configureMailer($name, array $mailer, ContainerBuilder $conta
138139

139140
protected function configureMailerTransport($name, array $mailer, ContainerBuilder $container, $transport, $isDefaultMailer = false)
140141
{
141-
foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode', 'timeout', 'source_ip') as $key) {
142+
foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode', 'timeout', 'source_ip', 'local_domain') as $key) {
142143
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.%s', $name, $key), $mailer[$key]);
143144
}
145+
144146
$definitionDecorator = new DefinitionDecorator('swiftmailer.transport.eventdispatcher.abstract');
145147
$container
146148
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
147149
;
150+
148151
if ('smtp' === $transport) {
149152
$authDecorator = new DefinitionDecorator('swiftmailer.transport.authhandler.abstract');
150153
$container
@@ -157,6 +160,15 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
157160
$container
158161
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator);
159162

163+
$configuratorDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.configurator.abstract');
164+
$container
165+
->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator)
166+
->setArguments(array(
167+
sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name),
168+
new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE),
169+
))
170+
;
171+
160172
$definitionDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.abstract');
161173
$container
162174
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.smtp', $name), $definitionDecorator)
@@ -170,21 +182,39 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
170182
->addMethodCall('setEncryption', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.encryption%%', $name)))
171183
->addMethodCall('setTimeout', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.timeout%%', $name)))
172184
->addMethodCall('setSourceIp', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.source_ip%%', $name)))
185+
->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure'))
173186
;
187+
188+
if (isset($mailer['stream_options'])) {
189+
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.stream_options', $name), $mailer['stream_options']);
190+
$definitionDecorator->addMethodCall('setStreamOptions', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.stream_options%%', $name)));
191+
}
192+
174193
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
175194
} elseif ('sendmail' === $transport) {
176195
$bufferDecorator = new DefinitionDecorator('swiftmailer.transport.buffer.abstract');
177196
$container
178197
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator);
179198

199+
$configuratorDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.configurator.abstract');
200+
$container
201+
->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator)
202+
->setArguments(array(
203+
sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name),
204+
new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE),
205+
))
206+
;
207+
180208
$definitionDecorator = new DefinitionDecorator(sprintf('swiftmailer.transport.%s.abstract', $transport));
181209
$container
182210
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport), $definitionDecorator)
183211
->setArguments(array(
184212
new Reference(sprintf('swiftmailer.mailer.%s.transport.buffer', $name)),
185213
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
186214
))
215+
->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure'))
187216
;
217+
188218
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
189219
} elseif ('mail' === $transport) {
190220
$definitionDecorator = new DefinitionDecorator(sprintf('swiftmailer.transport.%s.abstract', $transport));

Resources/config/schema/swiftmailer-1.0.xsd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<xsd:complexType name="config">
1111
<xsd:sequence>
1212
<xsd:element name="mailer" type="mailer" minOccurs="0" maxOccurs="unbounded" />
13+
<xsd:element name="stream-options" type="stream-options" minOccurs="0" maxOccurs="1" />
1314
<xsd:element name="antiflood" type="antiflood" minOccurs="0" maxOccurs="1" />
1415
<xsd:element name="spool" type="spool" minOccurs="0" maxOccurs="1" />
1516
<xsd:element name="delivery-whitelist-pattern" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
@@ -21,6 +22,20 @@
2122
<xsd:attributeGroup ref="mailer-config" />
2223
</xsd:complexType>
2324

25+
<xsd:complexType name="stream-options">
26+
<xsd:choice minOccurs="1" maxOccurs="unbounded">
27+
<xsd:element name="stream-option" type="stream-option" />
28+
</xsd:choice>
29+
<xsd:attribute name="name" type="xsd:string" />
30+
</xsd:complexType>
31+
32+
<xsd:complexType name="stream-option" mixed="true">
33+
<xsd:choice minOccurs="0" maxOccurs="unbounded">
34+
<xsd:element name="stream-option" type="stream-option" />
35+
</xsd:choice>
36+
<xsd:attribute name="name" type="xsd:string" />
37+
</xsd:complexType>
38+
2439
<xsd:attributeGroup name="mailer-config">
2540
<xsd:attribute name="username" type="xsd:string" />
2641
<xsd:attribute name="password" type="xsd:string" />
@@ -31,6 +46,7 @@
3146
<xsd:attribute name="timeout" type="xsd:string"/>
3247
<xsd:attribute name="type" type="xsd:string" />
3348
<xsd:attribute name="source-ip" type="xsd:string"/>
49+
<xsd:attribute name="local-domain" type="xsd:string"/>
3450
<xsd:attribute name="transport" type="xsd:string" />
3551
<xsd:attribute name="logging" type="xsd:string" />
3652
<xsd:attribute name="delivery-address" type="xsd:string" />
@@ -41,6 +57,7 @@
4157

4258
<xsd:complexType name="mailer">
4359
<xsd:sequence>
60+
<xsd:element name="stream-options" type="stream-options" minOccurs="0" maxOccurs="1" />
4461
<xsd:element name="antiflood" type="antiflood" minOccurs="0" maxOccurs="1" />
4562
<xsd:element name="spool" type="spool" minOccurs="0" maxOccurs="1" />
4663
<xsd:element name="delivery-whitelist-pattern" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>

Resources/config/swiftmailer.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
<argument type="service" id="swiftmailer.transport" />
3535
</service>
3636

37+
<service id="swiftmailer.transport.smtp.configurator.abstract" class="Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SmtpTransportConfigurator" abstract="true" public="false" />
38+
3739
<service id="swiftmailer.transport.sendmail.abstract" class="%swiftmailer.transport.sendmail.class%" abstract="true" public="false" />
3840

3941
<service id="swiftmailer.transport.mail.abstract" class="%swiftmailer.transport.mail.class%" abstract="true" public="false">

Tests/DependencyInjection/Fixtures/config/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'auth-mode' => 'login',
1111
'timeout' => '1000',
1212
'source_ip' => '127.0.0.1',
13+
'local_domain' => 'local.example.com',
1314
'logging' => true,
1415
'spool' => array('type' => 'memory'),
1516
'delivery_address' => 'single@host.com',

Tests/DependencyInjection/Fixtures/config/php/many_mailers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'auth-mode' => 'login',
1414
'timeout' => '1000',
1515
'source_ip' => '127.0.0.1',
16+
'local_domain' => 'first.example.org',
1617
'logging' => true,
1718
'sender_address' => 'first-sender@example.org',
1819
'delivery_address' => 'first@example.org',
@@ -31,6 +32,7 @@
3132
'auth-mode' => 'login',
3233
'timeout' => '1000',
3334
'source_ip' => '127.0.0.1',
35+
'local_domain' => 'second.example.org',
3436
'logging' => true,
3537
'spool' => array(
3638
'type' => 'memory',
@@ -51,6 +53,7 @@
5153
'auth-mode' => 'login',
5254
'timeout' => '1000',
5355
'source_ip' => '127.0.0.1',
56+
'local_domain' => 'third.example.org',
5457
'logging' => true,
5558
'spool' => array(
5659
'type' => 'file',

Tests/DependencyInjection/Fixtures/config/php/one_mailer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'auth-mode' => 'login',
1414
'timeout' => '1000',
1515
'source_ip' => '127.0.0.1',
16+
'local_domain' => 'local.example.org',
1617
),
1718
),
1819
));

Tests/DependencyInjection/Fixtures/config/php/sendmail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
$container->loadFromExtension('swiftmailer', array(
44
'transport' => 'sendmail',
5+
'local_domain' => 'local.example.org',
56
));

Tests/DependencyInjection/Fixtures/config/php/smtp.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
'timeout' => '1000',
1212
'source_ip' => '127.0.0.1',
1313
'logging' => true,
14+
'local_domain' => 'local.example.org',
1415
));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
$container->loadFromExtension('swiftmailer', array(
3+
'transport' => 'smtp',
4+
'host' => 'example.org',
5+
'port' => '12345',
6+
'source_ip' => '127.0.0.1',
7+
'stream_options' => array(
8+
'ssl' => array(
9+
'verify_peer' => true,
10+
'verify_depth' => 5,
11+
'cafile' => '/etc/ssl/cacert.pem',
12+
'CN_match' => 'ssl.example.com',
13+
),
14+
),
15+
));

Tests/DependencyInjection/Fixtures/config/xml/full.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
auth-mode="login"
1616
timeout="1000"
1717
source-ip="127.0.0.1"
18+
local-domain="local.example.com"
1819
logging="true"
1920
delivery-address="single@host.com">
2021
<swiftmailer:antiflood/>

Tests/DependencyInjection/Fixtures/config/xml/many_mailers.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
auth-mode="login"
1717
timeout="1000"
1818
source-ip="127.0.0.1"
19+
local-domain="first.example.org"
1920
logging="true"
2021
delivery-address="single@host.com">
2122
<swiftmailer:antiflood/>
@@ -30,6 +31,7 @@
3031
auth-mode="login"
3132
timeout="1000"
3233
source-ip="127.0.0.1"
34+
local-domain="second.example.org"
3335
logging="true"
3436
delivery-address="single@host.com">
3537
<swiftmailer:spool type="memory"/>
@@ -44,6 +46,7 @@
4446
auth-mode="login"
4547
timeout="1000"
4648
source-ip="127.0.0.1"
49+
local-domain="third.example.org"
4750
logging="true"
4851
delivery-address="single@host.com">
4952
</swiftmailer:mailer>

Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
encryption="tls"
1616
auth-mode="login"
1717
timeout="1000"
18-
source-ip="127.0.0.1">
18+
source-ip="127.0.0.1"
19+
local-domain="local.example.org">
1920
</swiftmailer:mailer>
2021
</swiftmailer:config>
2122
</container>

Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
<swiftmailer:config
1010
transport="sendmail"
11+
local-domain="local.example.org"
1112
/>
1213
</container>

Tests/DependencyInjection/Fixtures/config/xml/smtp.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
auth-mode="login"
1717
timeout="1000"
1818
source-ip="127.0.0.1"
19-
logging="true"/>
19+
local-domain="local.example.org"
20+
logging="true">
21+
</swiftmailer:config>
2022
</container>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd">
7+
8+
<swiftmailer:config
9+
transport="smtp"
10+
host="example.org"
11+
port="12345"
12+
source-ip="127.0.0.1">
13+
<swiftmailer:stream-options>
14+
<swiftmailer:stream-option name="ssl">
15+
<swiftmailer:stream-option name="verify_peer">true</swiftmailer:stream-option>
16+
<swiftmailer:stream-option name="verify_depth">5</swiftmailer:stream-option>
17+
<swiftmailer:stream-option name="cafile">/etc/ssl/cacert.pem</swiftmailer:stream-option>
18+
<swiftmailer:stream-option name="CN_match">ssl.example.com</swiftmailer:stream-option>
19+
</swiftmailer:stream-option>
20+
</swiftmailer:stream-options>
21+
</swiftmailer:config>
22+
</container>

Tests/DependencyInjection/Fixtures/config/yml/full.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
swiftmailer:
2-
transport: smtp
3-
username: user
4-
password: pass
5-
host: example.org
6-
port: 12345
7-
encryption: tls
8-
auth-mode: login
9-
timeout: 1000
10-
source_ip: 127.0.0.1
11-
logging: true
2+
transport: smtp
3+
username: user
4+
password: pass
5+
host: example.org
6+
port: 12345
7+
encryption: tls
8+
auth-mode: login
9+
timeout: 1000
10+
source_ip: 127.0.0.1
11+
local_domain: local.example.com
12+
logging: true
1213
spool:
1314
type: memory
1415
delivery_address: single@host.com

0 commit comments

Comments
 (0)