diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index e77984a..f7e1a35 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -98,6 +98,10 @@ public function __construct(Message $message, $structure, $partIdentifier = null $this->setFileName($parameters['name']); } + if (isset($structure->data)) { + $this->data = $structure->data; + } + $this->size = $structure->bytes; $this->mimeType = Message::typeIdToString($structure->type); @@ -218,10 +222,14 @@ public function saveAs($path) $streamFilter = null; } - // Fix an issue causing server to throw an error - // See: https://github.com/tedious/Fetch/issues/74 for more details - $fetch = imap_fetchbody($this->imapStream, $this->messageId, $this->partId ?: 1, FT_UID); - $result = imap_savebody($this->imapStream, $filePointer, $this->messageId, $this->partId ?: 1, FT_UID); + if (isset($this->data)) { + $result = fwrite($filePointer, $this->data); + } else { + // Fix an issue causing server to throw an error + // See: https://github.com/tedious/Fetch/issues/74 for more details + $fetch = imap_fetchbody($this->imapStream, $this->messageId, $this->partId ?: 1, FT_UID); + $result = imap_savebody($this->imapStream, $filePointer, $this->messageId, $this->partId ?: 1, FT_UID); + } if ($streamFilter) { stream_filter_remove($streamFilter); diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..b2a03ca 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -543,6 +543,48 @@ protected function processStructure($structure, $partIdentifier = null) } } + /** + * Handle legacy uuencoded attachments + * Needs PECL mailparse >= 0.9.0 + */ + if (function_exists('mailparse_uudecode_all') && (preg_match("/begin ([0-7]{3})/", $messageBody) > 0)) { + $fp = fopen('php://memory', 'w+'); + fwrite($fp, $messageBody); + $uuFiles = mailparse_uudecode_all($fp); + fclose($fp); + + if (is_array($uuFiles)) { + if (!isset($structure->parts)) { + $structure->parts = array(); + } + + foreach($uuFiles as $k => $v) { + if ($v['filename']) { + if ($k === 0) { + $messageBody = file_get_contents($v['filename']); + } else { + $obj = new \stdClass(); + $obj->disposition = 'attachment'; + $obj->subtype = strtoupper(pathinfo($v['origfilename'], PATHINFO_EXTENSION)); + $obj->bytes = filesize($v['filename']); + $obj->data = file_get_contents($v['filename']); + $obj->parameters = array( + 0 => new \stdClass() + ); + $obj->parameters[0]->attribute = 'filename'; + $obj->parameters[0]->value = $v['origfilename']; + + $structure->parts[] = $obj; + } + + unlink($v['filename']); + } + } + + $uuFiles = null; + } + } + if (strtolower($structure->subtype) === 'plain' || ($structure->type == 1 && strtolower($structure->subtype) !== 'alternative')) { if (isset($this->plaintextMessage)) { $this->plaintextMessage .= PHP_EOL . PHP_EOL;