1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sendmail package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2010, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Sendmail; |
11
|
|
|
|
12
|
|
|
use Sendmail\Sender\SenderInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* E-mail queue. |
16
|
|
|
* |
17
|
|
|
* @author Peter Gribanov <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Queue implements \IteratorAggregate, \Countable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* List messages. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $messages = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Mail sender. |
30
|
|
|
* |
31
|
|
|
* @var SenderInterface |
32
|
|
|
*/ |
33
|
|
|
protected $sender; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param SenderInterface $sender |
37
|
|
|
*/ |
38
|
18 |
|
public function __construct(SenderInterface $sender) |
39
|
|
|
{ |
40
|
18 |
|
$this->sender = $sender; |
41
|
18 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get iterator. |
45
|
|
|
* |
46
|
|
|
* @return \ArrayIterator |
47
|
|
|
*/ |
48
|
17 |
|
public function getIterator() |
49
|
|
|
{ |
50
|
17 |
|
return new \ArrayIterator($this->messages); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Возвращает количество сообщений. |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
11 |
|
public function count() |
59
|
|
|
{ |
60
|
11 |
|
return count($this->messages); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Clear list messages. |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
4 |
|
public function clear() |
69
|
|
|
{ |
70
|
4 |
|
unset($this->messages); |
71
|
4 |
|
$this->messages = array(); |
72
|
|
|
|
73
|
4 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add message. |
78
|
|
|
* |
79
|
|
|
* @param Message $message |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
14 |
|
public function add(Message $message) |
84
|
|
|
{ |
85
|
14 |
|
$this->messages[] = clone $message; |
86
|
|
|
|
87
|
14 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get sender. |
92
|
|
|
* |
93
|
|
|
* @return SenderInterface |
94
|
|
|
*/ |
95
|
1 |
|
public function getSender() |
96
|
|
|
{ |
97
|
1 |
|
return $this->sender; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add a message addressed to list recipients. |
102
|
|
|
* |
103
|
|
|
* @param array $recipients |
104
|
|
|
* @param Message $message |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
7 |
|
public function notify(array $recipients, Message $message) |
109
|
|
|
{ |
110
|
7 |
|
$message = clone $message; |
111
|
7 |
|
foreach ($recipients as $recipient) { |
112
|
6 |
|
$this->add($message->setTo($recipient)); |
113
|
|
|
} |
114
|
|
|
|
115
|
7 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Send all messages. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
5 |
|
public function send() |
124
|
|
|
{ |
125
|
5 |
|
foreach ($this as $message) { |
126
|
4 |
|
if (!$this->sender->send($message)) { |
127
|
4 |
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|