/** * 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); }
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; }
/** * 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 the headers collection * * @return Headers */ public function getHeaders() { return $this->part->getHeaders(); }
/** * 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; }