4 +-----------------------------------------------------------------------+
5 | program/include/rcube_mail_mime.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2007-2008, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Extend PEAR:Mail_mime class and override encodeHeaders method |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: sendmail.inc 506 2007-03-14 00:39:51Z thomasb $
24 * Replacement PEAR:Mail_mime with some additional or overloaded methods
28 class rcube_mail_mime extends Mail_mime
31 * Set build parameters
33 function setParam($param)
35 if (is_array($param)) {
36 $this->_build_params = array_merge($this->_build_params, $param);
41 * Adds an image to the list of embedded images.
43 * @param string $file The image file name OR image data itself
44 * @param string $c_type The content type
45 * @param string $name The filename of the image.
46 * Only use if $file is the image data
47 * @param bool $isfilename Whether $file is a filename or not
49 * @param string $contentid Desired Content-ID of MIME part
50 * Defaults to generated unique ID
51 * @return mixed true on success or PEAR_Error object
54 function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true, $contentid = '')
56 $filedata = ($isfilename === true) ? $this->_file2str($file) : $file;
57 if ($isfilename === true) {
58 $filename = ($name == '' ? $file : $name);
64 if (PEAR::isError($filedata)) {
68 if ($contentid == '') {
69 $contentid = md5(uniqid(time()));
72 $this->_html_images[] = array(
84 * returns the HTML body portion of the message
85 * @return string HTML body of the message
88 function getHTMLBody()
90 return $this->_htmlbody;
95 * Creates a new mimePart object, using multipart/mixed as
96 * the initial content-type and returns it during the
99 * @return object The multipart/mixed mimePart object
102 function &_addMixedPart()
104 $params['content_type'] = $this->_headers['Content-Type'] ? $this->_headers['Content-Type'] : 'multipart/mixed';
105 $ret = new Mail_mimePart('', $params);
111 * Encodes a header as per RFC2047
113 * @param array $input The header data to encode
114 * @param array $params Extra build parameters
115 * @return array Encoded data
119 function _encodeHeaders($input, $params = array())
122 $params += $this->_build_params;
124 foreach ($input as $hdr_name => $hdr_value)
126 // if header contains e-mail addresses
127 if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value)) {
128 $chunks = $this->_explode_quoted_string(',', $hdr_value);
131 $chunks = array($hdr_value);
137 foreach ($chunks as $i => $value) {
138 $value = trim($value);
140 //This header contains non ASCII chars and should be encoded.
141 if (preg_match('#[\x80-\xFF]{1}#', $value)) {
143 // Don't encode e-mail address
144 if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) {
145 $value = $matches[1];
146 $suffix = ' '.$matches[2];
149 switch ($params['head_encoding']) {
151 // Base64 encoding has been selected.
153 $encoded = base64_encode($value);
156 case 'quoted-printable':
158 // quoted-printable encoding has been selected
160 $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
161 // replace spaces with _
162 $encoded = str_replace(' ', '_', $encoded);
165 $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
168 // add chunk to output string by regarding the header maxlen
169 $len = strlen($value);
170 if ($i == 0 || $line_len + $len < $maxlen) {
171 $hdr_value .= ($i>0?', ':'') . $value;
172 $line_len += $len + ($i>0?2:0);
175 $hdr_value .= ($i>0?', ':'') . "\n " . $value;
180 $input[$hdr_name] = $hdr_value;
187 function _explode_quoted_string($delimiter, $string)
190 $strlen = strlen($string);
191 for ($q=$p=$i=0; $i < $strlen; $i++) {
192 if ($string{$i} == "\"" && $string{$i-1} != "\\") {
193 $q = $q ? false : true;
195 else if (!$q && $string{$i} == $delimiter) {
196 $result[] = substr($string, $p, $i - $p);
201 $result[] = substr($string, $p);