Fetch icon indicating copy to clipboard operation
Fetch copied to clipboard

attachment problem

Open renalpha opened this issue 8 years ago • 2 comments

Guys, i ran into a problem fetching attachments. Solved it.

messageId = $message->getUid(); $this->imapStream = $message->getImapBox()->getImapStream(); $this->structure = $structure; if (isset($partIdentifier)) $this->partId = $partIdentifier; $parameters = Message::getParametersFromStructure($structure); if (isset($parameters['filename'])) { $this->setFileName($parameters['filename']); } elseif (isset($parameters['name'])) { $this->setFileName($parameters['name']); } if(isset($structure->bytes)){ $this->size = $structure->bytes; } ``` $this->mimeType = Message::typeIdToString($structure->type); if (isset($structure->subtype)) $this->mimeType .= '/' . strtolower($structure->subtype); $this->encoding = $structure->encoding; ``` } /** - This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output - data to a browser. * - @return string */ 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); ``` $messageBody = Message::decode($messageBody, $this->encoding); $this->data = $messageBody; ``` } return $this->data; } /** - This returns the filename of the attachment, or false if one isn't given. * - @return string */ public function getFileName() { return (isset($this->filename)) ? $this->filename : false; } /** - This function returns the mimetype of the attachment. * - @return string */ public function getMimeType() { return $this->mimeType; } /** - This returns the size of the attachment. * - @return int */ public function getSize() { return $this->size; } /** - This function returns the object that contains the structure of this attachment. * - @return \stdClass */ public function getStructure() { return $this->structure; } /** - This function saves the attachment to the passed directory, keeping the original name of the file. * - @param string $path - @return bool */ public function saveToDirectory($path) { $path = rtrim($path, '/') . '/'; if (is_dir($path)){ if($this->getFileName() != false){ return $this->saveAs($path . $this->getFileName()); } } return false; } /** - This function saves the attachment to the exact specified location. * - @param string $path - @return bool */ public function saveAs($path) { $dirname = dirname($path); if (file_exists($path)) { if (!is_writable($path)) { return false; } } elseif (!is_dir($dirname) || !is_writable($dirname)) { return false; } if (($filePointer = fopen($path, 'w')) == false) { return false; } switch ($this->encoding) { case 3: //base64 $streamFilter = stream_filter_append($filePointer, 'convert.base64-decode', STREAM_FILTER_WRITE); break; ``` case 4: //quoted-printable $streamFilter = stream_filter_append($filePointer, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE); break; default: $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 ($streamFilter) { stream_filter_remove($streamFilter); } fclose($filePointer); return $result; } protected function setFileName($text) { $this->filename = MIME::decode($text, Message::$charset); } }

renalpha avatar Sep 21 '15 10:09 renalpha

@ renalpha

Hi,

  1. What is the nature of your attachment problem - all you say is "Guys, I ran in to this problem fetching attachments. Solved it." without giving any details of the error etc.
  2. How exactly did you solve it? (I assume you just posted the corrected code above)

deemyboy avatar Nov 20 '16 14:11 deemyboy

Pls learn how to post code in github.

garex avatar Dec 27 '17 09:12 garex