Skip to content

Commit 4aae914

Browse files
freescout-helpdeskWebklex
authored andcommitted
Fix sender name in non-latin emails sent from Gmail (#155)
* Fix sender name in non-latin emails sent from Gmail * Fix broken non-latin characters in body for ASCII (us-ascii) charset
1 parent 9579ee4 commit 4aae914

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/IMAP/Message.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,12 @@ private function parseAddresses($list) {
461461
$address->personal = false;
462462
}
463463

464-
$address->personal = imap_utf8($address->personal);
464+
$personalParts = imap_mime_header_decode($address->personal);
465+
466+
$address->personal = '';
467+
foreach ($personalParts as $p) {
468+
$address->personal .= $p->text;
469+
}
465470

466471
$address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false;
467472
$address->full = ($address->personal) ? $address->personal.' <'.$address->mail.'>' : $address->mail;
@@ -526,7 +531,21 @@ private function fetchStructure($structure, $partNumber = null) {
526531

527532
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | FT_UID);
528533
$content = $this->decodeString($content, $structure->encoding);
529-
$content = $this->convertEncoding($content, $encoding);
534+
535+
// We don't need to do convertEncoding() if charset is ASCII (us-ascii):
536+
// ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
537+
// https://stackoverflow.com/a/11303410
538+
//
539+
// us-ascii is the same as ASCII:
540+
// ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
541+
// prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
542+
// based on the typographical symbols predominantly in use there.
543+
// https://en.wikipedia.org/wiki/ASCII
544+
//
545+
// convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
546+
if ($encoding != 'us-ascii') {
547+
$content = $this->convertEncoding($content, $encoding);
548+
}
530549

531550
$body = new \stdClass;
532551
$body->type = "text";
@@ -545,7 +564,9 @@ private function fetchStructure($structure, $partNumber = null) {
545564

546565
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | FT_UID);
547566
$content = $this->decodeString($content, $structure->encoding);
548-
$content = $this->convertEncoding($content, $encoding);
567+
if ($encoding != 'us-ascii') {
568+
$content = $this->convertEncoding($content, $encoding);
569+
}
549570

550571
$body = new \stdClass;
551572
$body->type = "html";

0 commit comments

Comments
 (0)