mail = $mail; $this->id = $id; $this->part = $part; $this->encoding = $encoding; $this->options = $options; } public function fetch(): string { if (0 === $this->part) { $this->data = Imap::body($this->mail->getImapStream(), $this->id, $this->options); } else { if (null !== $this->data) { return $this->data; } $this->data = Imap::fetchbody($this->mail->getImapStream(), $this->id, $this->part, $this->options); } return $this->decodeAfterFetch($this->data); } public function decodeAfterFetch(string $data): string { switch ($this->encoding) { case ENC8BIT: $this->data = \imap_utf8((string) $data); break; case ENCBINARY: $this->data = \imap_binary((string) $data); break; case ENCBASE64: $this->data = \base64_decode((string) $data, false); break; case ENCQUOTEDPRINTABLE: $this->data = \quoted_printable_decode((string) $data); break; } return $this->convertEncodingAfterFetch(); } protected function convertEncodingAfterFetch(): string { if (isset($this->charset) && !empty(\trim($this->charset))) { $this->data = $this->mail->decodeMimeStr( (string) $this->data // Data to convert ); $this->data = $this->mail->convertToUtf8( $this->data, $this->charset ); } return (null === $this->data) ? '' : $this->data; } }