protected function extractPart(Part $part, $multiType = null) { $mimeType = strtok($part->contentType, ';'); $encoding = isset($part->contentTransferEncoding) ? $part->contentTransferEncoding : null; preg_match('/name="(?<filename>[a-zA-Z0-9.\\-_ ]+)"/is', $part->contentType, $attachmentName); if (isset($attachmentName['filename'])) { $this->attachments[] = array('mimetype' => $mimeType, 'filename' => $attachmentName['filename'], 'data' => $this->decodeContent($part->getContent(), $encoding, false)); return; } if ($mimeType != 'text/plain' && $mimeType != 'text/html') { return; } if ($multiType != 'multipart/alternative' || null === $this->body) { $this->body .= $this->decodeContent($part->getContent(), $encoding); } }
/** * Gets the attachment content * * @return Content */ public function getContent() { if ($this->part->getHeaders()->has('Content-Type')) { /** @var \Zend\Mail\Header\ContentType $contentTypeHeader */ $contentTypeHeader = $this->part->getHeader('Content-Type'); $contentType = $contentTypeHeader->getType(); $charset = $contentTypeHeader->getParameter('charset'); $encoding = $charset !== null ? $charset : 'ASCII'; } else { $contentType = 'text/plain'; $encoding = 'ASCII'; } if ($this->part->getHeaders()->has('Content-Transfer-Encoding')) { $contentTransferEncoding = $this->part->getHeader('Content-Transfer-Encoding')->getFieldValue(); } else { $contentTransferEncoding = 'BINARY'; } return new Content($this->part->getContent(), $contentType, $contentTransferEncoding, $encoding); }
/** * Public constructor * * In addition to the parameters of Part::__construct() this constructor supports: * - file filename or file handle of a file with raw message content * - flags array with flags for message, keys are ignored, use constants defined in \Zend\Mail\Storage * * @param array $params * @throws Exception\RuntimeException */ public function __construct(array $params) { if (isset($params['file'])) { if (!is_resource($params['file'])) { ErrorHandler::start(); $params['raw'] = file_get_contents($params['file']); $error = ErrorHandler::stop(); if ($params['raw'] === false) { throw new Exception\RuntimeException('could not open file', 0, $error); } } else { $params['raw'] = stream_get_contents($params['file']); } } if (!empty($params['flags'])) { // set key and value to the same value for easy lookup $this->flags = array_combine($params['flags'], $params['flags']); } parent::__construct($params); }
protected function processAttachment(Message &$message, Part $part) { $name = NULL; $filename = NULL; $contentType = NULL; $headers = $part->getHeaders(); $attachment = new Attachment($part); // Get the filename and/or name for the attachment. Try the // disposition first. if ($headers->has('content-disposition')) { $name = $part->getHeaderField('content-disposition', 'name'); $filename = $part->getHeaderField('content-disposition', 'filename'); } if ($headers->has('content-type')) { $contentType = strtolower($part->getHeaderField('content-type')); $name = $name ?: $part->getHeaderField('content-type', 'name'); $filename = $filename ?: $part->getHeaderField('content-type', 'filename'); } // Store this before overwriting $attachment->origName = $name; $attachment->origFilename = $filename; // Certain mime types don't provide name info but we can try // to infer it from the mime type. if (!$filename && $headers->has('content-id')) { $filename = trim($part->getHeaderField('content-id'), " <>"); } if (!$filename && $headers->has('x-attachment-id')) { $filename = trim($part->getHeaderField('x-attachment-id'), " <>"); } // Content-Location can be the URL path to a file. If this exists // then try to get the filename from this path. if (!$filename && $headers->has('content-location')) { $filename = basename($part->getHeaderField('content-location')); } // If it's a calendar event then let's give it a nice name if (!$filename && $contentType === 'text/calendar') { $filename = 'event.ics'; } if (!$filename) { $filename = $name; } if (!$filename) { $filename = 'noname'; } // Try to add an extension if it's missing one File::addExtensionIfMissing($filename, $contentType); // If we are fortunate enough to get an attachment ID, then // use that. Otherwise we want to create on in a deterministic // way. $attachment->name = $name; $attachment->filename = $filename; $attachment->mimeType = $contentType; $attachment->generateId($message); // Attachments are saved in YYYY/MM directories if ($this->attachmentsDir) { $attachment->generateFilepath($message, $this->attachmentsDir); $this->debug("Before writing attachment to disk"); $attachment->saveToFile(); $this->debug("After file_put_contents finished"); } $message->addAttachment($attachment); $this->debug("New attachment created"); }
/** * @return null|string */ protected function getContentDispositionValue() { return $this->part->getHeaders()->has('Content-Disposition') ? $this->part->getHeader('Content-Disposition')->getFieldValue() : null; }
/** * @param Part $part * * @return null|string */ private function getPlainTextOfPart(Part $part) { try { if ($part->getHeaderField('Content-Disposition') == 'attachment') { return null; } } catch (\Exception $e) { // Zend throws an Exception, if headerField does not exist } try { // If content-type is text/plain, return its content if ($part->getHeaderField('Content-Type') == 'text/plain') { return $this->getStringFromPart($part); } } catch (\Exception $e) { // If content-type is not available (exception thrown), return its content return $this->getStringFromPart($part); } return null; }
/** * Gets the Content-Disposition for the given part * * @param Part $part The message part * @param bool $format Can be FORMAT_RAW or FORMAT_ENCODED, see HeaderInterface::FORMAT_* constants * * @return string|null */ protected function getPartContentDisposition($part, $format = HeaderInterface::FORMAT_RAW) { return $part->getHeaders()->has('Content-Disposition') ? $part->getHeader('Content-Disposition')->getFieldValue($format) : null; }
/** * @param \Zend\Mail\Storage\Part $part * @return string */ private function getMessageContentDecoded($part) { $headers = $part->getHeaders(); $content = $part->getContent(); $decodedContent = $content; if ($headers->has('Content-Transfer-Encoding')) { $contentEncoding = $headers->get('contenttransferencoding')->getFieldValue(); switch ($contentEncoding) { case 'base64': $decodedContent = base64_decode($content); break; case 'quoted-printable': $decodedContent = quoted_printable_decode($content); break; default: $decodedContent = $content; } } if ($headers->has('Content-Type')) { /** @var \Zend\Mail\Header\ContentType $contentType */ $contentType = $headers->get('contenttype'); if ($charset = $contentType->getParameter('charset')) { if ($decodedCharsetContent = iconv($charset, static::DEFAULT_USED_ENCODING, $decodedContent)) { $decodedContent = $decodedCharsetContent; } } } return $decodedContent; }
/** * Gets a header in specified format * * @param string $name The name of header, matches case-insensitive, but camel-case is replaced with dashes * @param string $format change The type of return value to 'string' or 'array' * @return Headers */ public function getHeader($name, $format = null) { return $this->part->getHeader($name, $format); }
/** * Gets the Content-Type for the given part * * @param Part $part The message part * @return \Zend\Mail\Header\ContentType|null */ protected function getPartContentType($part) { return $part->getHeaders()->has('Content-Type') ? $part->getHeader('Content-Type') : null; }