From c9ba145f1d2f6f70a4c6ffd0a41c698382c199d2 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 7 Apr 2016 13:11:24 +0300 Subject: [PATCH] Fix. get text part of email message with single part email. --- app/fax/fax_emails.php | 2 +- app/fax/resources/functions/parse_message.php | 86 ++++++++++++------- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/app/fax/fax_emails.php b/app/fax/fax_emails.php index 234367e7b8..dcc7775e34 100644 --- a/app/fax/fax_emails.php +++ b/app/fax/fax_emails.php @@ -181,7 +181,7 @@ if (sizeof($result) != 0) { //get email body (if any) for cover page $fax_message = parse_message($connection, $email_id, FT_UID); - if ($fax_message == '') { + if ($fax_message != '') { $fax_message = strip_tags($fax_message); $fax_message = str_replace("\r\n\r\n","\r\n", $fax_message); } diff --git a/app/fax/resources/functions/parse_message.php b/app/fax/resources/functions/parse_message.php index c2945e76b2..45a32633bb 100644 --- a/app/fax/resources/functions/parse_message.php +++ b/app/fax/resources/functions/parse_message.php @@ -1,38 +1,64 @@ parts) && count($structure->parts)) { - for($i = 0; $i < count($structure->parts); $i++) { - $msg = ''; - $part = $structure->parts[$i]; - if($part->type == TYPETEXT){ - $msg = imap_fetchbody($connection, $message_number, $i+1, $option); - if($part->encoding == ENCBASE64){ - $msg = base64_decode($msg); - } - else if($part->encoding == ENCQUOTEDPRINTABLE){ - $msg = quoted_printable_decode($msg); - } - if($msg && $to_charset){ - $charset = ''; - if(isset($part->parameters) && count($part->parameters)) { - foreach($part->parameters as &$parameter){ - if($parameter->attribute == 'CHARSET') { - $charset = $parameter->value; - break; - } - } - } - if($charset){ - $msg = mb_convert_encoding($msg, $to_charset, $charset); - } - } + if(isset($structure->parts)) { + return parse_message_parts($connection, $structure, false, $message_number, $option, $to_charset); + } + return parse_message_part($connection, $structure, '1', $message_number, $option, $to_charset); +} - if($msg){ - return $msg; - } +function parse_message_parts($connection, $structure, $level, $message_number, $option, $to_charset) { + if(isset($structure->parts)) { + for($i = 0; $i < count($structure->parts); $i++) { + $part = $structure->parts[$i]; + if($part->type != TYPEMULTIPART){ + $id = $i + 1; + if($level) $id = $level . '.' . $id; + } + else{ + $id = $level; + } + + $msg = parse_message_part($connection, $part, $id, $message_number, $option, $to_charset); + if($msg){ + return $msg; } } } } + +function parse_message_part($connection, $part, $id, $message_number, $option, $to_charset){ + $msg = false; + + if($part->type == TYPETEXT){ + $msg = imap_fetchbody($connection, $message_number, $id, $option); + if($part->encoding == ENCBASE64){ + $msg = base64_decode($msg); + } + else if($part->encoding == ENCQUOTEDPRINTABLE){ + $msg = quoted_printable_decode($msg); + } + if($msg && $to_charset){ + $charset = ''; + if(isset($part->parameters) && count($part->parameters)) { + foreach($part->parameters as &$parameter){ + if($parameter->attribute == 'CHARSET') { + $charset = $parameter->value; + break; + } + } + } + if($charset){ + $msg = mb_convert_encoding($msg, $to_charset, $charset); + } + $msg = trim($msg); + } + } + + if(($part->type == TYPEMULTIPART) || ($part->type == TYPEMESSAGE)){ + $msg = parse_message_parts($connection, $part, $id, $message_number, $option, $to_charset); + } + + return $msg; +}