1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of SwiftMailer. |
5
|
|
|
* (c) 2011 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
|
|
|
/** |
12
|
|
|
* Stores Messages in memory. |
13
|
|
|
* |
14
|
|
|
* @author Fabien Potencier |
15
|
|
|
*/ |
16
|
|
|
class Swift_MemorySpool implements Swift_Spool |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $messages = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $flushRetries = 3; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Tests if this Transport mechanism has started. |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function isStarted() |
34
|
|
|
{ |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Starts this Transport mechanism. |
40
|
|
|
*/ |
41
|
|
|
public function start() |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Stops this Transport mechanism. |
47
|
|
|
*/ |
48
|
|
|
public function stop() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $retries |
54
|
|
|
*/ |
55
|
|
|
public function setFlushRetries($retries) |
56
|
|
|
{ |
57
|
|
|
$this->flushRetries = $retries; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Stores a message in the queue. |
62
|
|
|
* |
63
|
|
|
* @param Swift_Mime_Message $message The message to store |
64
|
|
|
* |
65
|
|
|
* @return bool Whether the operation has succeeded |
66
|
|
|
*/ |
67
|
2 |
|
public function queueMessage(Swift_Mime_Message $message) |
68
|
|
|
{ |
69
|
|
|
//clone the message to make sure it is not changed while in the queue |
70
|
2 |
|
$this->messages[] = clone $message; |
71
|
|
|
|
72
|
2 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sends messages using the given transport instance. |
77
|
|
|
* |
78
|
|
|
* @param Swift_Transport $transport A transport instance |
79
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
80
|
|
|
* |
81
|
|
|
* @return int The number of sent emails |
82
|
|
|
*/ |
83
|
2 |
|
public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) |
84
|
|
|
{ |
85
|
2 |
|
if (!$this->messages) { |
|
|
|
|
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
if (!$transport->isStarted()) { |
90
|
|
|
$transport->start(); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
$count = 0; |
94
|
2 |
|
$retries = $this->flushRetries; |
95
|
2 |
|
while ($retries--) { |
96
|
|
|
try { |
97
|
2 |
|
while ($message = array_pop($this->messages)) { |
98
|
2 |
|
$count += $transport->send($message, $failedRecipients); |
99
|
2 |
|
} |
100
|
2 |
|
} catch (Swift_TransportException $exception) { |
101
|
|
|
if ($retries) { |
102
|
|
|
// re-queue the message at the end of the queue to give a chance |
103
|
|
|
// to the other messages to be sent, in case the failure was due to |
104
|
|
|
// this message and not just the transport failing |
105
|
|
|
array_unshift($this->messages, $message); |
|
|
|
|
106
|
|
|
// wait half a second before we try again |
107
|
|
|
usleep(500000); |
108
|
|
|
} else { |
109
|
|
|
throw $exception; |
110
|
|
|
} |
111
|
|
|
} |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
return $count; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.