Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
50 / 50 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Queue | |
100.00% |
50 / 50 |
|
100.00% |
11 / 11 |
28 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| setRecipients | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| addRecipients | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addRecipient | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| setMessages | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| addMessages | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRecipients | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPreparedMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prepare | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Mail; |
| 15 | |
| 16 | /** |
| 17 | * Mail queue class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Mail |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.0.7 |
| 25 | */ |
| 26 | class Queue |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Recipients |
| 31 | * @var array |
| 32 | */ |
| 33 | protected array $recipients = []; |
| 34 | |
| 35 | /** |
| 36 | * Messages |
| 37 | * @var array |
| 38 | */ |
| 39 | protected array $messages = []; |
| 40 | |
| 41 | /** |
| 42 | * Prepared messages |
| 43 | * @var array |
| 44 | */ |
| 45 | protected array $prepared = []; |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * Instantiate the mail queue object |
| 51 | */ |
| 52 | public function __construct() |
| 53 | { |
| 54 | $args = func_get_args(); |
| 55 | |
| 56 | foreach ($args as $arg) { |
| 57 | if ($arg instanceof Message) { |
| 58 | $this->addMessage($arg); |
| 59 | } else if (is_array($arg)) { |
| 60 | $keys = array_keys($arg); |
| 61 | if (is_numeric($keys[0])) { |
| 62 | $this->addRecipients($arg); |
| 63 | } else { |
| 64 | $this->addRecipient($arg); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set (and clear) recipients in the queue |
| 72 | * |
| 73 | * @param array $recipients |
| 74 | * @return Queue |
| 75 | */ |
| 76 | public function setRecipients(array $recipients): Queue |
| 77 | { |
| 78 | $this->recipients = []; |
| 79 | foreach ($recipients as $recipient) { |
| 80 | $this->addRecipient($recipient); |
| 81 | } |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Add recipients to the queue |
| 87 | * |
| 88 | * @param array $recipients |
| 89 | * @return Queue |
| 90 | */ |
| 91 | public function addRecipients(array $recipients): Queue |
| 92 | { |
| 93 | foreach ($recipients as $recipient) { |
| 94 | $this->addRecipient($recipient); |
| 95 | } |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add a recipient to the queue |
| 101 | * |
| 102 | * $recipient = [ |
| 103 | * 'email' => 'me@domain.com', // Required |
| 104 | * 'name' => 'My Name', // Everything else is optional for individual message customization |
| 105 | * 'company' => 'My Company' |
| 106 | * ] |
| 107 | * |
| 108 | * @param array $recipient |
| 109 | * @throws Exception |
| 110 | * @return Queue |
| 111 | */ |
| 112 | public function addRecipient(array $recipient): Queue |
| 113 | { |
| 114 | if (!isset($recipient['email'])) { |
| 115 | throw new Exception("Error: The recipient's array must contain at least an 'email' key."); |
| 116 | } |
| 117 | if (!in_array($recipient, $this->recipients, true)) { |
| 118 | $this->recipients[] = $recipient; |
| 119 | } |
| 120 | |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set (and clear) messages in the queue |
| 126 | * |
| 127 | * @param array $messages |
| 128 | * @return Queue |
| 129 | */ |
| 130 | public function setMessages(array $messages): Queue |
| 131 | { |
| 132 | $this->messages = []; |
| 133 | foreach ($messages as $message) { |
| 134 | $this->addMessage($message); |
| 135 | } |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Add messages to the queue |
| 141 | * |
| 142 | * @param array $messages |
| 143 | * @return Queue |
| 144 | */ |
| 145 | public function addMessages(array $messages): Queue |
| 146 | { |
| 147 | foreach ($messages as $message) { |
| 148 | $this->addMessage($message); |
| 149 | } |
| 150 | return $this; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Add a message to the queue |
| 155 | * |
| 156 | * @param Message $message |
| 157 | * @return Queue |
| 158 | */ |
| 159 | public function addMessage(Message $message): Queue |
| 160 | { |
| 161 | if (!in_array($message, $this->messages, true)) { |
| 162 | $this->messages[] = $message; |
| 163 | } |
| 164 | return $this; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get messages |
| 169 | * |
| 170 | * @return array |
| 171 | */ |
| 172 | public function getMessages(): array |
| 173 | { |
| 174 | return $this->messages; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get recipients |
| 179 | * |
| 180 | * @return array |
| 181 | */ |
| 182 | public function getRecipients(): array |
| 183 | { |
| 184 | return $this->recipients; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get prepared messages |
| 189 | * |
| 190 | * @return array |
| 191 | */ |
| 192 | public function getPreparedMessages(): array |
| 193 | { |
| 194 | return $this->prepared; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Prepare queue for sending messages |
| 199 | * |
| 200 | * @return array |
| 201 | */ |
| 202 | public function prepare(): array |
| 203 | { |
| 204 | foreach ($this->recipients as $recipient) { |
| 205 | foreach ($this->messages as $message) { |
| 206 | $to = (isset($recipient['name'])) ? [$recipient['email'] => $recipient['name']] : $recipient['email']; |
| 207 | |
| 208 | $msg = clone $message; |
| 209 | $msg->setTo($to); |
| 210 | |
| 211 | foreach ($msg->getParts() as $i => $part) { |
| 212 | if (!($part instanceof Message\Attachment)) { |
| 213 | $subject = $msg->getSubject(); |
| 214 | $content = $part->getContent(); |
| 215 | foreach ($recipient as $key => $value) { |
| 216 | $subject = str_replace('[{' . $key . '}]', $value, $subject); |
| 217 | $content = str_replace('[{' . $key . '}]', $value, $content); |
| 218 | } |
| 219 | $msg->setSubject($subject); |
| 220 | $msg->getPart($i)->setContent($content); |
| 221 | } |
| 222 | } |
| 223 | $this->prepared[] = $msg; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return $this->prepared; |
| 228 | } |
| 229 | |
| 230 | } |