fix for emails that contains another emails in rfc822 format

This commit is contained in:
Sergey Linnik 2020-04-09 01:18:06 +03:00
parent 2e9989dbb3
commit 9ccbd2a80b
2 changed files with 48 additions and 12 deletions

View File

@ -87,9 +87,33 @@ class Attachment
$this->imapStream = $message->getImapBox()->getImapStream(); $this->imapStream = $message->getImapBox()->getImapStream();
$this->structure = $structure; $this->structure = $structure;
if (isset($partIdentifier)) if (isset($partIdentifier)) {
$this->partId = $partIdentifier; $this->partId = $partIdentifier;
// message in message
if ($this->structure->type === 2 && strtoupper($this->structure->subtype) === 'RFC822') {
$this->filename = 'message.eml';
$header = imap_fetchmime($this->imapStream, $this->messageId, $this->partId, FT_UID);
if (strtolower(Message::$charset) === 'utf-8') {
try {
$hObject = imap_rfc822_parse_headers($header);
$subject = MIME::decode($hObject->subject, Message::$charset);
$subject = preg_replace('#\s+#', ' ', $subject);
$subject = preg_replace('#^(.{0,50})#u', '$1', $subject);
if ($subject) {
$this->filename = $subject . '.eml';
}
} catch (\Throwable $e) {
}
}
}
}
$parameters = Message::getParametersFromStructure($structure); $parameters = Message::getParametersFromStructure($structure);
if (!empty($parameters['filename'])) { if (!empty($parameters['filename'])) {
@ -117,9 +141,18 @@ class Attachment
public function getData() public function getData()
{ {
if (!isset($this->data)) { if (!isset($this->data)) {
$messageBody = isset($this->partId) ? if ($this->partId) {
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID) $messageBody = imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID);
: imap_body($this->imapStream, $this->messageId, FT_UID);
// message in message
if ($this->structure->type === 2 && strtoupper($this->structure->subtype) === 'RFC822') {
$header = imap_fetchmime($this->imapStream, $this->messageId, $this->partId, FT_UID);
return $this->data = $header . $messageBody;
}
} else {
$messageBody = imap_body($this->imapStream, $this->messageId, FT_UID);
}
$messageBody = Message::decode($messageBody, $this->encoding); $messageBody = Message::decode($messageBody, $this->encoding);
$this->data = $messageBody; $this->data = $messageBody;

View File

@ -267,13 +267,13 @@ class Message
$structure = $this->getStructure(); $structure = $this->getStructure();
if (!isset($structure->parts)) { if (isset($structure->parts)) {
// not multipart
$this->processStructure($structure);
} else {
// multipart // multipart
foreach ($structure->parts as $id => $part) foreach ($structure->parts as $id => $part)
$this->processStructure($part, $id + 1); $this->processStructure($part, $id + 1);
} else {
// not multipart
$this->processStructure($structure);
} }
return true; return true;
@ -515,11 +515,14 @@ class Message
{ {
$parameters = self::getParametersFromStructure($structure); $parameters = self::getParametersFromStructure($structure);
if ((!empty($parameters['name']) || !empty($parameters['filename'])) if (!empty($parameters['name']) || !empty($parameters['filename'])) {
|| (isset($structure->subtype) && strtolower($structure->subtype) == 'rfc822') $attachment = new Attachment($this, $structure, $partIdentifier);
) {
$attachment = new Attachment($this, $structure, $partIdentifier);
$this->attachments[] = $attachment; $this->attachments[] = $attachment;
} else if (strtoupper($structure->subtype) === 'RFC822') {
$attachment = new Attachment($this, $structure, $partIdentifier);
$this->attachments[] = $attachment;
// do not process subparts (maybe it should be for all attachments?)
return;
} elseif ($structure->type == 0 || $structure->type == 1) { } elseif ($structure->type == 0 || $structure->type == 1) {
$messageBody = isset($partIdentifier) ? $messageBody = isset($partIdentifier) ?
imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID | FT_PEEK) imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID | FT_PEEK)