1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Mailer\SwiftMailer; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeImmutable; |
9
|
|
|
use DateTimeInterface; |
10
|
|
|
use Swift_Attachment; |
11
|
|
|
use Swift_EmbeddedFile; |
12
|
|
|
use Swift_Message; |
13
|
|
|
use Swift_Mime_Headers_UnstructuredHeader; |
14
|
|
|
use Swift_Mime_MimePart; |
15
|
|
|
use Swift_Signer; |
16
|
|
|
use Throwable; |
17
|
|
|
use Yiisoft\Mailer\File; |
18
|
|
|
use Yiisoft\Mailer\MessageInterface; |
19
|
|
|
|
20
|
|
|
use function is_string; |
21
|
|
|
use function reset; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Message implements a message class based on SwiftMailer. |
25
|
|
|
* |
26
|
|
|
* @see https://swiftmailer.symfony.com/docs/messages.html |
27
|
|
|
* @see Mailer |
28
|
|
|
*/ |
29
|
|
|
final class Message implements MessageInterface |
30
|
|
|
{ |
31
|
|
|
private Swift_Message $swiftMessage; |
32
|
|
|
private ?DateTimeImmutable $date = null; |
33
|
|
|
private ?Throwable $error = null; |
34
|
|
|
|
35
|
57 |
|
public function __construct() |
36
|
|
|
{ |
37
|
57 |
|
$this->swiftMessage = new Swift_Message(); |
38
|
57 |
|
} |
39
|
|
|
|
40
|
56 |
|
public function __clone() |
41
|
|
|
{ |
42
|
56 |
|
$this->swiftMessage = clone $this->swiftMessage; |
43
|
56 |
|
} |
44
|
|
|
|
45
|
3 |
|
public function getCharset(): string |
46
|
|
|
{ |
47
|
3 |
|
return $this->swiftMessage->getCharset(); |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
public function withCharset(string $charset): self |
51
|
|
|
{ |
52
|
5 |
|
$new = clone $this; |
53
|
5 |
|
$new->swiftMessage->setCharset($charset); |
54
|
5 |
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
public function getFrom() |
58
|
|
|
{ |
59
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getFrom()); |
60
|
|
|
} |
61
|
|
|
|
62
|
12 |
|
public function withFrom($from): self |
63
|
|
|
{ |
64
|
12 |
|
$new = clone $this; |
65
|
12 |
|
$new->swiftMessage->setFrom($from); |
66
|
12 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
public function getTo() |
70
|
|
|
{ |
71
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getTo()); |
72
|
|
|
} |
73
|
|
|
|
74
|
13 |
|
public function withTo($to): self |
75
|
|
|
{ |
76
|
13 |
|
$new = clone $this; |
77
|
13 |
|
$new->swiftMessage->setTo($to); |
78
|
13 |
|
return $new; |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
public function getReplyTo() |
82
|
|
|
{ |
83
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getReplyTo()); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
public function withReplyTo($replyTo): self |
87
|
|
|
{ |
88
|
6 |
|
$new = clone $this; |
89
|
6 |
|
$new->swiftMessage->setReplyTo($replyTo); |
90
|
6 |
|
return $new; |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
public function getCc() |
94
|
|
|
{ |
95
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getCc()); |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
public function withCc($cc): self |
99
|
|
|
{ |
100
|
6 |
|
$new = clone $this; |
101
|
6 |
|
$new->swiftMessage->setCc($cc); |
102
|
6 |
|
return $new; |
103
|
|
|
} |
104
|
|
|
|
105
|
5 |
|
public function getBcc() |
106
|
|
|
{ |
107
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getBcc()); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
public function withBcc($bcc): self |
111
|
|
|
{ |
112
|
6 |
|
$new = clone $this; |
113
|
6 |
|
$new->swiftMessage->setBcc($bcc); |
114
|
6 |
|
return $new; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
public function getSubject(): string |
118
|
|
|
{ |
119
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
120
|
2 |
|
return (string) $this->swiftMessage->getSubject(); |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
public function withSubject(string $subject): self |
124
|
|
|
{ |
125
|
10 |
|
$new = clone $this; |
126
|
10 |
|
$new->swiftMessage->setSubject($subject); |
127
|
10 |
|
return $new; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
public function getDate(): ?DateTimeImmutable |
131
|
|
|
{ |
132
|
2 |
|
return $this->date; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
public function withDate(DateTimeInterface $date): self |
136
|
|
|
{ |
137
|
2 |
|
if ($date instanceof DateTime) { |
138
|
2 |
|
$immutable = new DateTimeImmutable('@' . $date->getTimestamp()); |
139
|
2 |
|
$date = $immutable->setTimezone($date->getTimezone()); |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
$new = $this->withHeader('Date', $date->format(DateTimeInterface::RFC2822)); |
143
|
2 |
|
$new->date = $date; |
144
|
2 |
|
return $new; |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
public function getPriority(): int |
148
|
|
|
{ |
149
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
150
|
6 |
|
return (int) $this->swiftMessage->getPriority(); |
151
|
|
|
} |
152
|
|
|
|
153
|
7 |
|
public function withPriority(int $priority): self |
154
|
|
|
{ |
155
|
7 |
|
$new = clone $this; |
156
|
7 |
|
$new->swiftMessage->setPriority($priority); |
157
|
7 |
|
return $new; |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
public function getReturnPath(): string |
161
|
|
|
{ |
162
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
163
|
2 |
|
return (string) $this->swiftMessage->getReturnPath(); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
public function withReturnPath(string $address): self |
167
|
|
|
{ |
168
|
3 |
|
$new = clone $this; |
169
|
3 |
|
$new->swiftMessage->setReturnPath($address); |
170
|
3 |
|
return $new; |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
public function getSender(): string |
174
|
|
|
{ |
175
|
|
|
/** @var array<string, null>|null $sender */ |
176
|
2 |
|
$sender = $this->swiftMessage->getSender(); |
177
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
178
|
2 |
|
return empty($sender) ? '' : (string) array_key_first($sender); |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
public function withSender(string $address): self |
182
|
|
|
{ |
183
|
2 |
|
$new = clone $this; |
184
|
2 |
|
$new->swiftMessage->setSender($address); |
185
|
2 |
|
return $new; |
186
|
|
|
} |
187
|
|
|
|
188
|
2 |
|
public function getTextBody(): string |
189
|
|
|
{ |
190
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
191
|
2 |
|
return (string) $this->swiftMessage->getBody(); |
192
|
|
|
} |
193
|
|
|
|
194
|
7 |
|
public function withTextBody(string $text): self |
195
|
|
|
{ |
196
|
7 |
|
$new = clone $this; |
197
|
7 |
|
$new->setBody($text, 'text/plain'); |
198
|
7 |
|
return $new; |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
public function getHtmlBody(): string |
202
|
|
|
{ |
203
|
|
|
/** @psalm-suppress RedundantCastGivenDocblockType */ |
204
|
3 |
|
return (string) $this->swiftMessage->getBody(); |
205
|
|
|
} |
206
|
|
|
|
207
|
7 |
|
public function withHtmlBody(string $html): self |
208
|
|
|
{ |
209
|
7 |
|
$new = clone $this; |
210
|
7 |
|
$new->setBody($html, 'text/html'); |
211
|
7 |
|
return $new; |
212
|
|
|
} |
213
|
|
|
|
214
|
3 |
|
public function withAttached(File $file): self |
215
|
|
|
{ |
216
|
3 |
|
$attachment = $file->path() === null |
217
|
2 |
|
? new Swift_Attachment($file->content()) |
218
|
3 |
|
: Swift_Attachment::fromPath($file->path()) |
219
|
|
|
; |
220
|
|
|
|
221
|
3 |
|
if (!empty($file->name())) { |
222
|
3 |
|
$attachment->setFilename($file->name()); |
223
|
|
|
} |
224
|
|
|
|
225
|
3 |
|
if (!empty($file->contentType())) { |
226
|
3 |
|
$attachment->setContentType($file->contentType()); |
227
|
|
|
} |
228
|
|
|
|
229
|
3 |
|
$new = clone $this; |
230
|
3 |
|
$new->swiftMessage->attach($attachment); |
231
|
3 |
|
return $new; |
232
|
|
|
} |
233
|
|
|
|
234
|
3 |
|
public function withEmbedded(File $file): self |
235
|
|
|
{ |
236
|
3 |
|
$embedFile = $file->path() === null |
237
|
2 |
|
? new Swift_EmbeddedFile($file->content()) |
238
|
3 |
|
: Swift_EmbeddedFile::fromPath($file->path()) |
239
|
|
|
; |
240
|
|
|
|
241
|
3 |
|
if (!empty($file->name())) { |
242
|
3 |
|
$embedFile->setFilename($file->name()); |
243
|
|
|
} |
244
|
|
|
|
245
|
3 |
|
if (!empty($file->contentType())) { |
246
|
3 |
|
$embedFile->setContentType($file->contentType()); |
247
|
|
|
} |
248
|
|
|
|
249
|
3 |
|
$new = clone $this; |
250
|
3 |
|
$new->swiftMessage->embed($embedFile->setId($file->id())); |
251
|
3 |
|
return $new; |
252
|
|
|
} |
253
|
|
|
|
254
|
7 |
|
public function getHeader(string $name): array |
255
|
|
|
{ |
256
|
7 |
|
$headerSet = $this->swiftMessage->getHeaders(); |
257
|
|
|
|
258
|
7 |
|
if (!$headerSet->has($name)) { |
259
|
1 |
|
return []; |
260
|
|
|
} |
261
|
|
|
|
262
|
6 |
|
$headers = []; |
263
|
|
|
|
264
|
|
|
/** @var Swift_Mime_Headers_UnstructuredHeader $header */ |
265
|
6 |
|
foreach ($headerSet->getAll($name) as $header) { |
266
|
6 |
|
$headers[] = $header->getValue(); |
267
|
|
|
} |
268
|
|
|
|
269
|
6 |
|
return $headers; |
270
|
|
|
} |
271
|
|
|
|
272
|
2 |
|
public function withAddedHeader(string $name, string $value): self |
273
|
|
|
{ |
274
|
2 |
|
$new = clone $this; |
275
|
2 |
|
$new->swiftMessage->getHeaders()->addTextHeader($name, $value); |
276
|
2 |
|
return $new; |
277
|
|
|
} |
278
|
|
|
|
279
|
7 |
|
public function withHeader(string $name, $value): self |
280
|
|
|
{ |
281
|
7 |
|
$new = clone $this; |
282
|
7 |
|
$headerSet = $new->swiftMessage->getHeaders(); |
283
|
|
|
|
284
|
7 |
|
if ($headerSet->has($name)) { |
285
|
3 |
|
$headerSet->remove($name); |
286
|
|
|
} |
287
|
|
|
|
288
|
7 |
|
foreach ((array) $value as $v) { |
289
|
7 |
|
$headerSet->addTextHeader($name, $v); |
290
|
|
|
} |
291
|
|
|
|
292
|
7 |
|
return $new; |
293
|
|
|
} |
294
|
|
|
|
295
|
4 |
|
public function withHeaders(array $headers): self |
296
|
|
|
{ |
297
|
4 |
|
$new = clone $this; |
298
|
|
|
|
299
|
4 |
|
foreach ($headers as $name => $value) { |
300
|
3 |
|
$new = $new->withHeader($name, $value); |
301
|
|
|
} |
302
|
|
|
|
303
|
4 |
|
return $new; |
304
|
|
|
} |
305
|
|
|
|
306
|
2 |
|
public function getError(): ?Throwable |
307
|
|
|
{ |
308
|
2 |
|
return $this->error; |
309
|
|
|
} |
310
|
|
|
|
311
|
2 |
|
public function withError(Throwable $e): self |
312
|
|
|
{ |
313
|
2 |
|
$new = clone $this; |
314
|
2 |
|
$new->error = $e; |
315
|
2 |
|
return $new; |
316
|
|
|
} |
317
|
|
|
|
318
|
3 |
|
public function __toString(): string |
319
|
|
|
{ |
320
|
3 |
|
return $this->swiftMessage->toString(); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Returns a Swift message instance. |
325
|
|
|
* |
326
|
|
|
* @return Swift_Message Swift message instance. |
327
|
|
|
*/ |
328
|
10 |
|
public function getSwiftMessage(): Swift_Message |
329
|
|
|
{ |
330
|
10 |
|
return $this->swiftMessage; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Returns the addresses to which a read-receipt will be sent. |
335
|
|
|
* |
336
|
|
|
* @return array<string, string>|string The receipt receive email addresses. |
337
|
|
|
*/ |
338
|
5 |
|
public function getReadReceiptTo() |
339
|
|
|
{ |
340
|
5 |
|
return $this->normalizeAddresses($this->swiftMessage->getReadReceiptTo()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Returns a new instance with the specified ask for a delivery receipt from the recipient to be sent to addresses. |
345
|
|
|
* |
346
|
|
|
* @param string|string[] $addresses The receipt receive email address(es). |
347
|
|
|
* |
348
|
|
|
* @return self |
349
|
|
|
*/ |
350
|
6 |
|
public function withReadReceiptTo($addresses): self |
351
|
|
|
{ |
352
|
6 |
|
$new = clone $this; |
353
|
6 |
|
$new->swiftMessage->setReadReceiptTo((array) $addresses); |
354
|
6 |
|
return $new; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns a new instance with the specified attached signers. |
359
|
|
|
* |
360
|
|
|
* @param Swift_Signer[] $signers |
361
|
|
|
* |
362
|
|
|
* @return self |
363
|
|
|
*/ |
364
|
3 |
|
public function withAttachedSigners(array $signers): self |
365
|
|
|
{ |
366
|
3 |
|
$new = clone $this; |
367
|
|
|
|
368
|
3 |
|
foreach ($signers as $signer) { |
369
|
2 |
|
$new->swiftMessage->attachSigner($signer); |
370
|
|
|
} |
371
|
|
|
|
372
|
3 |
|
return $new; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Sets the message body. |
377
|
|
|
* |
378
|
|
|
* If body is already set and its content type matches given one, it will |
379
|
|
|
* be overridden, if content type miss match the multipart message will be composed. |
380
|
|
|
* |
381
|
|
|
* @param string $body The body content. |
382
|
|
|
* @param string $contentType The body content type. |
383
|
|
|
*/ |
384
|
11 |
|
private function setBody(string $body, string $contentType): void |
385
|
|
|
{ |
386
|
11 |
|
$oldBody = $this->swiftMessage->getBody(); |
387
|
11 |
|
$charset = $this->swiftMessage->getCharset(); |
388
|
|
|
|
389
|
11 |
|
if (!empty($oldBody)) { |
390
|
3 |
|
$oldContentType = $this->swiftMessage->getContentType(); |
391
|
|
|
|
392
|
3 |
|
if ($oldContentType === $contentType) { |
393
|
1 |
|
$this->swiftMessage->setBody($body, $contentType); |
394
|
1 |
|
return; |
395
|
|
|
} |
396
|
|
|
|
397
|
2 |
|
$this->swiftMessage->setBody(null); |
398
|
|
|
/** @psalm-suppress NullArgument */ |
399
|
2 |
|
$this->swiftMessage->setContentType(null); |
400
|
2 |
|
$this->swiftMessage->addPart($oldBody, $oldContentType, $charset); |
401
|
2 |
|
$this->swiftMessage->addPart($body, $contentType, $charset); |
402
|
2 |
|
return; |
403
|
|
|
} |
404
|
|
|
|
405
|
11 |
|
$parts = $this->swiftMessage->getChildren(); |
406
|
11 |
|
$partFound = false; |
407
|
|
|
|
408
|
11 |
|
foreach ($parts as $key => $part) { |
409
|
1 |
|
if ($part instanceof Swift_Mime_MimePart && $part->getContentType() === $contentType) { |
410
|
1 |
|
$charset = $part->getCharset(); |
411
|
1 |
|
unset($parts[$key]); |
412
|
1 |
|
$partFound = true; |
413
|
1 |
|
break; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
11 |
|
if (!$partFound) { |
418
|
11 |
|
$this->swiftMessage->setBody($body, $contentType); |
419
|
11 |
|
return; |
420
|
|
|
} |
421
|
|
|
|
422
|
1 |
|
reset($parts); |
423
|
1 |
|
$this->swiftMessage->setChildren($parts); |
424
|
1 |
|
$this->swiftMessage->addPart($body, $contentType, $charset); |
425
|
1 |
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Normalizes email addresses and names to the correct format. |
429
|
|
|
* |
430
|
|
|
* @param mixed $addresses |
431
|
|
|
* |
432
|
|
|
* @return array<string, string>|string |
433
|
|
|
*/ |
434
|
25 |
|
private function normalizeAddresses($addresses) |
435
|
|
|
{ |
436
|
25 |
|
if (empty($addresses)) { |
437
|
1 |
|
return ''; |
438
|
|
|
} |
439
|
|
|
|
440
|
24 |
|
if (is_string($addresses)) { |
441
|
|
|
return $addresses; |
442
|
|
|
} |
443
|
|
|
|
444
|
24 |
|
$normalized = []; |
445
|
|
|
|
446
|
|
|
/** @var mixed $name */ |
447
|
24 |
|
foreach ((array) $addresses as $address => $name) { |
448
|
24 |
|
$normalized[(string) $address] = is_string($name) ? $name : ''; |
449
|
|
|
} |
450
|
|
|
|
451
|
24 |
|
return $normalized; |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|