3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
15 protected $parts = array();
16 protected $mime = null;
19 * Returns the list of all Zend\Mime\Part in the message
21 * @return array of \Zend\Mime\Part
23 public function getParts()
29 * Sets the given array of Zend\Mime\Part as the array for the message
33 public function setParts($parts)
35 $this->parts
= $parts;
39 * Append a new Zend\Mime\Part to the current message
41 * @param \Zend\Mime\Part $part
43 public function addPart(Part
$part)
46 * @todo check for duplicate object handle
48 $this->parts
[] = $part;
52 * Check if message needs to be sent as multipart
53 * MIME message or if it has only one part.
57 public function isMultiPart()
59 return (count($this->parts
) > 1);
63 * Set Zend\Mime\Mime object for the message
65 * This can be used to set the boundary specifically or to use a subclass of
66 * Zend\Mime for generating the boundary.
68 * @param \Zend\Mime\Mime $mime
70 public function setMime(Mime
$mime)
76 * Returns the Zend\Mime\Mime object in use by the message
78 * If the object was not present, it is created and returned. Can be used to
79 * determine the boundary used in this message.
81 * @return \Zend\Mime\Mime
83 public function getMime()
85 if ($this->mime
=== null) {
86 $this->mime
= new Mime();
93 * Generate MIME-compliant message from the current configuration
95 * This can be a multipart message if more than one MIME part was added. If
96 * only one part is present, the content of this part is returned. If no
97 * part had been added, an empty string is returned.
99 * Parts are separated by the mime boundary as defined in Zend\Mime\Mime. If
100 * {@link setMime()} has been called before this method, the Zend\Mime\Mime
101 * object set by this call will be used. Otherwise, a new Zend\Mime\Mime object
102 * is generated and used.
104 * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
107 public function generateMessage($EOL = Mime
::LINEEND
)
109 if (!$this->isMultiPart()) {
110 $part = current($this->parts
);
111 $body = $part->getContent($EOL);
113 $mime = $this->getMime();
115 $boundaryLine = $mime->boundaryLine($EOL);
116 $body = 'This is a message in Mime Format. If you see this, '
117 . "your mail reader does not support this format." . $EOL;
119 foreach (array_keys($this->parts
) as $p) {
120 $body .= $boundaryLine
121 . $this->getPartHeaders($p, $EOL)
123 . $this->getPartContent($p, $EOL);
126 $body .= $mime->mimeEnd($EOL);
133 * Get the headers of a given part as an array
135 * @param int $partnum
138 public function getPartHeadersArray($partnum)
140 return $this->parts
[$partnum]->getHeadersArray();
144 * Get the headers of a given part as a string
146 * @param int $partnum
150 public function getPartHeaders($partnum, $EOL = Mime
::LINEEND
)
152 return $this->parts
[$partnum]->getHeaders($EOL);
156 * Get the (encoded) content of a given part as a string
158 * @param int $partnum
162 public function getPartContent($partnum, $EOL = Mime
::LINEEND
)
164 return $this->parts
[$partnum]->getContent($EOL);
168 * Explode MIME multipart string into separate parts
170 * Parts consist of the header and the body of each MIME part.
172 * @param string $body
173 * @param string $boundary
174 * @throws Exception\RuntimeException
177 protected static function _disassembleMime($body, $boundary)
181 // find every mime part limiter and cut out the
183 // the part before the first boundary string is discarded:
184 $p = strpos($body, '--' . $boundary."\n", $start);
190 // position after first boundary line
191 $start = $p +
3 +
strlen($boundary);
193 while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
194 $res[] = substr($body, $start, $p-$start);
195 $start = $p +
3 +
strlen($boundary);
198 // no more parts, find end boundary
199 $p = strpos($body, '--' . $boundary . '--', $start);
201 throw new Exception\
RuntimeException('Not a valid Mime Message: End Missing');
204 // the remaining part also needs to be parsed:
205 $res[] = substr($body, $start, $p-$start);
210 * Decodes a MIME encoded string and returns a Zend\Mime\Message object with
211 * all the MIME parts set according to the given string
213 * @param string $message
214 * @param string $boundary
215 * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
216 * @throws Exception\RuntimeException
217 * @return \Zend\Mime\Message
219 public static function createFromMessage($message, $boundary, $EOL = Mime
::LINEEND
)
221 $parts = Decode
::splitMessageStruct($message, $boundary, $EOL);
224 foreach ($parts as $part) {
226 // now we build a new MimePart for the current Message Part:
227 $properties = array();
228 foreach ($part['header'] as $header) {
229 /** @var \Zend\Mail\Header\HeaderInterface $header */
231 * @todo check for characterset and filename
234 $fieldName = $header->getFieldName();
235 $fieldValue = $header->getFieldValue();
236 switch (strtolower($fieldName)) {
238 $properties['type'] = $fieldValue;
240 case 'content-transfer-encoding':
241 $properties['encoding'] = $fieldValue;
244 $properties['id'] = trim($fieldValue,'<>');
246 case 'content-disposition':
247 $properties['disposition'] = $fieldValue;
249 case 'content-description':
250 $properties['description'] = $fieldValue;
252 case 'content-location':
253 $properties['location'] = $fieldValue;
255 case 'content-language':
256 $properties['language'] = $fieldValue;
259 throw new Exception\
RuntimeException('Unknown header ignored for MimePart:' . $fieldName);
263 $body = $part['body'];
265 if (isset($properties['encoding'])) {
266 switch ($properties['encoding']) {
267 case 'quoted-printable':
268 $body = quoted_printable_decode($body);
271 $body = base64_decode($body);
276 $newPart = new Part($body);
277 foreach ($properties as $key => $value) {
278 $newPart->$key = $value;
280 $res->addPart($newPart);