diff options
author | David Flatz | 2014-04-28 15:13:16 +0200 |
---|---|---|
committer | David Flatz | 2014-04-28 15:57:08 +0200 |
commit | e2e2de23c713a1b84ba0e1ab57dc26b1846cc614 (patch) | |
tree | 9c805750883ac31f71b3131bc8142f341f4bf463 /it_mail.class | |
parent | c6639d1537032199304c2d5c2b25df3517f7da9b (diff) | |
download | itools-e2e2de23c713a1b84ba0e1ab57dc26b1846cc614.tar.gz itools-e2e2de23c713a1b84ba0e1ab57dc26b1846cc614.tar.bz2 itools-e2e2de23c713a1b84ba0e1ab57dc26b1846cc614.zip |
introduce new function addrlist_escape that uses header_escape to only escape realname part of address lists
Diffstat (limited to 'it_mail.class')
-rw-r--r-- | it_mail.class | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/it_mail.class b/it_mail.class index 46095bf..5746056 100644 --- a/it_mail.class +++ b/it_mail.class @@ -82,7 +82,7 @@ function add_header($header, $value) { case 'To': foreach ((array)$value as $val) - $this->to[] = $this->header_escape($val, true); + $this->to[] = $this->addrlist_escape($val); break; case 'Subject': @@ -91,12 +91,12 @@ function add_header($header, $value) case 'Cc': foreach ((array)$value as $val) - $this->cc[] = $this->header_escape($val, true); + $this->cc[] = $this->addrlist_escape($val); break; case 'Bcc': foreach ((array)$value as $val) - $this->bcc[] = $this->header_escape($val, true); + $this->bcc[] = $this->addrlist_escape($val); break; case 'charset': @@ -108,7 +108,7 @@ function add_header($header, $value) /* FALLTHROUGH */ default: $this->header_names[] = $header; - $this->header_values[] = $this->header_escape($value, $header == 'From' || $header == 'Reply-To'); + $this->header_values[] = $header == 'From' || $header =='Reply-To' ? $this->addrlist_escape($value) : $this->header_escape($value); break; } } @@ -302,32 +302,31 @@ function send($p = array()) * @param $string String to be escaped * @return String escape suitable for sending in header line */ -function header_escape($string, $emailmode = false) +function header_escape($string) { - $escape = function ($part) - { - return preg_match('/[\x00-\x1f\x7f-\xff]/', $part) - ? ("=?{$this->charset}?Q?" . str_replace(" ", "_", preg_replace_callback('/[\x00-\x1f=\x7f-\xff]/', function($m) { return sprintf('=%02X', ord($m[0])); }, trim($part, '"'))) . "?=") - : $part; - }; + return preg_match('/[\x00-\x1f\x7f-\xff]/', $string) + ? ("=?{$this->charset}?Q?" . str_replace(" ", "_", preg_replace_callback('/[\x00-\x1f=\x7f-\xff]/', function($m) { return sprintf('=%02X', ord($m[0])); }, trim($string, '"'))) . "?=") + : $string; +} - if ($emailmode) +/** + * INTERNAL: Escape address lists in headers like From, To, Cc, Bcc + * @param $string String containing address list + * @return String suitable for sending in address list headers + */ +function addrlist_escape($string) +{ + # Exclude e-mail addresses from being encoded as + # e.g. GMail or Exchange have problems with that + foreach (explode(',', $string) as $mailbox) { - # If in emailmode (From, To, Cc, Bcc) then exclude e-mail addresses - # from being encoded as e.g. GMail or Exchange have problems with that - $emailpattern = '/^(.*)(\s+?<[^>]+@[^>]+>\s*)$/'; - foreach (explode(',', $string) as $mailbox) - { - if (preg_match($emailpattern, $mailbox, $matches)) - $result[] = $escape($matches[1]) . $matches[2]; - else - $result[] = $mailbox; - } - - return implode(',', $result); + if (preg_match('/^(.*)(\s+?<[^>]+@[^>]+>\s*)$/', $mailbox, $matches)) + $result[] = $this->header_escape($matches[1]) . $matches[2]; + else + $result[] = $mailbox; } - else - return $escape($string); + + return implode(',', $result); } |