|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\ParsedHeader; |
|
7
|
|
|
use Genkgo\Mail\Header\HeaderLine; |
|
8
|
|
|
use Genkgo\Mail\Header\HeaderName; |
|
9
|
|
|
use Genkgo\Mail\Header\HeaderValueParameter; |
|
10
|
|
|
use Genkgo\Mail\Mime\MultiPartInterface; |
|
11
|
|
|
use Genkgo\Mail\Mime\PartInterface; |
|
12
|
|
|
use Genkgo\Mail\Stream\ConcatenatedStream; |
|
13
|
|
|
use Genkgo\Mail\Stream\StringStream; |
|
14
|
|
|
|
|
15
|
|
|
final class MimeMessageFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param PartInterface $part |
|
19
|
|
|
* @return MessageInterface |
|
20
|
|
|
*/ |
|
21
|
8 |
|
public function createMessage(PartInterface $part): MessageInterface |
|
22
|
|
|
{ |
|
23
|
8 |
|
$message = new GenericMessage(); |
|
24
|
|
|
|
|
25
|
8 |
|
$part = $this->pickOptimalCharset($part); |
|
26
|
|
|
|
|
27
|
8 |
|
foreach ($part->getHeaders() as $header) { |
|
28
|
8 |
|
$message = $message->withHeader($header); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
8 |
|
if ($part instanceof MultiPartInterface) { |
|
32
|
4 |
|
$message = $message->withBody( |
|
33
|
4 |
|
new ConcatenatedStream( |
|
34
|
4 |
|
new \ArrayObject([ |
|
|
|
|
|
|
35
|
4 |
|
new StringStream("This is a multipart message in MIME format.\r\n"), |
|
36
|
4 |
|
$this->createPartStream($part, false) |
|
37
|
|
|
]) |
|
38
|
|
|
) |
|
39
|
|
|
); |
|
40
|
|
|
} else { |
|
41
|
4 |
|
$message = $message->withBody($this->createPartStream($part, false)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
return $message; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param PartInterface $part |
|
49
|
|
|
* @param bool $headers |
|
50
|
|
|
* @return StreamInterface |
|
51
|
|
|
*/ |
|
52
|
8 |
|
private function createPartStream(PartInterface $part, bool $headers = true): StreamInterface |
|
53
|
|
|
{ |
|
54
|
8 |
|
$part = $this->pickOptimalCharset($part); |
|
55
|
|
|
|
|
56
|
8 |
|
$partBody = $part->getBody(); |
|
57
|
8 |
|
$streams = new \ArrayObject(); |
|
58
|
8 |
|
if ($headers) { |
|
59
|
4 |
|
$streams->append(new StringStream($this->createHeaderLines($part))); |
|
60
|
4 |
|
$streams->append(new StringStream("\r\n\r\n")); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
8 |
|
$streams->append($partBody); |
|
64
|
|
|
|
|
65
|
8 |
|
if ($part instanceof MultiPartInterface) { |
|
66
|
4 |
|
$childParts = $part->getParts(); |
|
67
|
4 |
|
$boundary = $part->getBoundary(); |
|
68
|
|
|
|
|
69
|
4 |
|
foreach ($childParts as $childPart) { |
|
70
|
4 |
|
$streams->append(new StringStream("\r\n")); |
|
71
|
4 |
|
$streams->append(new StringStream('--' . (string) $boundary)); |
|
72
|
4 |
|
$streams->append(new StringStream("\r\n")); |
|
73
|
4 |
|
$streams->append($this->createPartStream($childPart)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
if ($childParts) { |
|
77
|
4 |
|
$streams->append(new StringStream('--' . (string) $boundary . '--')); |
|
78
|
4 |
|
$streams->append(new StringStream("\r\n")); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
8 |
|
if ($partBody->getSize() !== 0) { |
|
83
|
7 |
|
$streams->append(new StringStream("\r\n")); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
8 |
|
return new ConcatenatedStream($streams); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param PartInterface $part |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
4 |
|
private function createHeaderLines(PartInterface $part): string |
|
94
|
|
|
{ |
|
95
|
4 |
|
return \implode( |
|
96
|
4 |
|
"\r\n", |
|
97
|
4 |
|
\array_values( |
|
98
|
4 |
|
\array_map( |
|
99
|
4 |
|
function (HeaderInterface $header) { |
|
100
|
4 |
|
return (string) (new HeaderLine($header)); |
|
101
|
4 |
|
}, |
|
102
|
4 |
|
$part->getHeaders() |
|
103
|
|
|
) |
|
104
|
|
|
) |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param PartInterface $part |
|
110
|
|
|
* @return PartInterface |
|
111
|
|
|
*/ |
|
112
|
8 |
|
private function pickOptimalCharset(PartInterface $part): PartInterface |
|
113
|
|
|
{ |
|
114
|
|
|
try { |
|
115
|
8 |
|
$transferEncoding = (string) $part->getHeader('Content-Transfer-Encoding')->getValue(); |
|
116
|
4 |
|
} catch (\UnexpectedValueException $e) { |
|
117
|
4 |
|
return $part; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
8 |
|
$contentTypeHeader = $part->getHeader('Content-Type')->getValue(); |
|
121
|
8 |
|
if ($transferEncoding !== '7bit' || \substr((string)$contentTypeHeader, 0, 5) !== 'text/') { |
|
122
|
3 |
|
return $part; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
try { |
|
126
|
8 |
|
if ($contentTypeHeader->getParameter('charset')->getValue() === 'us-ascii') { |
|
127
|
8 |
|
return $part; |
|
128
|
|
|
} |
|
129
|
|
|
} catch (\UnexpectedValueException $e) { |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
8 |
|
return $part->withHeader( |
|
133
|
8 |
|
new ParsedHeader( |
|
134
|
8 |
|
new HeaderName('Content-Type'), |
|
135
|
8 |
|
$contentTypeHeader->withParameter( |
|
136
|
8 |
|
new HeaderValueParameter('charset', 'us-ascii') |
|
137
|
|
|
) |
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: