1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Mailer\Symfony; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Symfony\Component\Mime\Address; |
9
|
|
|
use Symfony\Component\Mime\Email; |
10
|
|
|
use Yiisoft\Mailer\MessageInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
final class EmailFactory |
16
|
|
|
{ |
17
|
|
|
public function create(MessageInterface $message): Email |
18
|
|
|
{ |
19
|
|
|
$email = (new Email()) |
20
|
|
|
->from(...$this->convertStringsToAddresses($message->getFrom())) |
21
|
|
|
->to(...$this->convertStringsToAddresses($message->getTo())) |
22
|
|
|
->replyTo(...$this->convertStringsToAddresses($message->getReplyTo())) |
23
|
|
|
->cc(...$this->convertStringsToAddresses($message->getCc())) |
24
|
|
|
->bcc(...$this->convertStringsToAddresses($message->getBcc())) |
25
|
|
|
->subject($message->getSubject()) |
26
|
|
|
->priority($message->getPriority()) |
27
|
|
|
->text($message->getTextBody(), $message->getCharset()) |
28
|
|
|
->html($message->getHtmlBody(), $message->getCharset()); |
29
|
|
|
|
30
|
|
|
$returnPath = $message->getReturnPath(); |
31
|
|
|
if ($returnPath !== '') { |
32
|
|
|
$email->returnPath($returnPath); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$sender = $message->getSender(); |
36
|
|
|
if ($sender !== '') { |
37
|
|
|
$email->sender($sender); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$date = $message->getDate(); |
41
|
|
|
if ($date !== null) { |
42
|
|
|
$email->date($date); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$emailHeaders = $email->getHeaders(); |
46
|
|
|
foreach ($message->getHeaders() as $name => $values) { |
|
|
|
|
47
|
|
|
foreach ($values as $value) { |
48
|
|
|
switch ($name) { |
49
|
|
|
case 'Date': |
50
|
|
|
$emailHeaders->addDateHeader($name, new DateTimeImmutable($value)); |
51
|
|
|
break; |
52
|
|
|
case 'Message-ID': |
53
|
|
|
$emailHeaders->addIdHeader($name, $value); |
54
|
|
|
break; |
55
|
|
|
default: |
56
|
|
|
$emailHeaders->addTextHeader($name, $value); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($message->getAttachments() as $file) { |
|
|
|
|
62
|
|
|
$file->path() === null |
63
|
|
|
? $email->attach((string) $file->content(), $file->name(), $file->contentType()) |
64
|
|
|
: $email->attachFromPath($file->path(), $file->name(), $file->contentType()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($message->getEmbeddedFiles() as $file) { |
|
|
|
|
68
|
|
|
$file->path() === null |
69
|
|
|
? $email->embed((string) $file->content(), $file->name(), $file->contentType()) |
70
|
|
|
: $email->embedFromPath($file->path(), $file->name(), $file->contentType()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $email; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Converts address instances to their string representations. |
78
|
|
|
* |
79
|
|
|
* @param Address[] $addresses |
80
|
|
|
* |
81
|
|
|
* @return array<string, string>|string |
82
|
|
|
*/ |
83
|
|
|
private function convertAddressesToStrings(array $addresses): array|string |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$strings = []; |
86
|
|
|
|
87
|
|
|
foreach ($addresses as $address) { |
88
|
|
|
$strings[$address->getAddress()] = $address->getName(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return empty($strings) ? '' : $strings; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Converts string representations of address to their instances. |
96
|
|
|
* |
97
|
|
|
* @param array<int|string, string>|string $strings |
98
|
|
|
* |
99
|
|
|
* @return Address[] |
100
|
|
|
*/ |
101
|
|
|
private function convertStringsToAddresses(array|string $strings): array |
102
|
|
|
{ |
103
|
|
|
if (is_string($strings)) { |
|
|
|
|
104
|
|
|
return [new Address($strings)]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$addresses = []; |
108
|
|
|
|
109
|
|
|
foreach ($strings as $address => $name) { |
110
|
|
|
if (!is_string($address)) { |
111
|
|
|
// email address without name |
112
|
|
|
$addresses[] = new Address($name); |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$addresses[] = new Address($address, $name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $addresses; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.