14
14
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
15
15
use Symfony \Component \DependencyInjection \ContainerInterface ;
16
16
use Symfony \Component \DependencyInjection \ChildDefinition ;
17
+ use Symfony \Component \DependencyInjection \Definition ;
17
18
use Symfony \Component \DependencyInjection \DefinitionDecorator ;
18
19
use Symfony \Component \DependencyInjection \Reference ;
19
20
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
@@ -66,68 +67,57 @@ public function load(array $configs, ContainerBuilder $container)
66
67
67
68
protected function configureMailer ($ name , array $ mailer , ContainerBuilder $ container , $ isDefaultMailer = false )
68
69
{
69
- if (null === $ mailer ['transport ' ]) {
70
- $ transport = 'null ' ;
71
- } elseif ('gmail ' === $ mailer ['transport ' ]) {
72
- $ mailer ['encryption ' ] = 'ssl ' ;
73
- $ mailer ['auth_mode ' ] = 'login ' ;
74
- $ mailer ['host ' ] = 'smtp.gmail.com ' ;
75
- $ transport = 'smtp ' ;
76
- } else {
77
- $ transport = $ mailer ['transport ' ];
78
- }
79
-
80
- if (null !== $ mailer ['url ' ]) {
81
- $ parts = parse_url ($ mailer ['url ' ]);
82
- if (!empty ($ parts ['scheme ' ])) {
83
- $ transport = $ parts ['scheme ' ];
84
- }
70
+ $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.transport.eventdispatcher.abstract ' );
71
+ $ container
72
+ ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name ), $ definitionDecorator )
73
+ ;
85
74
86
- if (!empty ($ parts ['user ' ])) {
87
- $ mailer ['username ' ] = $ parts ['user ' ];
88
- }
89
- if (!empty ($ parts ['pass ' ])) {
90
- $ mailer ['password ' ] = $ parts ['pass ' ];
91
- }
92
- if (!empty ($ parts ['host ' ])) {
93
- $ mailer ['host ' ] = $ parts ['host ' ];
94
- }
95
- if (!empty ($ parts ['port ' ])) {
96
- $ mailer ['port ' ] = $ parts ['port ' ];
97
- }
98
- if (!empty ($ parts ['query ' ])) {
99
- $ query = array ();
100
- parse_str ($ parts ['query ' ], $ query );
101
- if (!empty ($ query ['encryption ' ])) {
102
- $ mailer ['encryption ' ] = $ query ['encryption ' ];
103
- }
104
- if (!empty ($ query ['auth_mode ' ])) {
105
- $ mailer ['auth_mode ' ] = $ query ['auth_mode ' ];
106
- }
107
- }
75
+ $ options = array ();
76
+ $ envVariablesAllowed = array ('transport ' , 'url ' , 'username ' , 'password ' , 'host ' , 'port ' , 'timeout ' , 'source_ip ' , 'local_domain ' , 'encryption ' , 'auth_mode ' );
77
+ foreach ($ envVariablesAllowed as $ key ) {
78
+ $ container ->resolveEnvPlaceholders ($ mailer [$ key ], null , $ userEnvs );
79
+ $ options [$ key ] = $ mailer [$ key ];
108
80
}
109
- unset($ mailer ['url ' ]);
110
81
111
- $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), $ transport );
82
+ if ($ userEnvs ) {
83
+ $ transportId = sprintf ('swiftmailer.mailer.%s.transport.dynamic ' , $ name );
84
+ $ definitionDecorator = (new Definition (\Swift_Transport::class))
85
+ ->setFactory (array (SwiftmailerTransportFactory::class, 'createTransport ' ))
86
+ ->setArguments (array (
87
+ $ options ,
88
+ new Reference ('router.request_context ' ),
89
+ new Reference (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name )),
90
+ ));
91
+ $ container ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.dynamic ' , $ name ), $ definitionDecorator );
92
+ $ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport ' , $ name ), $ transportId );
93
+
94
+ $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.mailer.abstract ' );
95
+ $ container
96
+ ->setDefinition (sprintf ('swiftmailer.mailer.%s ' , $ name ), $ definitionDecorator )
97
+ ->replaceArgument (0 , new Reference (sprintf ('swiftmailer.mailer.%s.transport ' , $ name )))
98
+ ;
112
99
113
- if (isset ($ mailer ['disable_delivery ' ]) && $ mailer ['disable_delivery ' ]) {
114
- $ transport = 'null ' ;
115
- $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.delivery.enabled ' , $ name ), false );
100
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), 'dynamic ' );
116
101
} else {
117
- $ container -> setParameter ( sprintf ( ' swiftmailer. mailer.%s.delivery.enabled ' , $ name ), true );
118
- }
102
+ $ mailer = SwiftmailerTransportFactory:: resolveOptions ( $ mailer );
103
+ $ transport = $ mailer [ ' transport ' ];
119
104
120
- if (empty ($ mailer ['port ' ])) {
121
- $ mailer ['port ' ] = 'ssl ' === $ mailer ['encryption ' ] ? 465 : 25 ;
122
- }
105
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), $ transport );
106
+
107
+ $ transportId = in_array ($ transport , array ('smtp ' , 'mail ' , 'sendmail ' , 'null ' ))
108
+ ? sprintf ('swiftmailer.mailer.%s.transport.%s ' , $ name , $ transport )
109
+ : $ transport ;
123
110
124
- $ this ->configureMailerTransport ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
125
- $ this ->configureMailerSpool ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
111
+ $ this ->configureMailerTransport ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
112
+ }
113
+ $ this ->configureMailerSpool ($ name , $ mailer , $ container , $ transportId , $ isDefaultMailer );
126
114
$ this ->configureMailerSenderAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
127
115
$ this ->configureMailerAntiFlood ($ name , $ mailer , $ container , $ isDefaultMailer );
128
116
$ this ->configureMailerDeliveryAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
129
117
$ this ->configureMailerLogging ($ name , $ mailer , $ container , $ isDefaultMailer );
130
118
119
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.delivery.enabled ' , $ name ), empty ($ mailer ['disable_delivery ' ]));
120
+
131
121
// alias
132
122
if ($ isDefaultMailer ) {
133
123
$ container ->setAlias ('swiftmailer.mailer ' , sprintf ('swiftmailer.mailer.%s ' , $ name ));
@@ -144,11 +134,6 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
144
134
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.smtp.%s ' , $ name , $ key ), $ mailer [$ key ]);
145
135
}
146
136
147
- $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.transport.eventdispatcher.abstract ' );
148
- $ container
149
- ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name ), $ definitionDecorator )
150
- ;
151
-
152
137
if ('smtp ' === $ transport ) {
153
138
$ authDecorator = $ this ->createChildDefinition ('swiftmailer.transport.authhandler.abstract ' );
154
139
$ container
@@ -278,10 +263,6 @@ protected function configureMailerSpool($name, array $mailer, ContainerBuilder $
278
263
))
279
264
;
280
265
281
- if (in_array ($ transport , array ('smtp ' , 'mail ' , 'sendmail ' , 'null ' ))) {
282
- // built-in transport
283
- $ transport = sprintf ('swiftmailer.mailer.%s.transport.%s ' , $ name , $ transport );
284
- }
285
266
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport.real ' , $ name ), $ transport );
286
267
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport ' , $ name ), sprintf ('swiftmailer.mailer.%s.transport.spool ' , $ name ));
287
268
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.spool.enabled ' , $ name ), true );
0 commit comments