1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Mail\Adapters; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Container\Services; |
15
|
|
|
use PHPMailer\PHPMailer\PHPMailer as Mailer; |
|
|
|
|
16
|
|
|
use PHPMailer\PHPMailer\SMTP; |
|
|
|
|
17
|
|
|
|
18
|
|
|
class PHPMailer extends AbstractAdapter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
protected array $dependancies = [ |
24
|
|
|
['class' => Mailer::class, 'package' => 'phpmailer/phpmailer'], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Mailer |
29
|
|
|
*/ |
30
|
|
|
protected $mailer; |
31
|
|
|
|
32
|
|
|
public function __construct(bool $debug = false) |
33
|
|
|
{ |
34
|
|
|
$this->mailer = new Mailer(); |
35
|
|
|
$this->mailer->Debugoutput = static function ($str, $level): void { |
36
|
|
|
Services::logger()->info('[Mail][' . $level . ']: ' . $str); |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
parent::__construct($debug); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function init(array $config): static |
46
|
|
|
{ |
47
|
|
|
if (! empty($config['username']) && ! empty($config['password'])) { |
48
|
|
|
$this->mailer->SMTPAuth = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return parent::init($config); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function setPort(int $port): static |
58
|
|
|
{ |
59
|
|
|
$this->mailer->Port = $port; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function setHost(string $host): static |
68
|
|
|
{ |
69
|
|
|
$this->mailer->Host = $host; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function setUsername(string $username): static |
78
|
|
|
{ |
79
|
|
|
$this->mailer->Username = $username; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function setPassword(string $password): static |
88
|
|
|
{ |
89
|
|
|
$this->mailer->Password = $password; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function setDebug(int $debug = SMTP::DEBUG_SERVER): static |
98
|
|
|
{ |
99
|
|
|
$this->mailer->SMTPDebug = $debug; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
|
|
public function setProtocol(string $protocol): static |
108
|
|
|
{ |
109
|
|
|
match (strtolower($protocol)) { |
110
|
|
|
static::PROTOCOL_MAIL => $this->mailer->isMail(), |
111
|
|
|
static::PROTOCOL_QMAIL => $this->mailer->isQmail(), |
112
|
|
|
static::PROTOCOL_SENDMAIL => $this->mailer->isSendmail(), |
113
|
|
|
default => $this->mailer->isSMTP(), |
114
|
|
|
}; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
|
|
public function setTimeout(int $timeout): static |
123
|
|
|
{ |
124
|
|
|
$this->mailer->Timeout = $timeout; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function setCharset(string $charset): static |
133
|
|
|
{ |
134
|
|
|
$this->mailer->CharSet = $charset; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
|
|
public function setPriority(int $priority): static |
143
|
|
|
{ |
144
|
|
|
if (in_array($priority, static::PRIORITY_MAP, true)) { |
145
|
|
|
$this->mailer->Priority = $priority; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritDoc} |
153
|
|
|
*/ |
154
|
|
|
public function setEncryption(?string $encryption): static |
155
|
|
|
{ |
156
|
|
|
if ($encryption === static::ENCRYPTION_NONE) { |
157
|
|
|
$encryption = null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) { |
161
|
|
|
$this->mailer->SMTPSecure = $encryption; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritDoc} |
169
|
|
|
*/ |
170
|
|
|
public function clear(): self |
171
|
|
|
{ |
172
|
|
|
$this->mailer->clearAddresses(); |
173
|
|
|
$this->mailer->clearAllRecipients(); |
174
|
|
|
$this->mailer->clearAttachments(); |
175
|
|
|
$this->mailer->clearBCCs(); |
176
|
|
|
$this->mailer->clearCCs(); |
177
|
|
|
$this->mailer->clearCustomHeaders(); |
178
|
|
|
$this->mailer->clearReplyTos(); |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function alt(string $content): static |
187
|
|
|
{ |
188
|
|
|
$this->mailer->AltBody = $content; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritDoc} |
195
|
|
|
* |
196
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
197
|
|
|
*/ |
198
|
|
|
public function attach(array|string $path, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): static |
199
|
|
|
{ |
200
|
|
|
if (is_string($path)) { |
|
|
|
|
201
|
|
|
$path = [$path => $name]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
foreach ($path as $key => $value) { |
205
|
|
|
$this->mailer->addAttachment($key, $value, $encoding, $type, $disposition); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritDoc} |
213
|
|
|
* |
214
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
215
|
|
|
*/ |
216
|
|
|
public function attachBinary($binary, string $name, string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): static |
217
|
|
|
{ |
218
|
|
|
$this->mailer->addStringAttachment($binary, $name, $encoding, $type, $disposition); |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritDoc} |
225
|
|
|
* |
226
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
227
|
|
|
*/ |
228
|
|
|
public function bcc(array|string $address, bool|string $name = '', bool $set = false): static |
229
|
|
|
{ |
230
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
231
|
|
|
|
232
|
|
|
if ($set) { |
233
|
|
|
$this->mailer->clearBCCs(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
foreach ($addresses as $address) { |
237
|
|
|
$this->mailer->addBCC(...$address); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritDoc} |
245
|
|
|
* |
246
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
247
|
|
|
*/ |
248
|
|
|
public function cc(array|string $address, bool|string $name = '', bool $set = false): static |
249
|
|
|
{ |
250
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
251
|
|
|
|
252
|
|
|
if ($set) { |
253
|
|
|
$this->mailer->clearCCs(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
foreach ($addresses as $address) { |
257
|
|
|
$this->mailer->addCC(...$address); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritDoc} |
265
|
|
|
*/ |
266
|
|
|
public function dkim(string $pk, string $passphrase = '', string $selector = '', string $domain = ''): static |
267
|
|
|
{ |
268
|
|
|
$this->mailer->DKIM_domain = $domain; |
269
|
|
|
$this->mailer->DKIM_private = $pk; |
270
|
|
|
$this->mailer->DKIM_selector = $selector ?: 'blitz'; |
271
|
|
|
$this->mailer->DKIM_passphrase = $passphrase; |
272
|
|
|
$this->mailer->DKIM_identity = $this->mailer->From; |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* {@inheritDoc} |
279
|
|
|
* |
280
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
281
|
|
|
*/ |
282
|
|
|
public function embedded(string $path, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): static |
283
|
|
|
{ |
284
|
|
|
$this->mailer->addEmbeddedImage($path, $cid, $name, $encoding, $type, $disposition); |
285
|
|
|
|
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritDoc} |
291
|
|
|
* |
292
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
293
|
|
|
*/ |
294
|
|
|
public function embeddedBinary($binary, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): static |
295
|
|
|
{ |
296
|
|
|
$this->mailer->addStringEmbeddedImage($binary, $cid, $name, $encoding, $type, $disposition); |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritDoc} |
303
|
|
|
* |
304
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
305
|
|
|
*/ |
306
|
|
|
public function from(string $address, string $name = ''): static |
307
|
|
|
{ |
308
|
|
|
$this->mailer->setFrom($address, $name); |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* {@inheritDoc} |
315
|
|
|
* |
316
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
317
|
|
|
*/ |
318
|
|
|
public function header(array|string $name, ?string $value = null): static |
319
|
|
|
{ |
320
|
|
|
if (is_string($name)) { |
|
|
|
|
321
|
|
|
$name = [$name => $value]; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
foreach ($name as $key => $value) { |
325
|
|
|
$this->mailer->addCustomHeader($key, $value); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* {@inheritDoc} |
333
|
|
|
*/ |
334
|
|
|
public function html(string $content): static |
335
|
|
|
{ |
336
|
|
|
$this->mailer->isHTML(true); |
337
|
|
|
|
338
|
|
|
return $this->message($content); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritDoc} |
343
|
|
|
*/ |
344
|
|
|
public function lastId(): string |
345
|
|
|
{ |
346
|
|
|
return $this->mailer->getLastMessageID(); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* {@inheritDoc} |
351
|
|
|
*/ |
352
|
|
|
public function message(string $message): static |
353
|
|
|
{ |
354
|
|
|
$this->mailer->Body = $message; |
355
|
|
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* {@inheritDoc} |
361
|
|
|
* |
362
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
363
|
|
|
*/ |
364
|
|
|
public function replyTo(array|string $address, bool|string $name = '', bool $set = false): static |
365
|
|
|
{ |
366
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
367
|
|
|
|
368
|
|
|
if ($set) { |
369
|
|
|
$this->mailer->clearReplyTos(); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
foreach ($addresses as $address) { |
373
|
|
|
$this->mailer->addReplyTo(...$address); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* {@inheritDoc} |
381
|
|
|
* |
382
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
383
|
|
|
*/ |
384
|
|
|
public function send(): bool |
385
|
|
|
{ |
386
|
|
|
return $this->mailer->send(); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* {@inheritDoc} |
391
|
|
|
*/ |
392
|
|
|
public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): static |
393
|
|
|
{ |
394
|
|
|
$this->mailer->sign($cert_filename, $key_filename, $key_pass, $extracerts_filename); |
395
|
|
|
|
396
|
|
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* {@inheritDoc} |
401
|
|
|
*/ |
402
|
|
|
public function subject(string $subject): static |
403
|
|
|
{ |
404
|
|
|
$this->mailer->Subject = $subject; |
405
|
|
|
|
406
|
|
|
return $this; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* {@inheritDoc} |
411
|
|
|
*/ |
412
|
|
|
public function text(string $content): static |
413
|
|
|
{ |
414
|
|
|
$this->mailer->isHTML(false); |
415
|
|
|
|
416
|
|
|
return $this->message($content); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritDoc} |
421
|
|
|
* |
422
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
423
|
|
|
*/ |
424
|
|
|
public function to(array|string $address, bool|string $name = '', bool $set = false): static |
425
|
|
|
{ |
426
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
427
|
|
|
|
428
|
|
|
if ($set) { |
429
|
|
|
$this->mailer->clearAddresses(); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
foreach ($addresses as $address) { |
433
|
|
|
$this->mailer->addAddress(...$address); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths