/** * @param Message $mail * @param $partStructure * @param $partNum * @param bool|true $markAsSeen * @param bool|false $isDsn */ protected function initMailPart(Message $mail, $partStructure, $partNum, $markAsSeen = true, $isDsn = false) { $options = FT_UID; if (!$markAsSeen) { $options |= FT_PEEK; } $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, $options) : imap_body($this->getImapStream(), $mail->id, $options); if ($partStructure->encoding == 1) { $data = imap_utf8($data); } elseif ($partStructure->encoding == 2) { $data = imap_binary($data); } elseif ($partStructure->encoding == 3) { $data = imap_base64($data); } elseif ($partStructure->encoding == 4) { $data = quoted_printable_decode($data); } $params = $this->getParameters($partStructure); // attachments $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null); if ($attachmentId) { if (empty($params['filename']) && empty($params['name'])) { $fileName = $attachmentId . '.' . strtolower($partStructure->subtype); } else { $fileName = !empty($params['filename']) ? $params['filename'] : $params['name']; $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding); $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding); } $attachment = new Attachment(); $attachment->id = $attachmentId; $attachment->name = $fileName; if ($this->attachmentsDir) { $replace = ['/\\s/' => '_', '/[^0-9a-zа-яіїє_\\.]/iu' => '', '/_+/' => '_', '/(^_)|(_$)/' => '']; $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName)); $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName; file_put_contents($attachment->filePath, $data); } $mail->addAttachment($attachment); } else { if (!empty($params['charset'])) { $data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding); } if ($partStructure->type == 0 && $data) { if (strtolower($partStructure->subtype) == 'plain') { $mail->textPlain .= $data; } else { $mail->textHtml .= $data; } } elseif ($partStructure->type == 1 && $partStructure->ifsubtype && (strtolower($partStructure->subtype) == 'report' && isset($params['REPORT-TYPE']) && strtolower($params['REPORT-TYPE']) == 'delivery-status')) { $mail->dsnMessage = trim($data); $isDsn = true; } elseif ($partStructure->type == 2 && $data) { if ($isDsn || strtolower($partStructure->subtype) == 'delivery-status') { $mail->dsnReport = $data; } else { $mail->textPlain .= trim($data); } } } if (!empty($partStructure->parts)) { foreach ($partStructure->parts as $subPartNum => $subPartStructure) { if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') { $this->initMailPart($mail, $subPartStructure, $partNum, $markAsSeen, $isDsn); } else { $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1), $markAsSeen, $isDsn); } } } }
/** * @param Message $mail * @param $partStructure * @param $partNum * @param bool|true $markAsSeen * @param bool|false $isDsn * @param bool|false $isFbl */ protected function initMailPart(Message $mail, $partStructure, $partNum, $markAsSeen = true, $isDsn = false, $isFbl = false) { $options = FT_UID; if (!$markAsSeen) { $options |= FT_PEEK; } $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, $options) : imap_body($this->getImapStream(), $mail->id, $options); if ($partStructure->encoding == 1) { $data = imap_utf8($data); } elseif ($partStructure->encoding == 2) { $data = imap_binary($data); } elseif ($partStructure->encoding == 3) { $data = imap_base64($data); } elseif ($partStructure->encoding == 4) { $data = quoted_printable_decode($data); } $params = $this->getParameters($partStructure); // attachments $attachmentId = $partStructure->ifid ? trim($partStructure->id, ' <>') : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null); if ($attachmentId) { if (empty($params['filename']) && empty($params['name'])) { $fileName = $attachmentId . '.' . strtolower($partStructure->subtype); } else { $fileName = !empty($params['filename']) ? $params['filename'] : $params['name']; $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding); $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding); } $attachment = new Attachment(); $attachment->id = $attachmentId; $attachment->name = $fileName; if ($this->attachmentsDir) { $replace = ['/\\s/' => '_', '/[^0-9a-zа-яіїє_\\.]/iu' => '', '/_+/' => '_', '/(^_)|(_$)/' => '']; $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName)); $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName; file_put_contents($attachment->filePath, $data); } $mail->addAttachment($attachment); } else { if (!empty($params['charset'])) { $data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding); } if (!empty($data)) { $subtype = !empty($partStructure->ifsubtype) ? strtolower($partStructure->subtype) : ''; switch ($partStructure->type) { case TYPETEXT: switch ($subtype) { case 'plain': $mail->textPlain .= $data; break; case 'html': default: $mail->textHtml .= $data; } break; case TYPEMULTIPART: if ($subtype != 'report' || empty($params['report-type'])) { break; } $reportType = strtolower($params['report-type']); switch ($reportType) { case 'delivery-status': $mail->dsnMessage = trim($data); $isDsn = true; break; case 'feedback-report': $mail->fblMessage = trim($data); $isFbl = true; break; default: // Just pass through. } break; case TYPEMESSAGE: if ($isDsn || $subtype == 'delivery-status') { $mail->dsnReport = $data; } elseif ($isFbl || $subtype == 'feedback-report') { $mail->fblReport = $data; } else { $mail->textPlain .= trim($data); } break; default: // Just pass through. } } } if (!empty($partStructure->parts)) { foreach ($partStructure->parts as $subPartNum => $subPartStructure) { if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') { $this->initMailPart($mail, $subPartStructure, $partNum, $markAsSeen, $isDsn, $isFbl); } else { $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1), $markAsSeen, $isDsn, $isFbl); } } } }