1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
namespace ZBateson\MailMimeParser; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message\Part\MimePart; |
10
|
|
|
use ZBateson\MailMimeParser\Message\PartFilter; |
11
|
|
|
use GuzzleHttp\Psr7; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A parsed mime message with optional mime parts depending on its type. |
15
|
|
|
* |
16
|
|
|
* A mime message may have any number of mime parts, and each part may have any |
17
|
|
|
* number of sub-parts, etc... |
18
|
|
|
* |
19
|
|
|
* @author Zaahid Bateson |
20
|
|
|
*/ |
21
|
|
|
class Message extends MimePart |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Overridden to close the main message StreamInterface used by this |
25
|
|
|
* message. |
26
|
|
|
*/ |
27
|
6 |
|
public function __destruct() |
28
|
|
|
{ |
29
|
6 |
|
if ($this->stream !== null) { |
30
|
6 |
|
$this->stream->close(); |
31
|
|
|
} |
32
|
6 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Convenience method to parse a handle or string into a Message without |
36
|
|
|
* requiring including MailMimeParser, instantiating it, and calling parse. |
37
|
|
|
* |
38
|
|
|
* @param resource|string $handleOrString the resource handle to the input |
39
|
|
|
* stream of the mime message, or a string containing a mime message |
40
|
|
|
*/ |
41
|
|
|
public static function from($handleOrString) |
42
|
|
|
{ |
43
|
|
|
$mmp = new MailMimeParser(); |
44
|
|
|
return $mmp->parse($handleOrString); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the text/plain part at the given index (or null if not found.) |
49
|
|
|
* |
50
|
|
|
* @param int $index |
51
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MimePart |
52
|
|
|
*/ |
53
|
1 |
|
public function getTextPart($index = 0) |
54
|
|
|
{ |
55
|
1 |
|
return $this->getPart( |
56
|
1 |
|
$index, |
57
|
1 |
|
$this->partFilterFactory->newFilterFromInlineContentType('text/plain') |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the number of text/plain parts in this message. |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
1 |
|
public function getTextPartCount() |
67
|
|
|
{ |
68
|
1 |
|
return $this->getPartCount( |
69
|
1 |
|
$this->partFilterFactory->newFilterFromInlineContentType('text/plain') |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the text/html part at the given index (or null if not found.) |
75
|
|
|
* |
76
|
|
|
* @param $index |
77
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MimePart |
78
|
|
|
*/ |
79
|
1 |
|
public function getHtmlPart($index = 0) |
80
|
|
|
{ |
81
|
1 |
|
return $this->getPart( |
82
|
1 |
|
$index, |
83
|
1 |
|
$this->partFilterFactory->newFilterFromInlineContentType('text/html') |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the number of text/html parts in this message. |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
1 |
|
public function getHtmlPartCount() |
93
|
|
|
{ |
94
|
1 |
|
return $this->getPartCount( |
95
|
1 |
|
$this->partFilterFactory->newFilterFromInlineContentType('text/html') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns a string containing the entire body of a signed message for |
101
|
|
|
* verification. |
102
|
|
|
* |
103
|
|
|
* @return string or null if the message doesn't have any children, or the |
104
|
|
|
* child returns null for getHandle |
105
|
|
|
*/ |
106
|
2 |
|
public function getMessageStringForSignatureVerification() |
107
|
|
|
{ |
108
|
2 |
|
$child = $this->getChild(0); |
109
|
2 |
|
if ($child !== null && $child->getHandle() !== null) { |
110
|
1 |
|
$normalized = preg_replace( |
111
|
1 |
|
'/\r\n|\r|\n/', |
112
|
1 |
|
"\r\n", |
113
|
1 |
|
stream_get_contents($child->getHandle()) |
114
|
|
|
); |
115
|
1 |
|
return $normalized; |
116
|
|
|
} |
117
|
1 |
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the signature part of a multipart/signed message. |
122
|
|
|
* |
123
|
|
|
* The part returned is the part containing a Content-Type matching the one |
124
|
|
|
* defined in the multipart/signed part's "protocol" parameter. |
125
|
|
|
* |
126
|
|
|
* @return MimePart |
127
|
|
|
*/ |
128
|
|
|
public function getSignaturePart() |
129
|
|
|
{ |
130
|
|
|
return $this->getChild( |
131
|
|
|
0, |
132
|
|
|
$this->partFilterFactory->newFilterFromArray([ |
133
|
|
|
'signedpart' => PartFilter::FILTER_INCLUDE |
134
|
|
|
]) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the attachment part at the given 0-based index, or null if none |
140
|
|
|
* is set. |
141
|
|
|
* |
142
|
|
|
* @param int $index |
143
|
|
|
* @return MessagePart |
|
|
|
|
144
|
|
|
*/ |
145
|
1 |
|
public function getAttachmentPart($index) |
146
|
|
|
{ |
147
|
1 |
|
$attachments = $this->getAllAttachmentParts(); |
148
|
1 |
|
if (!isset($attachments[$index])) { |
149
|
1 |
|
return null; |
150
|
|
|
} |
151
|
1 |
|
return $attachments[$index]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns all attachment parts. |
156
|
|
|
* |
157
|
|
|
* "Attachments" are any non-multipart, non-signature and any text or html |
158
|
|
|
* html part witha Content-Disposition set to 'attachment'. |
159
|
|
|
* |
160
|
|
|
* @return MessagePart[] |
161
|
|
|
*/ |
162
|
1 |
|
public function getAllAttachmentParts() |
163
|
|
|
{ |
164
|
1 |
|
$parts = $this->getAllParts( |
165
|
1 |
|
$this->partFilterFactory->newFilterFromArray([ |
166
|
1 |
|
'multipart' => PartFilter::FILTER_EXCLUDE |
167
|
|
|
]) |
168
|
|
|
); |
169
|
1 |
|
return array_values(array_filter( |
170
|
1 |
|
$parts, |
171
|
1 |
|
function ($part) { |
172
|
|
|
return !( |
173
|
1 |
|
$part->isTextPart() |
174
|
1 |
|
&& $part->getContentDisposition() === 'inline' |
175
|
|
|
); |
176
|
1 |
|
} |
177
|
|
|
)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the number of attachments available. |
182
|
|
|
* |
183
|
|
|
* @return int |
184
|
|
|
*/ |
185
|
1 |
|
public function getAttachmentCount() |
186
|
|
|
{ |
187
|
1 |
|
return count($this->getAllAttachmentParts()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns a resource handle where the 'inline' text/plain content at the |
192
|
|
|
* passed $index can be read or null if unavailable. |
193
|
|
|
* |
194
|
|
|
* @param int $index |
195
|
|
|
* @param string $charset |
196
|
|
|
* @return resource |
197
|
|
|
*/ |
198
|
1 |
|
public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) |
199
|
|
|
{ |
200
|
1 |
|
$textPart = $this->getTextPart($index); |
201
|
1 |
|
if ($textPart !== null) { |
202
|
1 |
|
return $textPart->getContentResourceHandle($charset); |
203
|
|
|
} |
204
|
1 |
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the content of the inline text/plain part at the given index. |
209
|
|
|
* |
210
|
|
|
* Reads the entire stream content into a string and returns it. Returns |
211
|
|
|
* null if the message doesn't have an inline text part. |
212
|
|
|
* |
213
|
|
|
* @param int $index |
214
|
|
|
* @param string $charset |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
1 |
|
public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) |
218
|
|
|
{ |
219
|
1 |
|
$part = $this->getTextPart($index); |
220
|
1 |
|
if ($part !== null) { |
221
|
1 |
|
return $part->getContent($charset); |
222
|
|
|
} |
223
|
1 |
|
return null; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns a resource handle where the 'inline' text/html content at the |
228
|
|
|
* passed $index can be read or null if unavailable. |
229
|
|
|
* |
230
|
|
|
* @param int $index |
231
|
|
|
* @param string $charset |
232
|
|
|
* @return resource |
233
|
|
|
*/ |
234
|
1 |
|
public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) |
235
|
|
|
{ |
236
|
1 |
|
$htmlPart = $this->getHtmlPart($index); |
237
|
1 |
|
if ($htmlPart !== null) { |
238
|
1 |
|
return $htmlPart->getContentResourceHandle($charset); |
239
|
|
|
} |
240
|
1 |
|
return null; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns the content of the inline text/html part at the given index. |
245
|
|
|
* |
246
|
|
|
* Reads the entire stream content into a string and returns it. Returns |
247
|
|
|
* null if the message doesn't have an inline html part. |
248
|
|
|
* |
249
|
|
|
* @param int $index |
250
|
|
|
* @param string $charset |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
1 |
|
public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) |
254
|
|
|
{ |
255
|
1 |
|
$part = $this->getHtmlPart($index); |
256
|
1 |
|
if ($part !== null) { |
257
|
1 |
|
return $part->getContent($charset); |
258
|
|
|
} |
259
|
1 |
|
return null; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns true if either a Content-Type or Mime-Version header are defined |
264
|
|
|
* in this Message. |
265
|
|
|
* |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
3 |
|
public function isMime() |
269
|
|
|
{ |
270
|
3 |
|
$contentType = $this->getHeaderValue('Content-Type'); |
271
|
3 |
|
$mimeVersion = $this->getHeaderValue('Mime-Version'); |
272
|
3 |
|
return ($contentType !== null || $mimeVersion !== null); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Saves the message as a MIME message to the passed resource handle. |
277
|
|
|
* |
278
|
|
|
* @param resource $handle |
279
|
|
|
*/ |
280
|
1 |
|
public function save($handle) |
281
|
|
|
{ |
282
|
1 |
|
if ($this->stream !== null) { |
283
|
1 |
|
$dest = Psr7\stream_for($handle); |
|
|
|
|
284
|
1 |
|
$this->stream->rewind(); |
285
|
1 |
|
Psr7\copy_to_stream($this->stream, $dest); |
|
|
|
|
286
|
|
|
// don't close when out of scope |
287
|
1 |
|
$dest->detach(); |
288
|
|
|
} |
289
|
1 |
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Shortcut to call Message::save with a php://temp stream and return the |
293
|
|
|
* written email message as a string. |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
1 |
|
public function __toString() |
298
|
|
|
{ |
299
|
1 |
|
if ($this->stream !== null) { |
300
|
1 |
|
$this->stream->rewind(); |
301
|
1 |
|
return $this->stream->getContents(); |
302
|
|
|
} |
303
|
|
|
return ''; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
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