4 +-----------------------------------------------------------------------+
5 | program/include/rcube_mail_mime.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2007-2009, 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 protected $mime_content;
34 * Set build parameters
36 function setParam($param)
38 if (is_array($param)) {
39 $this->_build_params = array_merge($this->_build_params, $param);
44 * Adds an image to the list of embedded images.
46 * @param string $file The image file name OR image data itself
47 * @param string $c_type The content type
48 * @param string $name The filename of the image.
49 * Only use if $file is the image data
50 * @param bool $isfilename Whether $file is a filename or not
52 * @param string $contentid Desired Content-ID of MIME part
53 * Defaults to generated unique ID
54 * @return mixed true on success or PEAR_Error object
57 function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true, $contentid = '')
59 $filedata = ($isfilename === true) ? $this->_file2str($file) : $file;
60 if ($isfilename === true) {
61 $filename = ($name == '' ? $file : $name);
67 if (PEAR::isError($filedata)) {
71 if ($contentid == '') {
72 $contentid = md5(uniqid(time()));
75 $this->_html_images[] = array(
87 * returns the HTML body portion of the message
88 * @return string HTML body of the message
91 function getHTMLBody()
93 return $this->_htmlbody;
98 * Creates a new mimePart object, using multipart/mixed as
99 * the initial content-type and returns it during the
102 * @return object The multipart/mixed mimePart object
105 function &_addMixedPart()
107 $params['content_type'] = $this->_headers['Content-Type'] ? $this->_headers['Content-Type'] : 'multipart/mixed';
108 $ret = new Mail_mimePart('', $params);
114 * Encodes a header as per RFC2047
116 * @param array $input The header data to encode
117 * @param array $params Extra build parameters
118 * @return array Encoded data
122 function _encodeHeaders($input, $params = array())
125 $params += $this->_build_params;
127 foreach ($input as $hdr_name => $hdr_value)
129 // if header contains e-mail addresses
130 if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value)) {
131 $chunks = rcube_explode_quoted_string(',', $hdr_value);
134 $chunks = array($hdr_value);
140 foreach ($chunks as $i => $value) {
141 $value = trim($value);
143 //This header contains non ASCII chars and should be encoded.
144 if (preg_match('/[\x80-\xFF]{1}/', $value)) {
146 // Don't encode e-mail address
147 if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) {
148 $value = $matches[1];
149 $suffix = ' '.$matches[2];
152 switch ($params['head_encoding']) {
154 // Base64 encoding has been selected.
156 $encoded = base64_encode($value);
159 case 'quoted-printable':
161 // quoted-printable encoding has been selected
163 // replace ?, =, _ and spaces
164 $encoded = str_replace(array('=','_','?',' '), array('=3D','=5F','=3F','_'), $value);
165 $encoded = preg_replace('/([\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $encoded);
168 $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
171 // add chunk to output string by regarding the header maxlen
172 $len = strlen($value);
173 if ($i == 0 || $line_len + $len < $maxlen) {
174 $hdr_value .= ($i>0?', ':'') . $value;
175 $line_len += $len + ($i>0?2:0);
178 $hdr_value .= ($i>0?', ':'') . "\n " . $value;
183 $input[$hdr_name] = wordwrap($hdr_value, 990, "\n", true); // hard limit header length
191 * Provides caching of body of constructed MIME Message to avoid
192 * duplicate construction of message and damage of MIME headers
194 * @return string The mime content
198 public function &get($build_params = null)
200 if(empty($this->mime_content))
201 $this->mime_content = parent::get($build_params);
202 return $this->mime_content;