From c6ce30d4f80e155e2caf6d24f8ac873fd760bb09 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Mon, 15 Sep 2008 13:22:07 +0000 Subject: Fix bug with email addresses including full name and GMail --- it_mail.class | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/it_mail.class b/it_mail.class index dedf950..d67effb 100644 --- a/it_mail.class +++ b/it_mail.class @@ -81,7 +81,7 @@ function add_header($header, $value) { case 'To': foreach ((array)$value as $val) - $this->to[] = $this->header_escape($val); + $this->to[] = $this->header_escape($val, true); break; case 'Subject': @@ -90,12 +90,12 @@ function add_header($header, $value) case 'Cc': foreach ((array)$value as $val) - $this->cc[] = $this->header_escape($val); + $this->cc[] = $this->header_escape($val, true); break; case 'Bcc': foreach ((array)$value as $val) - $this->bcc[] = $this->header_escape($val); + $this->bcc[] = $this->header_escape($val, true); break; case 'charset': @@ -107,7 +107,7 @@ function add_header($header, $value) /* FALLTHROUGH */ default: $this->header_names[] = $header; - $this->header_values[] = $this->header_escape($value); + $this->header_values[] = $this->header_escape($value, $header == 'From'); break; } } @@ -294,9 +294,22 @@ function send() * @param $string String to be escaped * @return String escape suitable for sending in header line */ -function header_escape($string) +function header_escape($string, $emailmode = false) { - return preg_match('/[\x00-\x1f\x7f-\xff]/', $string) ? ("=?iso-8859-1?Q?" . preg_replace('/[\x00-\x20=\x7f-\xff]/e', "sprintf('=%02X', ord('\\0'))", $string) . "?=") : $string; + # If in emailmode (From, To, Cc, Bcc) then exclude + # from being encoded as e.g. GMail has problems with that + # Otherwise encode whole string at once, i.e. do not split + $emailpattern = $emailmode ? '/(\s*<[^>]+@[^>]+>[,\s]*)/' : '/DO_NOT_SLPIT{64738}/'; + + foreach (preg_split($emailpattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) + { + # Encode if not email address and contains special chars + $result .= !preg_match($emailpattern, $part) && preg_match('/[\x00-\x1f\x7f-\xff]/', $part) + ? ("=?iso-8859-1?Q?" . str_replace(" ", "_", preg_replace('/[\x00-\x1f=\x7f-\xff]/e', "sprintf('=%02X', ord('\\0'))", $part)) . "?=") + : $part; + } + + return $result; } /** -- cgit v1.2.3