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 Symfony\Component\Mailer\Mailer; |
|
|
|
|
15
|
|
|
use Symfony\Component\Mailer\Transport; |
|
|
|
|
16
|
|
|
use Symfony\Component\Mime\Address; |
|
|
|
|
17
|
|
|
use Symfony\Component\Mime\Crypto\DkimSigner; |
|
|
|
|
18
|
|
|
use Symfony\Component\Mime\Crypto\SMimeSigner; |
|
|
|
|
19
|
|
|
use Symfony\Component\Mime\Email; |
|
|
|
|
20
|
|
|
use Symfony\Component\Mime\Part\DataPart; |
|
|
|
|
21
|
|
|
use Symfony\Component\Mime\Part\File; |
|
|
|
|
22
|
|
|
use Throwable; |
23
|
|
|
|
24
|
|
|
class SymfonyMailer extends AbstractAdapter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
|
|
protected array $dependancies = [ |
30
|
|
|
['class' => Mailer::class, 'package' => 'symfony/mailer'], |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Email |
35
|
|
|
*/ |
36
|
|
|
protected $mailer; |
37
|
|
|
|
38
|
|
|
private ?Mailer $transporter = null; |
39
|
|
|
private string $charset = self::CHARSET_UTF8; |
40
|
|
|
private string $dsn = ''; |
41
|
|
|
private string $protocol = self::PROTOCOL_SMTP; |
42
|
|
|
private int $timeout = 0; |
43
|
|
|
private int $port = 587; |
44
|
|
|
private string $host = ''; |
45
|
|
|
private string $username = ''; |
46
|
|
|
private string $password = ''; |
47
|
|
|
private string $encryption = self::ENCRYPTION_TLS; |
48
|
|
|
private int $debug = 0; |
49
|
|
|
|
50
|
|
|
public function __construct(bool $debug = false) |
51
|
|
|
{ |
52
|
|
|
$this->mailer = new Email(); |
53
|
|
|
|
54
|
|
|
parent::__construct($debug); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setDsn(string $dsn): static |
58
|
|
|
{ |
59
|
|
|
$this->dsn = $dsn; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function setPort(int $port): static |
68
|
|
|
{ |
69
|
|
|
$this->port = $port; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function setHost(string $host): static |
78
|
|
|
{ |
79
|
|
|
$this->host = $host; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function setUsername(string $username): static |
88
|
|
|
{ |
89
|
|
|
$this->username = $username; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function setPassword(string $password): static |
98
|
|
|
{ |
99
|
|
|
$this->password = $password; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
|
|
public function setDebug(int $debug = 1): static |
108
|
|
|
{ |
109
|
|
|
$this->debug = $debug; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function setProtocol(string $protocol): static |
118
|
|
|
{ |
119
|
|
|
$this->protocol = $protocol; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function setTimeout(int $timeout): static |
128
|
|
|
{ |
129
|
|
|
$this->timeout = $timeout; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
|
public function setCharset(string $charset): static |
138
|
|
|
{ |
139
|
|
|
$this->charset = $charset; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritDoc} |
146
|
|
|
*/ |
147
|
|
|
public function setPriority(int $priority): static |
148
|
|
|
{ |
149
|
|
|
if (in_array($priority, static::PRIORITY_MAP, true)) { |
150
|
|
|
$this->mailer->priority($priority); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
|
|
public function setEncryption(?string $encryption): static |
160
|
|
|
{ |
161
|
|
|
if ($encryption === static::ENCRYPTION_NONE) { |
162
|
|
|
$encryption = null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) { |
166
|
|
|
$this->encryption = $encryption; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function clear(): self |
176
|
|
|
{ |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
*/ |
183
|
|
|
public function alt(string $content): static |
184
|
|
|
{ |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritDoc} |
190
|
|
|
*/ |
191
|
|
|
public function attach(array|string $path, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): static |
192
|
|
|
{ |
193
|
|
|
if (is_string($path)) { |
|
|
|
|
194
|
|
|
$path = [$path => $name]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
foreach ($path as $key => $value) { |
198
|
|
|
$this->mailer->addPart(new DataPart(new File($key), $value, $type)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritDoc} |
206
|
|
|
*/ |
207
|
|
|
public function attachBinary($binary, string $name, string $encoding = self::ENCODING_BASE64, string $type = '', string $disposition = 'attachment'): static |
208
|
|
|
{ |
209
|
|
|
$this->mailer->addPart(new DataPart(@fopen($binary, 'rb'), $name, $type)); |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritDoc} |
216
|
|
|
*/ |
217
|
|
|
public function bcc(array|string $address, bool|string $name = '', bool $set = false): static |
218
|
|
|
{ |
219
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
220
|
|
|
|
221
|
|
|
if ($set) { |
222
|
|
|
$this->mailer->bcc(...$addresses); |
223
|
|
|
} else { |
224
|
|
|
$this->mailer->addBcc(...$addresses); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritDoc} |
232
|
|
|
*/ |
233
|
|
|
public function cc(array|string $address, bool|string $name = '', bool $set = false): static |
234
|
|
|
{ |
235
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
236
|
|
|
|
237
|
|
|
if ($set) { |
238
|
|
|
$this->mailer->cc(...$addresses); |
239
|
|
|
} else { |
240
|
|
|
$this->mailer->addCC(...$addresses); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritDoc} |
248
|
|
|
*/ |
249
|
|
|
public function dkim(string $pk, string $passphrase = '', string $selector = '', string $domain = ''): static |
250
|
|
|
{ |
251
|
|
|
$signer = new DkimSigner($pk, $domain ?: site_url(), $selector ?: 'blitz', [], $passphrase); |
252
|
|
|
|
253
|
|
|
$this->mailer = $signer->sign($this->mailer); |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritDoc} |
260
|
|
|
*/ |
261
|
|
|
public function embedded(string $path, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): static |
262
|
|
|
{ |
263
|
|
|
$this->mailer->addPart((new DataPart(new File($path), $cid, $type))->asInline()); |
264
|
|
|
|
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritDoc} |
270
|
|
|
*/ |
271
|
|
|
public function embeddedBinary($binary, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): static |
272
|
|
|
{ |
273
|
|
|
$this->mailer->addPart((new DataPart(@fopen($binary, 'rb'), $name, $type))->asInline()); |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* {@inheritDoc} |
280
|
|
|
*/ |
281
|
|
|
public function from(string $address, string $name = ''): static |
282
|
|
|
{ |
283
|
|
|
$this->mailer->from($this->makeAddress($address, $name)); |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritDoc} |
290
|
|
|
*/ |
291
|
|
|
public function header(array|string $name, ?string $value = null): static |
292
|
|
|
{ |
293
|
|
|
if (is_string($name)) { |
|
|
|
|
294
|
|
|
$name = [$name => $value]; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
foreach ($name as $key => $value) { |
298
|
|
|
$this->mailer->getHeaders()->addTextHeader($key, $value); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* {@inheritDoc} |
306
|
|
|
*/ |
307
|
|
|
public function html(string $content): static |
308
|
|
|
{ |
309
|
|
|
$this->mailer->html($content, $this->charset); |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* {@inheritDoc} |
316
|
|
|
*/ |
317
|
|
|
public function message(string $message): static |
318
|
|
|
{ |
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* {@inheritDoc} |
324
|
|
|
*/ |
325
|
|
|
public function replyTo(array|string $address, bool|string $name = '', bool $set = false): static |
326
|
|
|
{ |
327
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
328
|
|
|
|
329
|
|
|
if ($set) { |
330
|
|
|
$this->mailer->replyTo(...$addresses); |
331
|
|
|
} else { |
332
|
|
|
$this->mailer->addReplyTo(...$addresses); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritDoc} |
340
|
|
|
*/ |
341
|
|
|
public function send(): bool |
342
|
|
|
{ |
343
|
|
|
try { |
344
|
|
|
$this->transporter()->send($this->mailer); |
345
|
|
|
|
346
|
|
|
return true; |
347
|
|
|
} catch (Throwable $e) { |
348
|
|
|
if ($this->debug > 0) { |
349
|
|
|
throw $e; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* {@inheritDoc} |
358
|
|
|
*/ |
359
|
|
|
public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): static |
360
|
|
|
{ |
361
|
|
|
$signer = new SMimeSigner($cert_filename, $key_filename, $key_pass, $extracerts_filename); |
362
|
|
|
|
363
|
|
|
$this->mailer = $signer->sign($this->mailer); |
364
|
|
|
|
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* {@inheritDoc} |
370
|
|
|
*/ |
371
|
|
|
public function subject(string $subject): static |
372
|
|
|
{ |
373
|
|
|
$this->mailer->subject($subject); |
374
|
|
|
|
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* {@inheritDoc} |
380
|
|
|
*/ |
381
|
|
|
public function text(string $content): static |
382
|
|
|
{ |
383
|
|
|
$this->mailer->text($content, $this->charset); |
384
|
|
|
|
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* {@inheritDoc} |
390
|
|
|
*/ |
391
|
|
|
public function to(array|string $address, bool|string $name = '', bool $set = false): static |
392
|
|
|
{ |
393
|
|
|
[$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
394
|
|
|
|
395
|
|
|
if ($set) { |
396
|
|
|
$this->mailer->to(...$addresses); |
397
|
|
|
} else { |
398
|
|
|
$this->mailer->addTo(...$addresses); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* {@inheritDoc} |
406
|
|
|
*/ |
407
|
|
|
public function lastId(): string |
408
|
|
|
{ |
409
|
|
|
return $this->mailer->generateMessageId(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* {@inheritDoc} |
414
|
|
|
* |
415
|
|
|
* @return Address |
416
|
|
|
*/ |
417
|
|
|
protected function makeAddress(string $email, string $name) |
418
|
|
|
{ |
419
|
|
|
[$email, $name] = parent::makeAddress($email, $name); |
420
|
|
|
|
421
|
|
|
return new Address($email, $name); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
private function transporter(): Mailer |
425
|
|
|
{ |
426
|
|
|
if (null !== $this->transporter) { |
427
|
|
|
return $this->transporter; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $this->transporter = new Mailer( |
431
|
|
|
Transport::fromDsn($this->buildDsn()) |
432
|
|
|
); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
private function buildDsn(): string |
436
|
|
|
{ |
437
|
|
|
if ($this->dsn !== '') { |
438
|
|
|
return $this->dsn; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return match ($this->protocol) { |
442
|
|
|
static::PROTOCOL_SMTP => "smtp://{$this->username}:{$this->password}@{$this->host}:{$this->port}", |
443
|
|
|
static::PROTOCOL_SENDMAIL => 'sendmail://default', |
444
|
|
|
static::PROTOCOL_MAIL => 'sendmail://default', |
445
|
|
|
static::PROTOCOL_POSTMARK => "postmark+smtp://{$this->username}@default", // username joue le role de ID |
446
|
|
|
static::PROTOCOL_SENDGRID => "sendgrid+smtp://apikey:{$this->username}@default", // username joue le role de API_KEY |
447
|
|
|
default => "{$this->protocol}+smtp://{$this->username}:{$this->password}@default", |
448
|
|
|
}; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
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