diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index 25042e2..bd01e7d 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -87,9 +87,33 @@ class Attachment $this->imapStream = $message->getImapBox()->getImapStream(); $this->structure = $structure; - if (isset($partIdentifier)) + if (isset($partIdentifier)) { $this->partId = $partIdentifier; + // message in message + if ($this->structure->type === 2 && strtoupper($this->structure->subtype) === 'RFC822') { + $this->filename = 'message.eml'; + + $header = imap_fetchmime($this->imapStream, $this->messageId, $this->partId, FT_UID); + + if (strtolower(Message::$charset) === 'utf-8') { + try { + $hObject = imap_rfc822_parse_headers($header); + + $subject = MIME::decode($hObject->subject, Message::$charset); + $subject = preg_replace('#\s+#', ' ', $subject); + $subject = preg_replace('#^(.{0,50})#u', '$1', $subject); + + if ($subject) { + $this->filename = $subject . '.eml'; + } + } catch (\Throwable $e) { + + } + } + } + } + $parameters = Message::getParametersFromStructure($structure); if (!empty($parameters['filename'])) { @@ -117,9 +141,18 @@ class Attachment public function getData() { if (!isset($this->data)) { - $messageBody = isset($this->partId) ? - imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID) - : imap_body($this->imapStream, $this->messageId, FT_UID); + if ($this->partId) { + $messageBody = imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID); + + // message in message + if ($this->structure->type === 2 && strtoupper($this->structure->subtype) === 'RFC822') { + $header = imap_fetchmime($this->imapStream, $this->messageId, $this->partId, FT_UID); + + return $this->data = $header . $messageBody; + } + } else { + $messageBody = imap_body($this->imapStream, $this->messageId, FT_UID); + } $messageBody = Message::decode($messageBody, $this->encoding); $this->data = $messageBody; diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index a76d71c..4afb07a 100644 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -267,13 +267,13 @@ class Message $structure = $this->getStructure(); - if (!isset($structure->parts)) { - // not multipart - $this->processStructure($structure); - } else { + if (isset($structure->parts)) { // multipart foreach ($structure->parts as $id => $part) $this->processStructure($part, $id + 1); + } else { + // not multipart + $this->processStructure($structure); } return true; @@ -515,11 +515,14 @@ class Message { $parameters = self::getParametersFromStructure($structure); - if ((!empty($parameters['name']) || !empty($parameters['filename'])) - || (isset($structure->subtype) && strtolower($structure->subtype) == 'rfc822') - ) { - $attachment = new Attachment($this, $structure, $partIdentifier); + if (!empty($parameters['name']) || !empty($parameters['filename'])) { + $attachment = new Attachment($this, $structure, $partIdentifier); $this->attachments[] = $attachment; + } else if (strtoupper($structure->subtype) === 'RFC822') { + $attachment = new Attachment($this, $structure, $partIdentifier); + $this->attachments[] = $attachment; + // do not process subparts (maybe it should be for all attachments?) + return; } elseif ($structure->type == 0 || $structure->type == 1) { $messageBody = isset($partIdentifier) ? imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID | FT_PEEK)