1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Charcoal\Email; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
// From 'charcoal-core' |
10
|
|
|
use Charcoal\Config\AbstractConfig; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Email configuration. |
14
|
|
|
*/ |
15
|
|
|
class EmailConfig extends AbstractConfig |
16
|
|
|
{ |
17
|
|
|
use EmailAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Whether SMTP should be used. |
21
|
|
|
* |
22
|
|
|
* @var boolean $smtp |
23
|
|
|
*/ |
24
|
|
|
private $smtp = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The SMTP hostname. |
28
|
|
|
* |
29
|
|
|
* @var string $smtpHostname |
30
|
|
|
*/ |
31
|
|
|
private $smtpHostname; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The SMTP port. |
35
|
|
|
* |
36
|
|
|
* @var integer $smtpPort |
37
|
|
|
*/ |
38
|
|
|
private $smtpPort; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The SMTP security type. |
42
|
|
|
* |
43
|
|
|
* @var string $smtpSecurity |
44
|
|
|
*/ |
45
|
|
|
private $smtpSecurity = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whether SMTP requires authentication. |
49
|
|
|
* |
50
|
|
|
* @var boolean $smtpAuth |
51
|
|
|
*/ |
52
|
|
|
private $smtpAuth; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The SMTP username. |
56
|
|
|
* |
57
|
|
|
* @var string $smtpUsername |
58
|
|
|
*/ |
59
|
|
|
private $smtpUsername; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The SMTP password. |
63
|
|
|
* |
64
|
|
|
* @var string $smtpPassword |
65
|
|
|
*/ |
66
|
|
|
private $smtpPassword; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The default sender's email address. |
70
|
|
|
* |
71
|
|
|
* @var string $defaultFrom |
72
|
|
|
*/ |
73
|
|
|
private $defaultFrom; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The default "Reply-To" email address. |
77
|
|
|
* |
78
|
|
|
* @var string $defaultReplyTo |
79
|
|
|
*/ |
80
|
|
|
private $defaultReplyTo; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Whether the email (open) should be tracked by default. |
84
|
|
|
* |
85
|
|
|
* @var boolean $defaultTrack |
86
|
|
|
*/ |
87
|
|
|
private $defaultTrackOpenEnabled; |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Whether the email (links) should be tracked by default. |
92
|
|
|
* |
93
|
|
|
* @var boolean $defaultTrack |
94
|
|
|
*/ |
95
|
|
|
private $defaultTrackLinksEnabled; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Whether the email should be logged by default. |
99
|
|
|
* |
100
|
|
|
* @var boolean $defaultLog |
101
|
|
|
*/ |
102
|
|
|
private $defaultLogEnabled; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Default email configuration. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function defaults(): array |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'smtp' => false, |
113
|
|
|
|
114
|
|
|
'default_from' => '', |
115
|
|
|
'default_reply_to' => '', |
116
|
|
|
|
117
|
|
|
'default_track_open_enabled' => true, |
118
|
|
|
'default_track_links_enabled' => true, |
119
|
|
|
'default_log_enabled' => true |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set whether SMTP should be used for sending the email. |
125
|
|
|
* |
126
|
|
|
* @param boolean $smtp If the email should be sent using SMTP or not. |
127
|
|
|
* @throws InvalidArgumentException If the SMTP state is not a boolean. |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
|
|
public function setSmtp($smtp) |
131
|
|
|
{ |
132
|
|
|
$this->smtp = !!$smtp; |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Determine if SMTP should be used. |
138
|
|
|
* |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
|
|
public function smtp() |
142
|
|
|
{ |
143
|
|
|
return $this->smtp; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the SMTP hostname to be used. |
148
|
|
|
* |
149
|
|
|
* @param string $hostname The SMTP hostname. |
150
|
|
|
* @throws InvalidArgumentException If the SMTP hostname is not a string. |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
|
|
public function setSmtpHostname($hostname) |
154
|
|
|
{ |
155
|
|
|
if (!is_string($hostname)) { |
156
|
|
|
throw new InvalidArgumentException( |
157
|
|
|
'SMTP Hostname must be a string.' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->smtpHostname = $hostname; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the SMTP hostname. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function smtpHostname() |
172
|
|
|
{ |
173
|
|
|
return $this->smtpHostname; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set the SMTP port to be used. |
178
|
|
|
* |
179
|
|
|
* @param integer $port The SMTP port. |
180
|
|
|
* @throws InvalidArgumentException If the SMTP port is not an integer. |
181
|
|
|
* @return self |
182
|
|
|
*/ |
183
|
|
|
public function setSmtpPort($port) |
184
|
|
|
{ |
185
|
|
|
if (!is_int($port)) { |
186
|
|
|
throw new InvalidArgumentException( |
187
|
|
|
'SMTP Port must be an integer.' |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->smtpPort = $port; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the SMTP port. |
198
|
|
|
* |
199
|
|
|
* @return integer |
200
|
|
|
*/ |
201
|
|
|
public function smtpPort() |
202
|
|
|
{ |
203
|
|
|
return $this->smtpPort; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set whether SMTP requires authentication. |
208
|
|
|
* |
209
|
|
|
* @param boolean $auth The SMTP authentication flag (if auth is required). |
210
|
|
|
* @return self |
211
|
|
|
*/ |
212
|
|
|
public function setSmtpAuth($auth) |
213
|
|
|
{ |
214
|
|
|
$this->smtpAuth = !!$auth; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Determine if SMTP requires authentication. |
220
|
|
|
* |
221
|
|
|
* @return boolean |
222
|
|
|
*/ |
223
|
|
|
public function smtpAuth() |
224
|
|
|
{ |
225
|
|
|
return $this->smtpAuth; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set the SMTP username to be used. |
230
|
|
|
* |
231
|
|
|
* @param string $username The SMTP username, if using authentication. |
232
|
|
|
* @throws InvalidArgumentException If the SMTP username is not a string. |
233
|
|
|
* @return self |
234
|
|
|
*/ |
235
|
|
|
public function setSmtpUsername($username) |
236
|
|
|
{ |
237
|
|
|
if (!is_string($username)) { |
238
|
|
|
throw new InvalidArgumentException( |
239
|
|
|
'SMTP Username must be a string.' |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->smtpUsername = $username; |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the SMTP username. |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function smtpUsername() |
254
|
|
|
{ |
255
|
|
|
return $this->smtpUsername; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set the SMTP password to be used. |
260
|
|
|
* |
261
|
|
|
* @param string $password The SMTP password, if using authentication. |
262
|
|
|
* @throws InvalidArgumentException If the SMTP password is not a string. |
263
|
|
|
* @return self |
264
|
|
|
*/ |
265
|
|
|
public function setSmtpPassword($password) |
266
|
|
|
{ |
267
|
|
|
if (!is_string($password)) { |
268
|
|
|
throw new InvalidArgumentException( |
269
|
|
|
'SMTP Password must be a string.' |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$this->smtpPassword = $password; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get the SMTP password. |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function smtpPassword() |
284
|
|
|
{ |
285
|
|
|
return $this->smtpPassword; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Set the SMTP security type to be used. |
290
|
|
|
* |
291
|
|
|
* @param string $security The SMTP security type (empty, "TLS", or "SSL"). |
292
|
|
|
* @throws InvalidArgumentException If the security type is not valid (empty, "TLS", or "SSL"). |
293
|
|
|
* @return self |
294
|
|
|
*/ |
295
|
|
|
public function setSmtpSecurity($security) |
296
|
|
|
{ |
297
|
|
|
$security = strtoupper($security); |
298
|
|
|
$validSecurity = [ '', 'TLS', 'SSL' ]; |
299
|
|
|
|
300
|
|
|
if (!in_array($security, $validSecurity)) { |
301
|
|
|
throw new InvalidArgumentException( |
302
|
|
|
'SMTP Security is not valid. Must be "", "TLS" or "SSL".' |
303
|
|
|
); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$this->smtpSecurity = $security; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get the SMTP security type. |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function smtpSecurity() |
317
|
|
|
{ |
318
|
|
|
return $this->smtpSecurity; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set the default sender's email address. |
323
|
|
|
* |
324
|
|
|
* @param string|array $email The default "From" email address. |
325
|
|
|
* @return self |
326
|
|
|
*/ |
327
|
|
|
public function setDefaultFrom($email) |
328
|
|
|
{ |
329
|
|
|
$this->defaultFrom = $this->parseEmail($email); |
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the sender email address. |
335
|
|
|
* |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
|
|
public function defaultFrom() |
339
|
|
|
{ |
340
|
|
|
return $this->defaultFrom; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the default "Reply-To" email address. |
345
|
|
|
* |
346
|
|
|
* @param string|array $email The default "Reply-To" email address. |
347
|
|
|
* @return self |
348
|
|
|
*/ |
349
|
|
|
public function setDefaultReplyTo($email) |
350
|
|
|
{ |
351
|
|
|
$this->defaultReplyTo = $this->parseEmail($email); |
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Get the "Reply-To" email address. |
357
|
|
|
* |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public function defaultReplyTo() |
361
|
|
|
{ |
362
|
|
|
return $this->defaultReplyTo; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Set whether the email sending should be logged by default. |
367
|
|
|
* |
368
|
|
|
* @param boolean $log The default log flag. |
369
|
|
|
* @return self |
370
|
|
|
*/ |
371
|
|
|
public function setDefaultLogEnabled($log) |
372
|
|
|
{ |
373
|
|
|
$this->defaultLogEnabled = !!$log; |
374
|
|
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Determine if the email sending should be logged by default. |
379
|
|
|
* |
380
|
|
|
* @return boolean |
381
|
|
|
*/ |
382
|
|
|
public function defaultLogEnabled() |
383
|
|
|
{ |
384
|
|
|
return $this->defaultLogEnabled; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Set whether the email (open) should be tracked by default. |
389
|
|
|
* |
390
|
|
|
* @param boolean $track The default track flag. |
391
|
|
|
* @return self |
392
|
|
|
*/ |
393
|
|
|
public function setDefaultTrackOpenEnabled($track) |
394
|
|
|
{ |
395
|
|
|
$this->defaultTrackOpenEnabled = !!$track; |
396
|
|
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Determine if the email (open) should be tracked by default. |
401
|
|
|
* |
402
|
|
|
* @return boolean |
403
|
|
|
*/ |
404
|
|
|
public function defaultTrackOpenEnabled() |
405
|
|
|
{ |
406
|
|
|
return $this->defaultTrackOpenEnabled; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Set whether the email links should be tracked by default. |
411
|
|
|
* |
412
|
|
|
* @param boolean $track The default track flag. |
413
|
|
|
* @return self |
414
|
|
|
*/ |
415
|
|
|
public function setDefaultTrackLinksEnabled($track) |
416
|
|
|
{ |
417
|
|
|
$this->defaultTrackLinksEnabled = !!$track; |
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Determine if the email links should be tracked by default. |
423
|
|
|
* |
424
|
|
|
* @return boolean |
425
|
|
|
*/ |
426
|
|
|
public function defaultTrackLinksEnabled() |
427
|
|
|
{ |
428
|
|
|
return $this->defaultTrackLinksEnabled; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|