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 <[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\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' => '[email protected]',

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' => '[email protected]',
1819
'delivery_address' => '[email protected]',
@@ -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
));

0 commit comments

Comments
 (0)