/**
  * 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
  * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
  * @throws Exception\RuntimeException
  * @return \Zend\Mime\Message
  */
 public static function createFromMessage($message, $boundary, $EOL = Mime::LINEEND)
 {
     $parts = Decode::splitMessageStruct($message, $boundary, $EOL);
     $res = new static();
     foreach ($parts as $part) {
         // now we build a new MimePart for the current Message Part:
         $properties = array();
         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;
             }
         }
         $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;
 }
 /**
  * Retrieves Message Content
  *
  * @param \Zend\Mail\Storage\Message $zendMailMessage
  * @return string|null
  */
 private function processContent($zendMailMessage)
 {
     $messageContent = '';
     if ($zendMailMessage->getBody()) {
         $parts = $zendMailMessage->getBody()->getParts();
         foreach ($parts as $part) {
             $split = Mime\Decode::splitContentType($part->type);
             if ($split['type'] == \Zend\Mime\Mime::TYPE_TEXT) {
                 $messageContent = $part->getContent();
             }
             if ($split['type'] == \Zend\Mime\Mime::MULTIPART_ALTERNATIVE) {
                 $boundary = $split['boundary'];
                 $bodyParts = Mime\Decode::splitMessageStruct($part->getContent(), $boundary);
                 foreach ($bodyParts as $bodyPart) {
                     $headers = $bodyPart['header'];
                     if ($headers->get('contenttype')->getType() == \Zend\Mime\Mime::TYPE_TEXT) {
                         $messageContent = $bodyPart['body'];
                     }
                 }
             }
         }
     }
     return $messageContent;
 }
Example #3
0
 public function testSplitInvalidMessage()
 {
     try {
         Mime\Decode::splitMessageStruct("--xxx\n", 'xxx');
     } catch (\Zend\Exception $e) {
         return;
         // ok
     }
     $this->fail('no exception raised while decoding invalid message');
 }
Example #4
0
 /**
  * Cache content and split in parts if multipart
  *
  * @throws Exception\RuntimeException
  * @return null
  */
 protected function _cacheContent()
 {
     // caching content if we can't fetch parts
     if ($this->content === null && $this->mail) {
         $this->content = $this->mail->getRawContent($this->messageNum);
     }
     if (!$this->isMultipart()) {
         return;
     }
     // split content in parts
     $boundary = $this->getHeaderField('content-type', 'boundary');
     if (!$boundary) {
         throw new Exception\RuntimeException('no boundary found in content type to split message');
     }
     $parts = Mime\Decode::splitMessageStruct($this->content, $boundary);
     if ($parts === null) {
         return;
     }
     $counter = 1;
     foreach ($parts as $part) {
         $this->parts[$counter++] = new static(array('headers' => $part['header'], 'content' => $part['body']));
     }
 }
Example #5
0
 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);
     }
 }
Example #6
0
 /**
  * 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
  * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  * @return \Zend\Mime\Message
  */
 public static function createFromMessage($message, $boundary, $EOL = Mime::LINEEND)
 {
     $parts = Decode::splitMessageStruct($message, $boundary, $EOL);
     $res = new self();
     foreach ($parts as $part) {
         // now we build a new MimePart for the current Message Part:
         $newPart = new Part($part['body']);
         foreach ($part['header'] as $key => $value) {
             /**
              * @todo check for characterset and filename
              */
             switch (strtolower($key)) {
                 case 'content-type':
                     $newPart->type = $value;
                     break;
                 case 'content-transfer-encoding':
                     $newPart->encoding = $value;
                     break;
                 case 'content-id':
                     $newPart->id = trim($value, '<>');
                     break;
                 case 'content-disposition':
                     $newPart->disposition = $value;
                     break;
                 case 'content-description':
                     $newPart->description = $value;
                     break;
                 case 'content-location':
                     $newPart->location = $value;
                     break;
                 case 'content-language':
                     $newPart->language = $value;
                     break;
                 default:
                     throw new Zend\Exception('Unknown header ignored for MimePart:' . $key);
             }
         }
         $res->addPart($newPart);
     }
     return $res;
 }