Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Fetch/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions src/Fetch/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down