1
1
<?php
2
2
3
- /*
4
- * This file is part of the symfony package.
5
- * (c) Fabien Potencier <[email protected] >
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
-
11
3
/**
12
4
* sfMailer is the main entry point for the mailer system.
13
5
*
14
- * This class is instanciated by sfContext on demand.
6
+ * This class is instanciated by sfContext on demand and compatible to swiftmailer ~5.2
15
7
*
16
8
* @package symfony
17
9
* @subpackage mailer
18
- * @author Fabien Potencier <[email protected] >
10
+ * @author Thomas A. Hirsch <[email protected] >
19
11
* @version SVN: $Id$
20
12
*/
21
- class sfMailer extends Swift_Mailer
13
+ class sfMailer extends sfMailerBase
22
14
{
23
- const
24
- REALTIME = 'realtime ' ,
25
- SPOOL = 'spool ' ,
26
- SINGLE_ADDRESS = 'single_address ' ,
27
- NONE = 'none ' ;
28
-
29
- protected
30
- $ spool = null ,
31
- $ logger = null ,
32
- $ strategy = 'realtime ' ,
33
- $ address = '' ,
34
- $ realtimeTransport = null ,
35
- $ force = false ,
36
- $ redirectingPlugin = null ;
37
-
38
- /**
39
- * Constructor.
40
- *
41
- * Available options:
42
- *
43
- * * charset: The default charset to use for messages
44
- * * logging: Whether to enable logging or not
45
- * * delivery_strategy: The delivery strategy to use
46
- * * spool_class: The spool class (for the spool strategy)
47
- * * spool_arguments: The arguments to pass to the spool constructor
48
- * * delivery_address: The email address to use for the single_address strategy
49
- * * transport: The main transport configuration
50
- * * * class: The main transport class
51
- * * * param: The main transport parameters
52
- *
53
- * @param sfEventDispatcher $dispatcher An event dispatcher instance
54
- * @param array $options An array of options
55
- */
56
- public function __construct (sfEventDispatcher $ dispatcher , $ options )
57
- {
58
- // options
59
- $ options = array_merge (array (
60
- 'charset ' => 'UTF-8 ' ,
61
- 'logging ' => false ,
62
- 'delivery_strategy ' => self ::REALTIME ,
63
- 'transport ' => array (
64
- 'class ' => 'Swift_MailTransport ' ,
65
- 'param ' => array (),
66
- ),
67
- ), $ options );
68
-
69
- $ constantName = 'sfMailer:: ' .strtoupper ($ options ['delivery_strategy ' ]);
70
- $ this ->strategy = defined ($ constantName ) ? constant ($ constantName ) : false ;
71
- if (!$ this ->strategy )
72
- {
73
- throw new InvalidArgumentException (sprintf ('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none) ' , $ options ['delivery_strategy ' ]));
74
- }
75
-
76
- if (sfMailer::NONE == $ this ->strategy )
77
- {
78
- $ options ['transport ' ]['class ' ] = 'Swift_NullTransport ' ;
79
- }
80
-
81
- // transport
82
- $ class = $ options ['transport ' ]['class ' ];
83
- $ transport = new $ class ();
84
- if (isset ($ options ['transport ' ]['param ' ]))
85
- {
86
- foreach ($ options ['transport ' ]['param ' ] as $ key => $ value )
87
- {
88
- $ method = 'set ' .ucfirst ($ key );
89
- if (method_exists ($ transport , $ method ))
90
- {
91
- $ transport ->$ method ($ value );
92
- }
93
- elseif (method_exists ($ transport , 'getExtensionHandlers ' ))
94
- {
95
- foreach ($ transport ->getExtensionHandlers () as $ handler )
96
- {
97
- if (in_array (strtolower ($ method ), array_map ('strtolower ' , (array ) $ handler ->exposeMixinMethods ())))
98
- {
99
- $ transport ->$ method ($ value );
100
- }
101
- }
102
- }
103
- }
104
- }
105
- $ this ->realtimeTransport = $ transport ;
106
-
107
- if (sfMailer::SPOOL == $ this ->strategy )
108
- {
109
- if (!isset ($ options ['spool_class ' ]))
110
- {
111
- throw new InvalidArgumentException ('For the spool mail delivery strategy, you must also define a spool_class option ' );
112
- }
113
- $ arguments = isset ($ options ['spool_arguments ' ]) ? $ options ['spool_arguments ' ] : array ();
114
-
115
- if ($ arguments )
116
- {
117
- $ r = new ReflectionClass ($ options ['spool_class ' ]);
118
- $ this ->spool = $ r ->newInstanceArgs ($ arguments );
119
- }
120
- else
121
- {
122
- $ this ->spool = new $ options ['spool_class ' ];
123
- }
124
-
125
- $ transport = new Swift_SpoolTransport ($ this ->spool );
126
- }
127
- elseif (sfMailer::SINGLE_ADDRESS == $ this ->strategy )
128
- {
129
- if (!isset ($ options ['delivery_address ' ]))
130
- {
131
- throw new InvalidArgumentException ('For the single_address mail delivery strategy, you must also define a delivery_address option ' );
132
- }
133
-
134
- $ this ->address = $ options ['delivery_address ' ];
135
-
136
- $ transport ->registerPlugin ($ this ->redirectingPlugin = new Swift_Plugins_RedirectingPlugin ($ this ->address ));
137
- }
138
-
139
- parent ::__construct ($ transport );
140
-
141
- // logger
142
- if ($ options ['logging ' ])
143
- {
144
- $ this ->logger = new sfMailerMessageLoggerPlugin ($ dispatcher );
145
-
146
- $ transport ->registerPlugin ($ this ->logger );
147
- }
148
-
149
- // preferences
150
- Swift_Preferences::getInstance ()->setCharset ($ options ['charset ' ]);
151
-
152
- $ dispatcher ->notify (new sfEvent ($ this , 'mailer.configure ' ));
153
- }
154
-
155
- /**
156
- * Gets the realtime transport instance.
157
- *
158
- * @return Swift_Transport The realtime transport instance.
159
- */
160
- public function getRealtimeTransport ()
161
- {
162
- return $ this ->realtimeTransport ;
163
- }
164
-
165
- /**
166
- * Sets the realtime transport instance.
167
- *
168
- * @param Swift_Transport $transport The realtime transport instance.
169
- */
170
- public function setRealtimeTransport (Swift_Transport $ transport )
171
- {
172
- $ this ->realtimeTransport = $ transport ;
173
- }
174
-
175
- /**
176
- * Gets the logger instance.
177
- *
178
- * @return sfMailerMessageLoggerPlugin The logger instance.
179
- */
180
- public function getLogger ()
181
- {
182
- return $ this ->logger ;
183
- }
184
-
185
- /**
186
- * Sets the logger instance.
187
- *
188
- * @param sfMailerMessageLoggerPlugin $logger The logger instance.
189
- */
190
- public function setLogger ($ logger )
191
- {
192
- $ this ->logger = $ logger ;
193
- }
194
-
195
- /**
196
- * Gets the delivery strategy.
197
- *
198
- * @return string The delivery strategy
199
- */
200
- public function getDeliveryStrategy ()
201
- {
202
- return $ this ->strategy ;
203
- }
204
-
205
- /**
206
- * Gets the delivery address.
207
- *
208
- * @return string The delivery address
209
- */
210
- public function getDeliveryAddress ()
211
- {
212
- return $ this ->address ;
213
- }
214
-
215
- /**
216
- * Sets the delivery address.
217
- *
218
- * @param string $address The delivery address
219
- */
220
- public function setDeliveryAddress ($ address )
221
- {
222
- $ this ->address = $ address ;
223
-
224
- if (sfMailer::SINGLE_ADDRESS == $ this ->strategy )
225
- {
226
- $ this ->redirectingPlugin ->setRecipient ($ address );
227
- }
228
- }
229
-
230
- /**
231
- * Creates a new message.
232
- *
233
- * @param string|array $from The from address
234
- * @param string|array $to The recipient(s)
235
- * @param string $subject The subject
236
- * @param string $body The body
237
- *
238
- * @return Swift_Message A Swift_Message instance
239
- */
240
- public function compose ($ from = null , $ to = null , $ subject = null , $ body = null )
241
- {
242
- $ msg = null ;
243
-
244
- if (version_compare (Swift::VERSION , '6.0.0 ' ) >= 0 ) {
245
- $ msg = new Swift_Message ($ subject );
246
- } else {
247
- $ msg = Swift_Message::newInstance ($ subject );
248
- }
249
-
250
- return $ msg
251
- ->setFrom ($ from )
252
- ->setTo ($ to )
253
- ->setBody ($ body )
254
- ;
255
- }
256
-
257
- /**
258
- * Sends a message.
259
- *
260
- * @param string|array $from The from address
261
- * @param string|array $to The recipient(s)
262
- * @param string $subject The subject
263
- * @param string $body The body
264
- *
265
- * @return int The number of sent emails
266
- */
267
- public function composeAndSend ($ from , $ to , $ subject , $ body )
268
- {
269
- return $ this ->send ($ this ->compose ($ from , $ to , $ subject , $ body ));
270
- }
271
-
272
- /**
273
- * Forces the next call to send() to use the realtime strategy.
274
- *
275
- * @return sfMailer The current sfMailer instance
276
- */
277
- public function sendNextImmediately ()
278
- {
279
- $ this ->force = true ;
280
-
281
- return $ this ;
282
- }
283
-
284
15
/**
285
16
* Sends the given message.
286
17
*
287
- * @param Swift_Message $message A transport instance
288
- * @param string[] &$failedRecipients An array of failures by-reference
18
+ * @param Swift_Mime_Message $message A transport instance
19
+ * @param string[] &$failedRecipients An array of failures by-reference
289
20
*
290
21
* @return int|false The number of sent emails
291
22
*/
292
- public function send (Swift_Message $ message , &$ failedRecipients = null )
23
+ public function send (Swift_Mime_Message $ message , &$ failedRecipients = null )
293
24
{
294
25
if ($ this ->force )
295
26
{
@@ -306,27 +37,18 @@ public function send(Swift_Message $message, &$failedRecipients = null)
306
37
return parent ::send ($ message , $ failedRecipients );
307
38
}
308
39
40
+
309
41
/**
310
- * Sends the current messages in the spool.
311
- *
312
- * The return value is the number of recipients who were accepted for delivery.
313
- *
314
- * @param string[] &$failedRecipients An array of failures by-reference
315
- *
316
- * @return int The number of sent emails
42
+ * @inheritDoc
317
43
*/
318
- public function flushQueue (&$ failedRecipients = null )
319
- {
320
- return $ this ->getSpool ()->flushQueue ($ this ->realtimeTransport , $ failedRecipients );
321
- }
322
-
323
- public function getSpool ()
44
+ public function compose ($ from = null , $ to = null , $ subject = null , $ body = null )
324
45
{
325
- if (self ::SPOOL != $ this ->strategy )
326
- {
327
- throw new LogicException (sprintf ('You can only send messages in the spool if the delivery strategy is "spool" (%s is the current strategy). ' , $ this ->strategy ));
328
- }
46
+ $ msg = Swift_Message::newInstance ($ subject );
329
47
330
- return $ this ->spool ;
48
+ return $ msg
49
+ ->setFrom ($ from )
50
+ ->setTo ($ to )
51
+ ->setBody ($ body )
52
+ ;
331
53
}
332
54
}
0 commit comments