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
30 * Adds an image to the list of embedded images.
32 * @param string $file The image file name OR image data itself
33 * @param string $c_type The content type
34 * @param string $name The filename of the image.
35 * Only use if $file is the image data
36 * @param bool $isfilename Whether $file is a filename or not
38 * @param string $contentid Desired Content-ID of MIME part
39 * Defaults to generated unique ID
40 * @return mixed true on success or PEAR_Error object
43 function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true, $contentid = '')
45 $filedata = ($isfilename === true) ? $this->_file2str($file) : $file;
46 if ($isfilename === true)
47 $filename = ($name == '' ? $file : $name);
51 if (PEAR::isError($filedata))
55 $contentid = md5(uniqid(time()));
57 $this->_html_images[] = array(
69 * returns the HTML body portion of the message
70 * @return string HTML body of the message
73 function getHTMLBody()
75 return $this->_htmlbody;
80 * Encodes a header as per RFC2047
82 * @param array $input The header data to encode
83 * @param array $params Extra build parameters
84 * @return array Encoded data
88 function _encodeHeaders($input, $params = array())
91 $params += $this->_build_params;
93 foreach ($input as $hdr_name => $hdr_value)
95 // if header contains e-mail addresses
96 if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value))
97 $chunks = $this->_explode_quoted_string(',', $hdr_value);
99 $chunks = array($hdr_value);
104 foreach ($chunks as $i => $value)
106 $value = trim($value);
108 //This header contains non ASCII chars and should be encoded.
109 if (preg_match('#[\x80-\xFF]{1}#', $value))
112 // Don't encode e-mail address
113 if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches))
115 $value = $matches[1];
116 $suffix = ' '.$matches[2];
119 switch ($params['head_encoding'])
122 // Base64 encoding has been selected.
124 $encoded = base64_encode($value);
127 case 'quoted-printable':
129 // quoted-printable encoding has been selected
131 $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
132 // replace spaces with _
133 $encoded = str_replace(' ', '_', $encoded);
136 $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
139 // add chunk to output string by regarding the header maxlen
140 $len = strlen($value);
141 if ($line_len + $len < $maxlen)
143 $hdr_value .= ($i>0?', ':'') . $value;
144 $line_len += $len + ($i>0?2:0);
148 $hdr_value .= ($i>0?', ':'') . "\n " . $value;
153 $input[$hdr_name] = $hdr_value;
160 function _explode_quoted_string($delimiter, $string)
163 $strlen = strlen($string);
164 for ($q=$p=$i=0; $i < $strlen; $i++)
166 if ($string{$i} == "\"" && $string{$i-1} != "\\")
167 $q = $q ? false : true;
168 else if (!$q && $string{$i} == $delimiter)
170 $result[] = substr($string, $p, $i - $p);
175 $result[] = substr($string, $p);