From fbd0b8827bd6094d52709aa9f28684d54c8a9b06 Mon Sep 17 00:00:00 2001 From: Peter Rotich Date: Sat, 27 Sep 2014 14:42:42 +0000 Subject: [PATCH] bug: Decode mailbox when parsing an email address This addresses cases where incoming emails doesn't include charset and mailbox may include non-ascii characters. --- include/class.format.php | 5 +++-- include/class.mailparse.php | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/class.format.php b/include/class.format.php index bb53e085..88cbb1c0 100644 --- a/include/class.format.php +++ b/include/class.format.php @@ -73,9 +73,10 @@ function mimedecode($text, $encoding='UTF-8') { $str.= Format::encode($part->text, $part->charset, $encoding); $text = $str; - } elseif(function_exists('iconv_mime_decode')) { + } elseif($text[0] == '=' && function_exists('iconv_mime_decode')) { $text = iconv_mime_decode($text, 0, $encoding); - } elseif(!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) { + } elseif(!strcasecmp($encoding, 'utf-8') + && function_exists('imap_utf8')) { $text = imap_utf8($text); } diff --git a/include/class.mailparse.php b/include/class.mailparse.php index fd0e179a..2c188360 100644 --- a/include/class.mailparse.php +++ b/include/class.mailparse.php @@ -326,12 +326,18 @@ function parseAddressList($address){ $parsed = Mail_RFC822::parseAddressList($address, null, null,false); - if(PEAR::isError($parsed)) + if (PEAR::isError($parsed)) return array(); + // Decode name and mailbox foreach ($parsed as $p) { $p->personal = Format::mimedecode($p->personal, $this->charset); + // Some mail clients may send ISO-8859-1 strings without proper encoding. + // Also, handle the more sane case where the mailbox is properly encoded + // against RFC2047 + $p->mailbox = Format::mimedecode($p->mailbox, $this->charset); } + return $parsed; }