4 +-----------------------------------------------------------------------+
5 | program/lib/rc_mime.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2007, 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 $
22 // require Mail_mime class 1.4.0
23 require_once('Mail/mime.php');
26 class rc_mail_mime extends Mail_mime
29 * Set build parameters
31 function setParam($param)
34 $this->_build_params = array_merge($this->_build_params, $param);
38 * Adds an image to the list of embedded images.
40 * @param string $file The image file name OR image data itself
41 * @param string $c_type The content type
42 * @param string $name The filename of the image.
43 * Only use if $file is the image data
44 * @param bool $isfilename Whether $file is a filename or not
46 * @param string $contentid Desired Content-ID of MIME part
47 * Defaults to generated unique ID
48 * @return mixed true on success or PEAR_Error object
51 function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true, $contentid = '')
53 $filedata = ($isfilename === true) ? $this->_file2str($file) : $file;
54 if ($isfilename === true)
55 $filename = ($name == '' ? $file : $name);
59 if (PEAR::isError($filedata))
63 $contentid = md5(uniqid(time()));
65 $this->_html_images[] = array(
77 * returns the HTML body portion of the message
78 * @return string HTML body of the message
81 function getHTMLBody()
83 return $this->_htmlbody;
88 * Creates a new mimePart object, using multipart/mixed as
89 * the initial content-type and returns it during the
92 * @return object The multipart/mixed mimePart object
95 function &_addMixedPart()
97 $params['content_type'] = $this->_headers['Content-Type'] ? $this->_headers['Content-Type'] : 'multipart/mixed';
98 $ret = new Mail_mimePart('', $params);
104 * Encodes a header as per RFC2047
106 * @param array $input The header data to encode
107 * @param array $params Extra build parameters
108 * @return array Encoded data
112 function _encodeHeaders($input, $params = array())
115 $params += $this->_build_params;
117 foreach ($input as $hdr_name => $hdr_value)
119 // if header contains e-mail addresses
120 if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value))
121 $chunks = $this->_explode_quoted_string(',', $hdr_value);
123 $chunks = array($hdr_value);
128 foreach ($chunks as $i => $value)
130 $value = trim($value);
132 //This header contains non ASCII chars and should be encoded.
133 if (preg_match('#[\x80-\xFF]{1}#', $value))
136 // Don't encode e-mail address
137 if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches))
139 $value = $matches[1];
140 $suffix = ' '.$matches[2];
143 switch ($params['head_encoding'])
146 // Base64 encoding has been selected.
148 $encoded = base64_encode($value);
151 case 'quoted-printable':
153 // quoted-printable encoding has been selected
155 $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
156 // replace spaces with _
157 $encoded = str_replace(' ', '_', $encoded);
160 $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
163 // add chunk to output string by regarding the header maxlen
164 $len = strlen($value);
165 if ($i == 0 || $line_len + $len < $maxlen)
167 $hdr_value .= ($i>0?', ':'') . $value;
168 $line_len += $len + ($i>0?2:0);
172 $hdr_value .= ($i>0?', ':'') . "\n " . $value;
177 $input[$hdr_name] = $hdr_value;
184 function _explode_quoted_string($delimiter, $string)
187 $strlen = strlen($string);
188 for ($q=$p=$i=0; $i < $strlen; $i++)
190 if ($string{$i} == "\"" && $string{$i-1} != "\\")
191 $q = $q ? false : true;
192 else if (!$q && $string{$i} == $delimiter)
194 $result[] = substr($string, $p, $i - $p);
199 $result[] = substr($string, $p);