/** * Encodes a string containing email addresses according to RFC 2047. * * This differs from MIME::encode() because it keeps email addresses legal, * only encoding the personal information. * * @param string $addresses The email addresses to encode. * @param string $charset The character set of the text. * @param string $defserver The default domain to append to mailboxes. * * @return string The text, encoded only if it contains non-ascii * characters */ public function encodeAddress($addresses, $charset = null, $defserver = null) { if (is_array($addresses)) { $addr_arr = $addresses; } else { /* parseAddressList() does not process the null entry * 'undisclosed-recipients:;' correctly. */ if (preg_match('/undisclosed-recipients:\\s*;/i', trim($addresses))) { return $addresses; } $parser =& new Mail_RFC822(); $addr_arr = $parser->parseAddressList($addresses, $defserver, true, false); } $text = ''; if (is_array($addr_arr)) { foreach ($addr_arr as $addr) { // Check for groups. if (!empty($addr->groupname)) { $text .= Horde_MIME::encode($addr->groupname, $charset) . ': ' . Horde_MIME::encodeAddress($addr->addresses) . '; '; } else { if (empty($addr->personal)) { $personal = ''; } else { if (substr($addr->personal, 0, 1) == '"' && substr($addr->personal, -1) == '"') { $addr->personal = stripslashes(substr($addr->personal, 1, -1)); } $personal = Horde_MIME::encode($addr->personal, $charset); } $text .= Horde_MIME::trimEmailAddress(Horde_MIME::rfc822WriteAddress($addr->mailbox, $addr->host, $personal)) . ', '; } } } return rtrim($text, ' ,'); }
/** * Take a set of headers and make sure they are encoded properly. * * @param array $headers The headers to encode. * @param string $charset The character set to use. * * @return array The array of encoded headers. */ public function encode($headers, $charset) { $addressKeys = array('To', 'Cc', 'Bcc', 'From'); $asciikeys = array('MIME-Version', 'Received', 'Message-ID', 'Date', 'Content-Disposition', 'Content-Transfer-Encoding', 'Content-ID', 'Content-Type', 'Content-Description'); foreach ($headers as $key => $val) { if (is_array($val)) { foreach ($val as $key2 => $val2) { $headers[$key][$key2] = Horde_MIME::wrapHeaders($key, $val2, $this->getEOL()); } } else { if (in_array($key, $addressKeys)) { $text = Horde_MIME::encodeAddress($val, $charset, $this->_defaultServer); } else { $text = Horde_MIME::encode($val, in_array($key, $asciikeys) ? 'US-ASCII' : $charset); } $headers[$key] = Horde_MIME::wrapHeaders($key, $text, $this->getEOL()); } } return $headers; }
/** * Add the appropriate MIME headers for this part to an existing array. * * @param array $headers An array of any other headers for the part. * * @return array The headers, with the MIME headers added. */ public function header($headers = array()) { $eol = $this->getEOL(); $ptype = $this->getPrimaryType(); /* Get the character set for this part. */ $charset = $this->getCharset(); /* Get the Content-Type - this is ALWAYS required. */ $ctype = $this->getType(true); foreach ($this->getAllContentTypeParameters() as $key => $value) { /* Skip the charset key since that would have already been * added to $ctype by getType(). */ if ($key == 'charset') { continue; } $encode_2231 = Horde_MIME::encodeRFC2231($key, $value, $charset); /* Try to work around non RFC 2231-compliant MUAs by sending both * a RFC 2047-like parameter name and then the correct RFC 2231 * parameter. See: * http://lists.horde.org/archives/dev/Week-of-Mon-20040426/014240.html */ /*if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) && ((strpos($encode_2231, '*=') !== false) || (strpos($encode_2231, '*0=') !== false))) { $ctype .= '; ' . $key . '="' . MIME::encode($value, $charset) . '"'; }*/ $ctype .= '; ' . $encode_2231; } $headers['Content-Type'] = Horde_MIME::wrapHeaders('Content-Type', $ctype, $eol); /* Get the description, if any. */ if ($descrip = $this->getDescription()) { $headers['Content-Description'] = Horde_MIME::wrapHeaders('Content-Description', Horde_MIME::encode($descrip, $charset), $eol); } /* message/* parts require no additional header information. */ if ($ptype == 'message') { return $headers; } /* Don't show Content-Disposition for multipart messages unless there is a name parameter. */ $name = $this->getName(); if ($ptype != 'multipart' || !empty($name)) { $disp = $this->getDisposition(); /* Add any disposition parameter information, if available. */ if (!empty($name)) { $encode_2231 = Horde_MIME::encodeRFC2231('filename', $name, $charset); /* Same broken RFC 2231 workaround as above. */ /*if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) && ((strpos($encode_2231, '*=') !== false) || (strpos($encode_2231, '*0=') !== false))) { $disp .= '; filename="' . MIME::encode($name, $charset) . '"'; }*/ $disp .= '; ' . $encode_2231; } $headers['Content-Disposition'] = Horde_MIME::wrapHeaders('Content-Disposition', $disp, $eol); } /* Add transfer encoding information. */ $headers['Content-Transfer-Encoding'] = $this->getTransferEncoding(); /* Add content ID information. */ if (!is_null($this->_contentid)) { $headers['Content-ID'] = $this->_contentid; } return $headers; }