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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mime;
class Message
{
protected $parts = [];
protected $mime = null;
/**
* Returns the list of all Zend\Mime\Part in the message
*
* @return Part[]
*/
public function getParts()
{
return $this->parts;
}
/**
* Sets the given array of Zend\Mime\Part as the array for the message
*
* @param array $parts
* @return self
*/
public function setParts($parts)
{
$this->parts = $parts;
return $this;
}
/**
* Append a new Zend\Mime\Part to the current message
*
* @param \Zend\Mime\Part $part
* @throws Exception\InvalidArgumentException
* @return self
*/
public function addPart(Part $part)
{
foreach ($this->getParts() as $key => $row) {
if ($part == $row) {
throw new Exception\InvalidArgumentException(sprintf(
'Provided part %s already defined.',
$part->getId()
));
}
}
$this->parts[] = $part;
return $this;
}
/**
* Check if message needs to be sent as multipart
* MIME message or if it has only one part.
*
* @return bool
*/
public function isMultiPart()
{
return (count($this->parts) > 1);
}
/**
* Set Zend\Mime\Mime object for the message
*
* This can be used to set the boundary specifically or to use a subclass of
* Zend\Mime for generating the boundary.
*
* @param \Zend\Mime\Mime $mime
* @return self
*/
public function setMime(Mime $mime)
{
$this->mime = $mime;
return $this;
}
/**
* Returns the Zend\Mime\Mime object in use by the message
*
* If the object was not present, it is created and returned. Can be used to
* determine the boundary used in this message.
*
* @return \Zend\Mime\Mime
*/
public function getMime()
{
if ($this->mime === null) {
$this->mime = new Mime();
}
return $this->mime;
}
/**
* Generate MIME-compliant message from the current configuration
*
* This can be a multipart message if more than one MIME part was added. If
* only one part is present, the content of this part is returned. If no
* part had been added, an empty string is returned.
*
* Parts are separated by the mime boundary as defined in Zend\Mime\Mime. If
* {@link setMime()} has been called before this method, the Zend\Mime\Mime
* object set by this call will be used. Otherwise, a new Zend\Mime\Mime object
* is generated and used.
*
* @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
* @return string
*/
public function generateMessage($EOL = Mime::LINEEND)
{
if (! $this->isMultiPart()) {
if (empty($this->parts)) {
return '';
}
$part = current($this->parts);
$body = $part->getContent($EOL);
} else {
$mime = $this->getMime();
$boundaryLine = $mime->boundaryLine($EOL);
$body = 'This is a message in Mime Format. If you see this, '
. "your mail reader does not support this format." . $EOL;
foreach (array_keys($this->parts) as $p) {
$body .= $boundaryLine
. $this->getPartHeaders($p, $EOL)
. $EOL
. $this->getPartContent($p, $EOL);
}
$body .= $mime->mimeEnd($EOL);
}
return trim($body);
}
/**
* Get the headers of a given part as an array
*
* @param int $partnum
* @return array
*/
public function getPartHeadersArray($partnum)
{
return $this->parts[$partnum]->getHeadersArray();
}
/**
* Get the headers of a given part as a string
*
* @param int $partnum
* @param string $EOL
* @return string
*/
public function getPartHeaders($partnum, $EOL = Mime::LINEEND)
{
return $this->parts[$partnum]->getHeaders($EOL);
}
/**
* Get the (encoded) content of a given part as a string
*
* @param int $partnum
* @param string $EOL
* @return string
*/
public function getPartContent($partnum, $EOL = Mime::LINEEND)
{
return $this->parts[$partnum]->getContent($EOL);
}
/**
* Explode MIME multipart string into separate parts
*
* Parts consist of the header and the body of each MIME part.
*
* @param string $body
* @param string $boundary
* @throws Exception\RuntimeException
* @return array
*/
// @codingStandardsIgnoreStart
protected static function _disassembleMime($body, $boundary)
{
// @codingStandardsIgnoreEnd
$start = 0;
$res = [];
// find every mime part limiter and cut out the
// string before it.
// the part before the first boundary string is discarded:
$p = strpos($body, '--' . $boundary."\n", $start);
if ($p === false) {
// no parts found!
return [];
}
// position after first boundary line
$start = $p + 3 + strlen($boundary);
while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
$res[] = substr($body, $start, $p - $start);
$start = $p + 3 + strlen($boundary);
}
// no more parts, find end boundary
$p = strpos($body, '--' . $boundary . '--', $start);
if ($p === false) {
throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
}
// the remaining part also needs to be parsed:
$res[] = substr($body, $start, $p - $start);
return $res;
}
/**
* Decodes a MIME encoded string and returns a Zend\Mime\Message object with
* all the MIME parts set according to the given string
*
* @param string $message
* @param string $boundary Multipart boundary; if omitted, $message will be
* treated as a single part.
* @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
* @throws Exception\RuntimeException
* @return Message
*/
public static function createFromMessage($message, $boundary = null, $EOL = Mime::LINEEND)
{
if ($boundary) {
$parts = Decode::splitMessageStruct($message, $boundary, $EOL);
} else {
Decode::splitMessage($message, $headers, $body, $EOL);
$parts = [[
'header' => $headers,
'body' => $body,
]];
}
$res = new static();
foreach ($parts as $part) {
// now we build a new MimePart for the current Message Part:
$properties = [];
foreach ($part['header'] as $header) {
/** @var \Zend\Mail\Header\HeaderInterface $header */
/**
* @todo check for characterset and filename
*/
$fieldName = $header->getFieldName();
$fieldValue = $header->getFieldValue();
switch (strtolower($fieldName)) {
case 'content-type':
$properties['type'] = $fieldValue;
break;
case 'content-transfer-encoding':
$properties['encoding'] = $fieldValue;
break;
case 'content-id':
$properties['id'] = trim($fieldValue, '<>');
break;
case 'content-disposition':
$properties['disposition'] = $fieldValue;
break;
case 'content-description':
$properties['description'] = $fieldValue;
break;
case 'content-location':
$properties['location'] = $fieldValue;
break;
case 'content-language':
$properties['language'] = $fieldValue;
break;
default:
// Ignore unknown header
break;
}
}
$body = $part['body'];
if (isset($properties['encoding'])) {
switch ($properties['encoding']) {
case 'quoted-printable':
$body = quoted_printable_decode($body);
break;
case 'base64':
$body = base64_decode($body);
break;
}
}
$newPart = new Part($body);
foreach ($properties as $key => $value) {
$newPart->$key = $value;
}
$res->addPart($newPart);
}
return $res;
}
}