protected function extractPart(Part $part, $multiType = null) { $mimeType = strtok($part->contentType, ';'); $encoding = isset($part->contentTransferEncoding) ? $part->contentTransferEncoding : null; preg_match('/name="(?<filename>[a-zA-Z0-9.\\-_ ]+)"/is', $part->contentType, $attachmentName); if (isset($attachmentName['filename'])) { $this->attachments[] = array('mimetype' => $mimeType, 'filename' => $attachmentName['filename'], 'data' => $this->decodeContent($part->getContent(), $encoding, false)); return; } if ($mimeType != 'text/plain' && $mimeType != 'text/html') { return; } if ($multiType != 'multipart/alternative' || null === $this->body) { $this->body .= $this->decodeContent($part->getContent(), $encoding); } }
/** * Gets the attachment content * * @return Content */ public function getContent() { if ($this->part->getHeaders()->has('Content-Type')) { /** @var \Zend\Mail\Header\ContentType $contentTypeHeader */ $contentTypeHeader = $this->part->getHeader('Content-Type'); $contentType = $contentTypeHeader->getType(); $charset = $contentTypeHeader->getParameter('charset'); $encoding = $charset !== null ? $charset : 'ASCII'; } else { $contentType = 'text/plain'; $encoding = 'ASCII'; } $contentTransferEncoding = 'BINARY'; if ($this->part->getHeaders()->has('Content-Transfer-Encoding')) { $contentTransferEncoding = $this->part->getHeader('Content-Transfer-Encoding')->getFieldValue(); } return new Content($this->part->getContent(), $contentType, $contentTransferEncoding, $encoding); }
protected function processContent(Message &$message, Part $part) { $headers = $part->getHeaders(); $contentType = $headers->has('content-type') ? strtolower($part->getHeaderField('content-type')) : NULL; if ($this->isMultipartType($contentType)) { $boundary = $part->getHeaderField('content-type', 'boundary'); if ($boundary) { $subStructs = Decode::splitMessageStruct($part->getContent(), $boundary); $subPartNum = 1; foreach ($subStructs as $subStruct) { $subPart = new Part(['content' => $subStruct['body'], 'headers' => $subStruct['header']]); // Recursive call $subPart->partNum = $part->partNum . "." . $subPartNum++; $this->processContent($message, $subPart); } } else { $wrappedPart = new Part(['raw' => $part->getContent()]); $wrappedPart->partNum = $part->partNum; $this->processContent($message, $wrappedPart); } } elseif ($this->isTextType($contentType) || !$contentType) { $this->processTextContent($message, $contentType, self::convertContent($part->getContent(), $part->getHeaders()), $contentType ? $part->getHeaderField('content-type', 'charset') : 'US-ASCII'); } else { $this->processAttachment($message, $part); } }
/** * @param \Zend\Mail\Storage\Part $part * @return string */ private function getMessageContentDecoded($part) { $headers = $part->getHeaders(); $content = $part->getContent(); $decodedContent = $content; if ($headers->has('Content-Transfer-Encoding')) { $contentEncoding = $headers->get('contenttransferencoding')->getFieldValue(); switch ($contentEncoding) { case 'base64': $decodedContent = base64_decode($content); break; case 'quoted-printable': $decodedContent = quoted_printable_decode($content); break; default: $decodedContent = $content; } } if ($headers->has('Content-Type')) { /** @var \Zend\Mail\Header\ContentType $contentType */ $contentType = $headers->get('contenttype'); if ($charset = $contentType->getParameter('charset')) { if ($decodedCharsetContent = iconv($charset, static::DEFAULT_USED_ENCODING, $decodedContent)) { $decodedContent = $decodedCharsetContent; } } } return $decodedContent; }