1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Charcoal\Email; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
// From 'pimple/pimple' |
11
|
|
|
use Pimple\Container; |
12
|
|
|
|
13
|
|
|
// From 'locomotivemtl/charcoal-factory' |
14
|
|
|
use Charcoal\Factory\FactoryInterface; |
15
|
|
|
|
16
|
|
|
// From 'locomotivemtl/charcoal-core' |
17
|
|
|
use Charcoal\Model\AbstractModel; |
18
|
|
|
|
19
|
|
|
// From 'locomotivemtl/charcoal-queue' |
20
|
|
|
use Charcoal\Queue\QueueItemInterface; |
21
|
|
|
use Charcoal\Queue\QueueItemTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Email queue item. |
25
|
|
|
*/ |
26
|
|
|
class EmailQueueItem extends AbstractModel implements QueueItemInterface |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
use QueueItemTrait; |
29
|
|
|
use EmailAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The recipient's email address. |
33
|
|
|
* |
34
|
|
|
* @var string $to |
35
|
|
|
*/ |
36
|
|
|
private $to; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The sender's email address. |
40
|
|
|
* |
41
|
|
|
* @var string $from |
42
|
|
|
*/ |
43
|
|
|
private $from; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The email subject. |
47
|
|
|
* |
48
|
|
|
* @var string $subject. |
49
|
|
|
*/ |
50
|
|
|
private $subject; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The HTML message body. |
54
|
|
|
* |
55
|
|
|
* @var string $msgHtml |
56
|
|
|
*/ |
57
|
|
|
private $msgHtml; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The plain-text message body. |
61
|
|
|
* |
62
|
|
|
* @var string $msgTxt |
63
|
|
|
*/ |
64
|
|
|
private $msgTxt; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The campaign ID. |
68
|
|
|
* |
69
|
|
|
* @var string $campaign |
70
|
|
|
*/ |
71
|
|
|
private $campaign; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var FactoryInterface $emailFactory |
75
|
|
|
*/ |
76
|
|
|
private $emailFactory; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the primary key that uniquely identifies each queue item. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function key() |
84
|
|
|
{ |
85
|
|
|
return 'id'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the recipient's email address. |
90
|
|
|
* |
91
|
|
|
* @param string|array $email An email address. |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function setTo($email) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->to = $this->parseEmail($email); |
98
|
|
|
} catch (Exception $e) { |
99
|
|
|
$this->logger->warning(sprintf('Invalid "to" email: "%s"', strval($email))); |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the recipient's email address. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function to() |
110
|
|
|
{ |
111
|
|
|
return $this->to; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the sender's email address. |
116
|
|
|
* |
117
|
|
|
* @param string|array $email An email address. |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
|
|
public function setFrom($email) |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$this->from = $this->parseEmail($email); |
124
|
|
|
} catch (Exception $e) { |
125
|
|
|
$this->logger->warning(sprintf('Invalid "from" email: "%s"', strval($email))); |
126
|
|
|
} |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the sender's email address. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function from() |
136
|
|
|
{ |
137
|
|
|
return $this->from; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the email subject. |
142
|
|
|
* |
143
|
|
|
* @param string $subject The email subject. |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
|
|
public function setSubject($subject) |
147
|
|
|
{ |
148
|
|
|
$this->subject = $subject; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the email subject. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function subject() |
158
|
|
|
{ |
159
|
|
|
return $this->subject; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the email's HTML message body. |
164
|
|
|
* |
165
|
|
|
* @param string $body The HTML message body. |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
|
|
public function setMsgHtml($body) |
169
|
|
|
{ |
170
|
|
|
$this->msgHtml = $body; |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the email's HTML message body. |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function msgHtml() |
180
|
|
|
{ |
181
|
|
|
return $this->msgHtml; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set the email's plain-text message body. |
186
|
|
|
* |
187
|
|
|
* @param string $body The plain-text mesage body. |
188
|
|
|
* @return self |
189
|
|
|
*/ |
190
|
|
|
public function setMsgTxt($body) |
191
|
|
|
{ |
192
|
|
|
$this->msgTxt = $body; |
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the email's plain-text message body. |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function msgTxt() |
202
|
|
|
{ |
203
|
|
|
return $this->msgTxt; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set the campaign ID. |
208
|
|
|
* |
209
|
|
|
* @param string $campaign The campaign identifier. |
210
|
|
|
* @return self |
211
|
|
|
*/ |
212
|
|
|
public function setCampaign($campaign) |
213
|
|
|
{ |
214
|
|
|
$this->campaign = $campaign; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the campaign ID. |
220
|
|
|
* |
221
|
|
|
* If it has not been explicitely set, it will be auto-generated (with uniqid). |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function campaign() |
226
|
|
|
{ |
227
|
|
|
return $this->campaign; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Process the item. |
232
|
|
|
* |
233
|
|
|
* @param callable $alwaysCallback An optional callback routine executed after the item is processed. |
234
|
|
|
* @param callable $successCallback An optional callback routine executed when the item is resolved. |
235
|
|
|
* @param callable $failureCallback An optional callback routine executed when the item is rejected. |
236
|
|
|
* @return boolean|null Returns TRUE i this item was successfully processed, |
237
|
|
|
* FALSE on failure or if an error occurs, NULL if this item is already processed. |
238
|
|
|
*/ |
239
|
|
|
public function process( |
240
|
|
|
callable $alwaysCallback = null, |
241
|
|
|
callable $successCallback = null, |
242
|
|
|
callable $failureCallback = null |
243
|
|
|
): ?bool { |
244
|
|
|
if ($this->processed() === true) { |
245
|
|
|
// Do not process twice, ever. |
246
|
|
|
return null; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$email = $this->emailFactory()->create('email'); |
250
|
|
|
$email->setData($this->data()); |
251
|
|
|
|
252
|
|
|
try { |
253
|
|
|
$result = $email->send(); |
254
|
|
|
if ($result === true) { |
255
|
|
|
$this->setProcessed(true); |
256
|
|
|
$this->setProcessedDate('now'); |
257
|
|
|
$this->setMsgHtml(null); |
258
|
|
|
$this->setMsgTxt(null); |
259
|
|
|
$this->update([ |
260
|
|
|
'processed', |
261
|
|
|
'processed_date', |
262
|
|
|
'msg_html', |
263
|
|
|
'msg_txt', |
264
|
|
|
]); |
265
|
|
|
|
266
|
|
|
if ($successCallback !== null) { |
267
|
|
|
$successCallback($this); |
268
|
|
|
} |
269
|
|
|
} else { |
270
|
|
|
if ($failureCallback !== null) { |
271
|
|
|
$failureCallback($this); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $result; |
276
|
|
|
} catch (Exception $e) { |
277
|
|
|
// Todo log error |
278
|
|
|
if ($failureCallback !== null) { |
279
|
|
|
$failureCallback($this); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return false; |
283
|
|
|
} finally { |
284
|
|
|
if ($alwaysCallback !== null) { |
285
|
|
|
$alwaysCallback($this); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param Container $container Pimple DI container. |
292
|
|
|
* @return void |
293
|
|
|
*/ |
294
|
|
|
protected function setDependencies(Container $container): void |
295
|
|
|
{ |
296
|
|
|
parent::setDependencies($container); |
297
|
|
|
$this->setEmailFactory($container['email/factory']); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Hook called before saving the item. |
302
|
|
|
* |
303
|
|
|
* @return boolean |
304
|
|
|
* @see \Charcoal\Queue\QueueItemTrait::preSaveQueueItem() |
305
|
|
|
*/ |
306
|
|
|
protected function preSave(): bool |
307
|
|
|
{ |
308
|
|
|
parent::preSave(); |
309
|
|
|
|
310
|
|
|
$this->preSaveQueueItem(); |
311
|
|
|
|
312
|
|
|
return true; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return FactoryInterface |
317
|
|
|
*/ |
318
|
|
|
protected function emailFactory(): FactoryInterface |
319
|
|
|
{ |
320
|
|
|
return $this->emailFactory; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param FactoryInterface $factory The factory to create email objects. |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
private function setEmailFactory(FactoryInterface $factory): void |
328
|
|
|
{ |
329
|
|
|
$this->emailFactory = $factory; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|