-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMessage.php
342 lines (307 loc) · 10.2 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* This file is part of the ZBateson\MailMimeParser project.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
namespace ZBateson\MailMimeParser;
use GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use ZBateson\MailMimeParser\Header\HeaderConsts;
use ZBateson\MailMimeParser\Message\Helper\MultipartHelper;
use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper;
use ZBateson\MailMimeParser\Message\IMessagePart;
use ZBateson\MailMimeParser\Message\MimePart;
use ZBateson\MailMimeParser\Message\PartChildrenContainer;
use ZBateson\MailMimeParser\Message\PartFilter;
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
use ZBateson\MailMimeParser\Message\PartStreamContainer;
/**
* An email message.
*
* The message could represent a simple text email, a multipart message with
* children, or a non-mime message containing UUEncoded parts.
*
* @author Zaahid Bateson
*/
class Message extends MimePart implements IMessage
{
/**
* @var MultipartHelper service providing functions for multipart messages.
*/
private MultipartHelper $multipartHelper;
/**
* @var PrivacyHelper service providing functions for multipart/signed
* messages.
*/
private PrivacyHelper $privacyHelper;
public function __construct(
?LoggerInterface $logger = null,
?PartStreamContainer $streamContainer = null,
?PartHeaderContainer $headerContainer = null,
?PartChildrenContainer $partChildrenContainer = null,
?MultipartHelper $multipartHelper = null,
?PrivacyHelper $privacyHelper = null
) {
parent::__construct(
null,
$logger,
$streamContainer,
$headerContainer,
$partChildrenContainer
);
$di = MailMimeParser::getGlobalContainer();
$this->multipartHelper = $multipartHelper ?? $di->get(MultipartHelper::class);
$this->privacyHelper = $privacyHelper ?? $di->get(PrivacyHelper::class);
}
/**
* Convenience method to parse a handle or string into an IMessage without
* requiring including MailMimeParser, instantiating it, and calling parse.
*
* If the passed $resource is a resource handle or StreamInterface, the
* resource must remain open while the returned IMessage object exists.
* Pass true as the second argument to have the resource attached to the
* IMessage and closed for you when it's destroyed, or pass false to
* manually close it if it should remain open after the IMessage object is
* destroyed.
*
* @param resource|StreamInterface|string $resource The resource handle to
* the input stream of the mime message, or a string containing a
* mime message.
* @param bool $attached pass true to have it attached to the returned
* IMessage and destroyed with it.
*/
public static function from(mixed $resource, bool $attached) : IMessage
{
static $mmp = null;
if ($mmp === null) {
$mmp = new MailMimeParser();
}
return $mmp->parse($resource, $attached);
}
/**
* Returns true if the current part is a mime part.
*
* The message is considered 'mime' if it has either a Content-Type or
* MIME-Version header defined.
*
*/
public function isMime() : bool
{
$contentType = $this->getHeaderValue(HeaderConsts::CONTENT_TYPE);
$mimeVersion = $this->getHeaderValue(HeaderConsts::MIME_VERSION);
return ($contentType !== null || $mimeVersion !== null);
}
public function getSubject() : ?string
{
return $this->getHeaderValue(HeaderConsts::SUBJECT);
}
public function getTextPart(int $index = 0) : ?IMessagePart
{
return $this->getPart(
$index,
PartFilter::fromInlineContentType('text/plain')
);
}
public function getTextPartCount() : int
{
return $this->getPartCount(
PartFilter::fromInlineContentType('text/plain')
);
}
public function getHtmlPart(int $index = 0) : ?IMessagePart
{
return $this->getPart(
$index,
PartFilter::fromInlineContentType('text/html')
);
}
public function getHtmlPartCount() : int
{
return $this->getPartCount(
PartFilter::fromInlineContentType('text/html')
);
}
public function getTextStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface
{
$textPart = $this->getTextPart($index);
if ($textPart !== null) {
return $textPart->getContentStream($charset);
}
return null;
}
public function getTextContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string
{
$part = $this->getTextPart($index);
if ($part !== null) {
return $part->getContent($charset);
}
return null;
}
public function getHtmlStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface
{
$htmlPart = $this->getHtmlPart($index);
if ($htmlPart !== null) {
return $htmlPart->getContentStream($charset);
}
return null;
}
public function getHtmlContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string
{
$part = $this->getHtmlPart($index);
if ($part !== null) {
return $part->getContent($charset);
}
return null;
}
public function setTextPart(mixed $resource, string $charset = 'UTF-8') : static
{
$this->multipartHelper
->setContentPartForMimeType(
$this,
'text/plain',
$resource,
$charset
);
return $this;
}
public function setHtmlPart(mixed $resource, string $charset = 'UTF-8') : static
{
$this->multipartHelper
->setContentPartForMimeType(
$this,
'text/html',
$resource,
$charset
);
return $this;
}
public function removeTextPart(int $index = 0) : bool
{
return $this->multipartHelper
->removePartByMimeType(
$this,
'text/plain',
$index
);
}
public function removeAllTextParts(bool $moveRelatedPartsBelowMessage = true) : bool
{
return $this->multipartHelper
->removeAllContentPartsByMimeType(
$this,
'text/plain',
$moveRelatedPartsBelowMessage
);
}
public function removeHtmlPart(int $index = 0) : bool
{
return $this->multipartHelper
->removePartByMimeType(
$this,
'text/html',
$index
);
}
public function removeAllHtmlParts(bool $moveRelatedPartsBelowMessage = true) : bool
{
return $this->multipartHelper
->removeAllContentPartsByMimeType(
$this,
'text/html',
$moveRelatedPartsBelowMessage
);
}
public function getAttachmentPart(int $index) : ?IMessagePart
{
return $this->getPart(
$index,
PartFilter::fromAttachmentFilter()
);
}
public function getAllAttachmentParts() : array
{
return $this->getAllParts(
PartFilter::fromAttachmentFilter()
);
}
public function getAttachmentCount() : int
{
return \count($this->getAllAttachmentParts());
}
public function addAttachmentPart(mixed $resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static
{
$this->multipartHelper
->createAndAddPartForAttachment(
$this,
$resource,
$mimeType,
(\strcasecmp($disposition, 'inline') === 0) ? 'inline' : 'attachment',
$filename,
$encoding
);
return $this;
}
public function addAttachmentPartFromFile(string $filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static
{
$handle = Psr7\Utils::streamFor(\fopen($filePath, 'r'));
if ($filename === null) {
$filename = \basename($filePath);
}
$this->addAttachmentPart($handle, $mimeType, $filename, $disposition, $encoding);
return $this;
}
public function removeAttachmentPart(int $index) : static
{
$part = $this->getAttachmentPart($index);
$this->removePart($part);
return $this;
}
public function getSignedMessageStream() : ?StreamInterface
{
return $this
->privacyHelper
->getSignedMessageStream($this);
}
public function getSignedMessageAsString() : ?string
{
return $this
->privacyHelper
->getSignedMessageAsString($this);
}
public function getSignaturePart() : ?IMessagePart
{
if (\strcasecmp($this->getContentType(), 'multipart/signed') === 0) {
return $this->getChild(1);
}
return null;
}
public function setAsMultipartSigned(string $micalg, string $protocol) : static
{
$this->privacyHelper
->setMessageAsMultipartSigned($this, $micalg, $protocol);
return $this;
}
public function setSignature(string $body) : static
{
$this->privacyHelper
->setSignature($this, $body);
return $this;
}
public function getMessageId() : ?string
{
return $this->getHeaderValue(HeaderConsts::MESSAGE_ID);
}
public function getErrorLoggingContextName() : string
{
$params = '';
if (!empty($this->getMessageId())) {
$params .= ', message-id=' . $this->getContentId();
}
$params .= ', content-type=' . $this->getContentType();
$nsClass = static::class;
$class = \substr($nsClass, (\strrpos($nsClass, '\\') ?? -1) + 1);
return $class . '(' . \spl_object_id($this) . $params . ')';
}
}