getContentStream(); * $someChars = $part->read(4); * * $stream2 = $part->getContentStream(); * $moreChars = $part->read(4); * echo ($someChars === $moreChars); //1 * ``` * * In this case the Stream was rewound, and $stream's second call to read 4 * bytes reads the same first 4. * * @see IMessagePart::getBinaryContentStream() to get the content stream * without any charset conversions. * @see IMessagePart::saveContent() to save the binary contents to file. * @see IMessagePart::setCharsetOverride() to override the charset of the * content and ignore the charset returned from calling * IMessagePart::getCharset() when reading. * @param string $charset Optional charset for the returned stream. * @return StreamInterface|null the stream */ public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface; /** * Returns the raw data stream for the current part, if it exists, or null * if there's no content associated with the stream. * * This is basically the same as calling * {@see IMessagePart::getContentStream()}, except no automatic charset * conversion is done. Note that for non-text streams, this doesn't have an * effect, as charset conversion is not performed in that case, and is * useful only when: * * - The charset defined is not correct, and the conversion produces errors; * or * - You'd like to read the raw contents without conversion, for instance to * save it to file or allow a user to download it as-is (in a download * link for example). * * @see IMessagePart::getContentStream() to get the content stream with * charset conversions applied. * @see IMessagePart::getBinaryContentResourceHandle() to get a resource * handle instead. * @see IMessagePart::saveContent() to save the binary contents to file. * @return StreamInterface|null the stream */ public function getBinaryContentStream() : ?StreamInterface; /** * Returns a resource handle for the content's raw data stream, or null if * the part doesn't have a content stream. * * The method wraps a call to {@see IMessagePart::getBinaryContentStream()} * and returns a resource handle for the returned Stream. * * @see IMessagePart::getBinaryContentStream() to get a stream instead. * @see IMessagePart::saveContent() to save the binary contents to file. * @return resource|null the resource */ public function getBinaryContentResourceHandle() : mixed; /** * Saves the binary content of the stream to the passed file, resource or * stream. * * Note that charset conversion is not performed in this case, and the * contents of the part are saved in their binary format as transmitted (but * after any content-transfer decoding is performed). {@see * IMessagePart::getBinaryContentStream()} for a more detailed description * of the stream. * * If the passed parameter is a string, it's assumed to be a filename to * write to. The file is opened in 'w+' mode, and closed before returning. * * When passing a resource or Psr7 Stream, the resource is not closed, nor * rewound. * * @see IMessagePart::getContentStream() to get the content stream with * charset conversions applied. * @see IMessagePart::getBinaryContentStream() to get the content as a * binary stream. * @see IMessagePart::getBinaryContentResourceHandle() to get the content as * a resource handle. * @param string|resource|StreamInterface $filenameResourceOrStream */ public function saveContent($filenameResourceOrStream) : static; /** * Shortcut to reading stream content and assigning it to a string. Returns * null if the part doesn't have a content stream. * * The returned string is encoded to the passed $charset character encoding. * * @see IMessagePart::getContentStream() * @param string $charset the target charset for the returned string * @return ?string the content */ public function getContent(string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string; /** * Attaches the stream or resource handle for the part's content. The * stream is closed when another stream is attached, or the MimePart is * destroyed. * * @see IMessagePart::setContent() to pass a string as the content. * @see IMessagePart::getContentStream() to get the content stream. * @see IMessagePart::detachContentStream() to detach the content stream. * @param StreamInterface $stream the content * @param string $streamCharset the charset of $stream */ public function attachContentStream(StreamInterface $stream, string $streamCharset = MailMimeParser::DEFAULT_CHARSET) : static; /** * Detaches the content stream. * * @see IMessagePart::getContentStream() to get the content stream. * @see IMessagePart::attachContentStream() to attach a content stream. */ public function detachContentStream() : static; /** * Sets the content of the part to the passed string, resource, or stream. * * @see IMessagePart::getContentStream() to get the content stream. * @see IMessagePart::attachContentStream() to attach a content stream. * @see IMessagePart::detachContentStream() to detach the content stream. * @param string|resource|StreamInterface $resource the content. * @param string $resourceCharset the charset of the passed $resource. */ public function setContent($resource, string $resourceCharset = MailMimeParser::DEFAULT_CHARSET) : static; /** * Returns a resource handle for the string representation of this part, * containing its headers, content and children. For an IMessage, this * would be the entire RFC822 (or greater) email. * * If the part has not been modified and represents a parsed part, the * original stream should be returned. Otherwise a stream representation of * the part including its modifications should be returned. This insures * that an unmodified, signed message could be passed on that way even after * parsing and reading. * * The returned stream is not guaranteed to be RFC822 (or greater) compliant * for the following reasons: * * - The original email or part, if not modified, is returned as-is and may * not be compliant. * - Although certain parts may have been modified, an original unmodified * header from the original email or part may not be compliant. * - A user may set headers in a non-compliant format. * * @see IMessagePart::getStream() to get a Psr7 StreamInterface instead of a * resource handle. * @see IMessagePart::__toString() to write the part to a string and return * it. * @see IMessage::save() to write the part to a file, resource handle or * Psr7 stream. * @return resource the resource handle containing the part. */ public function getResourceHandle() : mixed; /** * Returns a Psr7 StreamInterface for the string representation of this * part, containing its headers, content and children. * * If the part has not been modified and represents a parsed part, the * original stream should be returned. Otherwise a stream representation of * the part including its modifications should be returned. This insures * that an unmodified, signed message could be passed on that way even after * parsing and reading. * * The returned stream is not guaranteed to be RFC822 (or greater) compliant * for the following reasons: * * - The original email or part, if not modified, is returned as-is and may * not be compliant. * - Although certain parts may have been modified, an original unmodified * header from the original email or part may not be compliant. * - A user may set headers in a non-compliant format. * * @see IMessagePart::getResourceHandle() to get a resource handle. * @see IMessagePart::__toString() to write the part to a string and return * it. * @see IMessage::save() to write the part to a file, resource handle or * Psr7 stream. * @return StreamInterface the stream containing the part. */ public function getStream() : StreamInterface; /** * Writes a string representation of this part, including its headers, * content and children to the passed file, resource, or stream. * * If the part has not been modified and represents a parsed part, the * original stream should be written to the file. Otherwise a stream * representation of the part including its modifications should be written. * This insures that an unmodified, signed message could be passed on this * way even after parsing and reading. * * The written stream is not guaranteed to be RFC822 (or greater) compliant * for the following reasons: * * - The original email or part, if not modified, is returned as-is and may * not be compliant. * - Although certain parts may have been modified, an original unmodified * header from the original email or part may not be compliant. * - A user may set headers in a non-compliant format. * * If the passed $filenameResourceOrStream is a string, it's assumed to be a * filename to write to. * * When passing a resource or Psr7 Stream, the resource is not closed, nor * rewound after being written to. * * @see IMessagePart::getResourceHandle() to get a resource handle. * @see IMessagePart::__toString() to get the part in a string. * @see IMessage::save() to write the part to a file, resource handle or * Psr7 stream. * @param string|resource|StreamInterface $filenameResourceOrStream the * file, resource, or stream to write to. * @param string $filemode Optional filemode to open a file in (if * $filenameResourceOrStream is a string) */ public function save(mixed $filenameResourceOrStream, string $filemode = 'w+') : static; /** * Returns the message/part as a string, containing its headers, content and * children. * * Convenience method for calling getContents() on * {@see IMessagePart::getStream()}. * * @see IMessagePart::getStream() to get a Psr7 StreamInterface instead of a * string. * @see IMessagePart::getResourceHandle() to get a resource handle. * @see IMessage::save() to write the part to a file, resource handle or * Psr7 stream. */ public function __toString() : string; }