| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
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 |
/**
|
|
|
12 |
* sfMailer is the main entry point for the mailer system.
|
|
|
13 |
*
|
|
|
14 |
* This class is instanciated by sfContext on demand.
|
|
|
15 |
*
|
|
|
16 |
* @package symfony
|
|
|
17 |
* @subpackage mailer
|
|
|
18 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
19 |
* @version SVN: $Id: sfMailer.class.php 28841 2010-03-29 08:13:57Z fabien $
|
|
|
20 |
*/
|
|
|
21 |
class sfMailer extends Swift_Mailer
|
|
|
22 |
{
|
|
|
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' => '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 |
// transport
|
|
|
77 |
$class = $options['transport']['class'];
|
|
|
78 |
$transport = new $class();
|
|
|
79 |
if (isset($options['transport']['param']))
|
|
|
80 |
{
|
|
|
81 |
foreach ($options['transport']['param'] as $key => $value)
|
|
|
82 |
{
|
|
|
83 |
$method = 'set'.ucfirst($key);
|
|
|
84 |
if (method_exists($transport, $method))
|
|
|
85 |
{
|
|
|
86 |
$transport->$method($value);
|
|
|
87 |
}
|
|
|
88 |
elseif (method_exists($transport, 'getExtensionHandlers'))
|
|
|
89 |
{
|
|
|
90 |
foreach ($transport->getExtensionHandlers() as $handler)
|
|
|
91 |
{
|
|
|
92 |
if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods())))
|
|
|
93 |
{
|
|
|
94 |
$transport->$method($value);
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
$this->realtimeTransport = $transport;
|
|
|
101 |
|
|
|
102 |
if (sfMailer::SPOOL == $this->strategy)
|
|
|
103 |
{
|
|
|
104 |
if (!isset($options['spool_class']))
|
|
|
105 |
{
|
|
|
106 |
throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option');
|
|
|
107 |
}
|
|
|
108 |
$arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array();
|
|
|
109 |
|
|
|
110 |
if ($arguments)
|
|
|
111 |
{
|
|
|
112 |
$r = new ReflectionClass($options['spool_class']);
|
|
|
113 |
$this->spool = $r->newInstanceArgs($arguments);
|
|
|
114 |
}
|
|
|
115 |
else
|
|
|
116 |
{
|
|
|
117 |
$this->spool = new $options['spool_class'];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$transport = new Swift_SpoolTransport($this->spool);
|
|
|
121 |
}
|
|
|
122 |
elseif (sfMailer::SINGLE_ADDRESS == $this->strategy)
|
|
|
123 |
{
|
|
|
124 |
if (!isset($options['delivery_address']))
|
|
|
125 |
{
|
|
|
126 |
throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$this->address = $options['delivery_address'];
|
|
|
130 |
|
|
|
131 |
$transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address));
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
parent::__construct($transport);
|
|
|
135 |
|
|
|
136 |
// logger
|
|
|
137 |
if ($options['logging'])
|
|
|
138 |
{
|
|
|
139 |
$this->logger = new sfMailerMessageLoggerPlugin($dispatcher);
|
|
|
140 |
|
|
|
141 |
$transport->registerPlugin($this->logger);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
if (sfMailer::NONE == $this->strategy)
|
|
|
145 |
{
|
|
|
146 |
// must be registered after logging
|
|
|
147 |
$transport->registerPlugin(new Swift_Plugins_BlackholePlugin());
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// preferences
|
|
|
151 |
Swift_Preferences::getInstance()->setCharset($options['charset']);
|
|
|
152 |
|
|
|
153 |
$dispatcher->notify(new sfEvent($this, 'mailer.configure'));
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Gets the realtime transport instance.
|
|
|
158 |
*
|
|
|
159 |
* @return Swift_Transport The realtime transport instance.
|
|
|
160 |
*/
|
|
|
161 |
public function getRealtimeTransport()
|
|
|
162 |
{
|
|
|
163 |
return $this->realtimeTransport;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Sets the realtime transport instance.
|
|
|
168 |
*
|
|
|
169 |
* @param Swift_Transport $transport The realtime transport instance.
|
|
|
170 |
*/
|
|
|
171 |
public function setRealtimeTransport(Swift_Transport $transport)
|
|
|
172 |
{
|
|
|
173 |
$this->realtimeTransport = $transport;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Gets the logger instance.
|
|
|
178 |
*
|
|
|
179 |
* @return sfMailerMessageLoggerPlugin The logger instance.
|
|
|
180 |
*/
|
|
|
181 |
public function getLogger()
|
|
|
182 |
{
|
|
|
183 |
return $this->logger;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Sets the logger instance.
|
|
|
188 |
*
|
|
|
189 |
* @param sfMailerMessageLoggerPlugin $logger The logger instance.
|
|
|
190 |
*/
|
|
|
191 |
public function setLogger($logger)
|
|
|
192 |
{
|
|
|
193 |
$this->logger = $logger;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Gets the delivery strategy.
|
|
|
198 |
*
|
|
|
199 |
* @return string The delivery strategy
|
|
|
200 |
*/
|
|
|
201 |
public function getDeliveryStrategy()
|
|
|
202 |
{
|
|
|
203 |
return $this->strategy;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Gets the delivery address.
|
|
|
208 |
*
|
|
|
209 |
* @return string The delivery address
|
|
|
210 |
*/
|
|
|
211 |
public function getDeliveryAddress()
|
|
|
212 |
{
|
|
|
213 |
return $this->address;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
/**
|
|
|
217 |
* Sets the delivery address.
|
|
|
218 |
*
|
|
|
219 |
* @param string $address The delivery address
|
|
|
220 |
*/
|
|
|
221 |
public function setDeliveryAddress($address)
|
|
|
222 |
{
|
|
|
223 |
$this->address = $address;
|
|
|
224 |
|
|
|
225 |
if (sfMailer::SINGLE_ADDRESS == $this->strategy)
|
|
|
226 |
{
|
|
|
227 |
$this->redirectingPlugin->setRecipient($address);
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Creates a new message.
|
|
|
233 |
*
|
|
|
234 |
* @param string|array $from The from address
|
|
|
235 |
* @param string|array $to The recipient(s)
|
|
|
236 |
* @param string $subject The subject
|
|
|
237 |
* @param string $body The body
|
|
|
238 |
*
|
|
|
239 |
* @return Swift_Message A Swift_Message instance
|
|
|
240 |
*/
|
|
|
241 |
public function compose($from = null, $to = null, $subject = null, $body = null)
|
|
|
242 |
{
|
|
|
243 |
return Swift_Message::newInstance()
|
|
|
244 |
->setFrom($from)
|
|
|
245 |
->setTo($to)
|
|
|
246 |
->setSubject($subject)
|
|
|
247 |
->setBody($body)
|
|
|
248 |
;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Sends a message.
|
|
|
253 |
*
|
|
|
254 |
* @param string|array $from The from address
|
|
|
255 |
* @param string|array $to The recipient(s)
|
|
|
256 |
* @param string $subject The subject
|
|
|
257 |
* @param string $body The body
|
|
|
258 |
*
|
|
|
259 |
* @return int The number of sent emails
|
|
|
260 |
*/
|
|
|
261 |
public function composeAndSend($from, $to, $subject, $body)
|
|
|
262 |
{
|
|
|
263 |
return $this->send($this->compose($from, $to, $subject, $body));
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Forces the next call to send() to use the realtime strategy.
|
|
|
268 |
*
|
|
|
269 |
* @return sfMailer The current sfMailer instance
|
|
|
270 |
*/
|
|
|
271 |
public function sendNextImmediately()
|
|
|
272 |
{
|
|
|
273 |
$this->force = true;
|
|
|
274 |
|
|
|
275 |
return $this;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Sends the given message.
|
|
|
280 |
*
|
|
|
281 |
* @param Swift_Transport $transport A transport instance
|
|
|
282 |
* @param string[] &$failedRecipients An array of failures by-reference
|
|
|
283 |
*
|
|
|
284 |
* @return int|false The number of sent emails
|
|
|
285 |
*/
|
|
|
286 |
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
|
|
|
287 |
{
|
|
|
288 |
if ($this->force)
|
|
|
289 |
{
|
|
|
290 |
$this->force = false;
|
|
|
291 |
|
|
|
292 |
if (!$this->realtimeTransport->isStarted())
|
|
|
293 |
{
|
|
|
294 |
$this->realtimeTransport->start();
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
return $this->realtimeTransport->send($message, $failedRecipients);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return parent::send($message, $failedRecipients);
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Sends the current messages in the spool.
|
|
|
305 |
*
|
|
|
306 |
* The return value is the number of recipients who were accepted for delivery.
|
|
|
307 |
*
|
|
|
308 |
* @param string[] &$failedRecipients An array of failures by-reference
|
|
|
309 |
*
|
|
|
310 |
* @return int The number of sent emails
|
|
|
311 |
*/
|
|
|
312 |
public function flushQueue(&$failedRecipients = null)
|
|
|
313 |
{
|
|
|
314 |
return $this->getSpool()->flushQueue($this->realtimeTransport, $failedRecipients);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
public function getSpool()
|
|
|
318 |
{
|
|
|
319 |
if (self::SPOOL != $this->strategy)
|
|
|
320 |
{
|
|
|
321 |
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));
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
return $this->spool;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
static public function initialize()
|
|
|
328 |
{
|
|
|
329 |
require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/swift_init.php';
|
|
|
330 |
}
|
|
|
331 |
}
|