From 87142b5e7e244287421370f2b50a6574ee086003 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 7 Jul 2007 23:36:24 +0000 Subject: [PATCH] Adding Mail::_encodeHeaderValue() --- mail.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/mail.php b/mail.php index 07f5a11..a97b624 100644 --- a/mail.php +++ b/mail.php @@ -253,6 +253,7 @@ class Mail $address = trim($this->registry->unsanitize($address)); $name = $this->_fetch_first_line($name); $name = trim($this->registry->unsanitize($name)); + $name = $this->_encodeHeaderValue($name); $tostring = ($name == null ? $address : "\"$name\" <$address>"); // sanitize the from field @@ -262,10 +263,12 @@ class Mail // sanitize the from name $this->fromname = $this->_fetch_first_line($this->fromname); $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname))); + $this->fromname = $this->_encodeHeaderValue($this->fromname); // sanitize the subject $this->subject = $this->_fetch_first_line($this->subject); $this->subject = trim($this->registry->unsanitize($this->subject)); + $this->subject = $this->_encodeHeaderValue($this->subject); // sanitize the body $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim); @@ -339,6 +342,37 @@ class Mail $broken = explode("\n", $string); return $broken[0]; } + + // ################################################################### + /** + * Encodes a header value (to name, fron name, subject, etc.) according + * to RFC 2047 + * + * @param string The text to encode + * + * @return string Encoded text + */ + function _encodeHeaderValue($text) + { + $enc = ''; + + for ($i = 0; $i < strlen($text); $i++) + { + $char = ord($text[$i]); + // allowed characters: a-zA-Z0-9_\+\-\*_= + if (($char >= 65 AND $char <= 90) OR ($char >= 97 AND $char <= 122) OR ($char >= 48 AND $char <= 57) OR $char == 33 OR $char == 42 OR $char == 43 OR $char == 45 OR $char == 47 OR $char == 61) + { + $enc .= $text[$i]; + } + else + { + // this is a non-ASCII character, so encode it + $enc .= '=' . strtoupper(dechex($char)); + } + } + + return '=?' . $this->charset . '?q?' . $enc . '?='; + } } /*=====================================================================*\ -- 2.22.5