/** * Saves the current resource to the given path. Returns true if move action was successful; false otherwise. * * @param string $outputFolder the output folder path for this resource * * @throws \Msl\ResourceProxy\Exception\ResourceMoveContentException * * @return bool */ public function moveToOutputFolder($outputFolder) { try { if ($this->message->isMultipart()) { // Getting the resource content (subject + body of the email) $contentPart = $this->message->getPart(1); $content = $contentPart->getContent(); // Check for attachment // Getting second part of message object $part = $this->message->getPart(2); // Get the attachment file name if ($part->getHeaders()->has('Content-Disposition')) { $fileName = $part->getHeaderField('Content-Disposition', 'filename'); // Get the attachment and decode $attachment = base64_decode($part->getContent()); // Save the attachment $attachmentFileName = $this->getAttachmentFileName($fileName, $outputFolder); $finalAttOutputDirectory = $this->getAttachmentFileFolder($outputFolder); if (!file_exists($finalAttOutputDirectory)) { mkdir($finalAttOutputDirectory, 0775, true); } file_put_contents($attachmentFileName, $attachment); } } else { // Getting the resource content (subject + body of the email) $content = $this->message->getContent(); } // Writing content to file // Setting the file name (output folder + message sub folder + current object string representation + timestamp) $outputFileName = $this->getContentFileName($outputFolder); // Writing the content to the output file $finalOutputDirectory = $this->getContentFileFolder($outputFolder); if (!file_exists($finalOutputDirectory)) { mkdir($finalOutputDirectory, 0775, true); } file_put_contents($outputFileName, $content); } catch (\Exception $e) { throw new Exception\ResourceMoveContentException(sprintf('Error while moving the content of resource \'%s\' to the output folder \'%s\'. Error message is: \'%s\'.', $this->toString(), $outputFolder, $e->getMessage())); } return true; }
public function testWrongMultipart() { $message = new Message(array('raw' => "Content-Type: multipart/mixed\r\n\r\ncontent")); try { $message->getPart(1); } catch (Exception\RuntimeException $e) { return; // ok } $this->fail('no exception raised while getting part from message without boundary'); }
/** * @param Message $message * * @return array|null */ public function getAttachments(Message $message) { $attachments = null; // Only multi-part will have attachments if ($message->isMultipart()) { if ($message->countParts() > 1) { $partCount = $message->countParts(); for ($i = 0; $i < $partCount; $i++) { $part = $message->getPart($i + 1); // Check for headers, to avoid exceptions if ($part->getHeaders() !== null) { // Getting disposition $disposition = null; try { $disposition = $part->getHeaderField('Content-Disposition'); } catch (\Exception $e) { // Zend throws an Exception, if headerField does not exist } if ($disposition == 'attachment') { $content = quoted_printable_decode($part->getContent()); // Encoding? try { $transferEncoding = $part->getHeaderField('Content-Transfer-Encoding'); if ($transferEncoding == 'base64') { $content = base64_decode($content); } } catch (\Exception $e) { // Zend throws an Exception, if headerField does not exist } $attachments[] = array('mime' => $part->getHeaderField('Content-Type'), 'filename' => $part->getHeaderField('Content-Disposition', 'filename'), 'content' => $content); } } } } } return $attachments; }