cvs: php4 /pear/Mail mime.php

From: Date: Thu, 20 Sep 2001 17:43:22 +0000
Subject: cvs: php4 /pear/Mail mime.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-861@lists.php.net to get a copy of this message
heyesr Thu Sep 20 13:43:22 2001 EDT Modified files: /php4/pear/Mail mime.php Log: All code changes. Including better encoding support, more control over build process and character set support.

Index: php4/pear/Mail/mime.php diff -u php4/pear/Mail/mime.php:1.5 php4/pear/Mail/mime.php:1.6 --- php4/pear/Mail/mime.php:1.5 Thu Sep 20 13:36:17 2001 +++ php4/pear/Mail/mime.php Thu Sep 20 13:43:20 2001 @@ -14,9 +14,10 @@ // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Tomas V.V.Cox <cox@idecnet.com> | +// | Richard Heyes <richard.heyes@heyes-computing.net | // +----------------------------------------------------------------------+ // -// $Id: mime.php,v 1.5 2001/09/20 17:36:17 heyesr Exp $ +// $Id: mime.php,v 1.6 2001/09/20 17:43:20 heyesr Exp $ require_once 'PEAR.php'; @@ -32,6 +33,7 @@ * Sascha Schumann <sascha@schumann.cx>. * * @author Tomas V.V.Cox <cox@idecnet.com> +* @author Richard Heyes <richard.heyes@heyes-computing.net> * @package Mail * @access public */ @@ -60,20 +62,25 @@ /** * list of the attached images * @var array - */ - var $_html_images = array(); + */ + var $_html_images = array(); /** * list of the attachements * @var array - */ + */ var $_parts = array(); /** + * Build parameters + * @var array + */ + var $_build_params = array(); + /** * Headers for the mail * @var array */ var $_headers = array(); - + /* * Constructor function * @@ -86,6 +93,13 @@ } $this->_boundary = '=_' . md5(uniqid(time())); + + $this->_build_params = array( + 'text_encoding' => '7bit', + 'html_encoding' => 'quoted-printable', + '7bit_wrap' => 998, + 'charset' => 'iso-8859-1' + ); } /* @@ -101,7 +115,7 @@ * @return mixed true on success or PEAR_Error object * @access public */ - function set_txtbody($data, $isfile = false) + function setTXTBody($data, $isfile = false) { if (!$isfile) { $this->_txtbody = $data; @@ -112,7 +126,7 @@ } $this->_txtbody = $cont; } - return TRUE; + return true; } /* @@ -125,7 +139,7 @@ * @return mixed true on success or PEAR_Error object * @access public */ - function set_htmlbody($data, $isfile = false) + function setHTMLBody($data, $isfile = false) { if (!$isfile) { $this->_htmlbody = $data; @@ -136,7 +150,8 @@ } $this->_htmlbody = $cont; } - return TRUE; + + return true; } /* @@ -148,6 +163,10 @@ */ function _buildHtml($orig_boundary) { + if (!isset($this->_txtbody)) { + $this->_txtbody = ''; + } + $sec_boundary = '=_' . md5(uniqid(time())); $thr_boundary = '=_' . md5(uniqid(time())); @@ -155,97 +174,103 @@ $this->_multipart .= '--' . $orig_boundary . MAIL_MIME_CRLF . 'Content-Type: multipart/alternative;' . MAIL_MIME_CRLF . chr(9) . - 'boundary="' . $sec_boundary.'"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + 'boundary="' . $sec_boundary.'"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . '--' . $sec_boundary . MAIL_MIME_CRLF . - 'Content-Type: text/plain' . MAIL_MIME_CRLF . - 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - chunk_split(base64_encode($this->_txtbody)) . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + 'Content-Type: text/plain; charset="' . $this->_build_params['charset'] . '"' . MAIL_MIME_CRLF . + $this->_getEncodedData($this->_txtbody, $this->_build_params['text_encoding']) . MAIL_MIME_CRLF . - '--' . $sec_boundary . MAIL_MIME_CRLF . - 'Content-Type: text/html' . MAIL_MIME_CRLF . - 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - chunk_split(base64_encode($this->_htmlbody)) . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + '--'.$sec_boundary . MAIL_MIME_CRLF . + 'Content-Type: text/html; charset="' . $this->_build_params['charset'] . '"' . MAIL_MIME_CRLF . + $this->_getEncodedData($this->_htmlbody, $this->_build_params['html_encoding']) . MAIL_MIME_CRLF . '--' . $sec_boundary . '--' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; + } else { - //replaces image names with content-id's. - for ($i=0; $i<count($this->_html_images); $i++) { - $this->_htmlbody = ereg_replace($this->_html_images[$i]['name'], - 'cid:' . $this->_html_images[$i]['cid'], - $this->_htmlbody); + + // Replaces image names with content-id's. + for ($i = 0; $i < count($this->_html_images); $i++) { + $this->_htmlbody = str_replace($this->_html_images[$i]['name'], + 'cid:' . $this->_html_images[$i]['cid'], + $this->_htmlbody); } $this->_multipart .= - '--' . $orig_boundary . MAIL_MIME_CRLF . + '--'.$orig_boundary . MAIL_MIME_CRLF . 'Content-Type: multipart/related;' . MAIL_MIME_CRLF . chr(9) . - 'boundary="' . $sec_boundary.'"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + 'boundary="' . $sec_boundary . '"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - '--' . $sec_boundary . MAIL_MIME_CRLF . + '--'.$sec_boundary . MAIL_MIME_CRLF . 'Content-Type: multipart/alternative;' . MAIL_MIME_CRLF . chr(9) . - 'boundary="' . $thr_boundary . '"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + 'boundary="' . $thr_boundary . '"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . '--' . $thr_boundary . MAIL_MIME_CRLF . - 'Content-Type: text/plain' . MAIL_MIME_CRLF . - 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - chunk_split(base64_encode($this->_txtbody)) . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + 'Content-Type: text/plain; charset="' . $this->_build_params['charset'] . '"' . MAIL_MIME_CRLF . + $this->_getEncodedData($this->_txtbody, $this->_build_params['text_encoding']) . MAIL_MIME_CRLF . - '--' . $thr_boundary . MAIL_MIME_CRLF . - 'Content-Type: text/html' . MAIL_MIME_CRLF . - 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - chunk_split(base64_encode($this->_htmlbody)) . MAIL_MIME_CRLF . MAIL_MIME_CRLF . - '--' . $thr_boundary . '--' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; + '--'.$thr_boundary . MAIL_MIME_CRLF . + 'Content-Type: text/html; charset="' . $this->_build_params['charset'] . '"' . MAIL_MIME_CRLF . + $this->_getEncodedData($this->_htmlbody, $this->_build_params['html_encoding']) . MAIL_MIME_CRLF . + '--' . $thr_boundary . '--' . MAIL_MIME_CRLF; - for ($i=0; $i<count($this->_html_images); $i++) { + // Add the embedded images + for ($i = 0; $i < count($this->_html_images); $i++) { $this->_multipart .= '--' . $sec_boundary . MAIL_MIME_CRLF; - $this->_buildHtml_image($i); + $this->_buildHtmlImage($this->_html_images[$i]); } - $this->_multipart .= "--" . $sec_boundary . '--' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; + $this->_multipart .= '--' . $sec_boundary . '--' . MAIL_MIME_CRLF; } } /* * Adds an image to the list of embedded images. * - * @param string $file_name The image file name + * @param string $file The image file name OR image data itself * @param string $c_type The content type + * @param string $name The filename of the image. Only use if $file is the image data + * @param bool $isfilename Whether $file is a filename or not. Defaults to true * @return mixed true on success or PEAR_Error object * @access public */ - function addHTMLImage ($file_name, $c_type='application/octet-stream') + function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true) { - $file = $this->_file2str($file_name); - if (PEAR::isError($file)) { - return $file; + $filedata = ($isfilename === true) ? $this->_file2str($file) : $file; + $filename = ($isfilename === true) ? basename($file) : basename($name); + if (PEAR::isError($filedata)) { + return $filedata; } $this->_html_images[] = array( - 'body' => $file, - 'name' => basename($file_name), + 'body' => $filedata, + 'name' => $filename, 'c_type' => $c_type, 'cid' => md5(uniqid(time())) - ); - return TRUE; + ); + return true; } /* * Adds a file to the list of attachments. * - * @param string $file_name The file name of the file to attach + * @param string $file The file name of the file to attach OR the file data itself * @param string $c_type The content type + * @param string $name The filename of the attachment. Only use if $file is the file data + * @param bool $isFilename Whether $file is a filename or not. Defaults to true * @return mixed true on success or PEAR_Error object * @access public */ - function addAttachment ($file_name, $c_type='application/octet-stream') + function addAttachment($file, $c_type='application/octet-stream', $name = '', $isfilename = true) { - $file = $this->_file2str($file_name); - if (PEAR::isError($file)) { - return $file; + $filedata = ($isfilename === true) ? $this->_file2str($file) : $file; + $filename = ($isfilename === true) ? basename($file) : basename($name); + if (PEAR::isError($filedata)) { + return $filedata; } + $this->_parts[] = array( - 'body' => $file, - 'name' => basename($file_name), + 'body' => $filedata, + 'name' => $filename, 'c_type' => $c_type ); - return TRUE; + return true; } /* @@ -254,13 +279,13 @@ * @return string * @acces private */ - function & _file2str ($file_name) + function & _file2str($file_name) { if (!is_readable($file_name)) { - return $this->raiseError("File is not readable $file_name"); + return $this->raiseError('File is not readable ' . $file_name); } - if (!$fd = fopen($file_name, 'r')) { - return $this->raiseError("Could not open $file_name"); + if (!$fd = fopen($file_name, 'rb')) { + return $this->raiseError('Could not open ' . $file_name); } $cont = fread($fd, filesize($file_name)); fclose($fd); @@ -273,16 +298,15 @@ * @param integer $i number of the image to build * @access private */ - function _buildHtmlImage ($i) + function _buildHtmlImage($image) { - $this->_multipart .= 'Content-Type: '.$this->_html_images[$i]['c_type']; + $this->_multipart .= 'Content-Type: ' . $image['c_type']; - $fname = basename($this->_html_images[$i]['name']); + $fname = basename($image['name']); $this->_multipart .= '; name="' . $fname . '"' . MAIL_MIME_CRLF; - $this->_multipart .= 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF; - $this->_multipart .= 'Content-ID: <' . $this->_html_images[$i]['cid'] . '>' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; - $this->_multipart .= chunk_split(base64_encode($this->_html_images[$i]['body'])) . MAIL_MIME_CRLF; + $this->_multipart .= 'Content-ID: <' . $image['cid'] . '>' . MAIL_MIME_CRLF; + $this->_multipart .= $this->_getEncodedData($image['body'], 'base64') . MAIL_MIME_CRLF; } /* @@ -297,55 +321,172 @@ $message_part = ''; $message_part.= 'Content-Type: ' . $part['c_type']; if ($part['name'] != '') { - $message_part .= '; name="' . $part['name'].'"' . MAIL_MIME_CRLF; + $message_part .= '; name="' . $part['name'] . '"' . MAIL_MIME_CRLF; } else { $message_part .= MAIL_MIME_CRLF; } // Determine content encoding. if ($part['c_type'] == 'text/plain') { - $message_part .= 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; - $message_part .= chunk_split(base64_encode($part['body'])) . MAIL_MIME_CRLF; + $message_part .= 'Content-Disposition: attachment; filename="' . $part['name'] . '"' . MAIL_MIME_CRLF; + $message_part .= $this->_getEncodedData($part['body'], $this->_build_params['text_encoding']) . MAIL_MIME_CRLF; + } elseif ($part['c_type'] == 'message/rfc822') { - $message_part .= 'Content-Transfer-Encoding: 7bit' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; - $message_part .= $part['body'] . MAIL_MIME_CRLF; + $message_part .= $this->_getEncodedData($part['body'], '7bit') . MAIL_MIME_CRLF; + } else { - $message_part .= 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF; - $message_part .= 'Content-Disposition: attachment; filename="' . $part['name'] . '"' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; - $message_part .= chunk_split(base64_encode($part['body'])) . MAIL_MIME_CRLF; + $message_part .= 'Content-Disposition: attachment; filename="' . $part['name'] . '"' . MAIL_MIME_CRLF; + $message_part .= $this->_getEncodedData($part['body'], 'base64') . MAIL_MIME_CRLF; } return $message_part; } /* + * Encodes data quoted-printable style + * + * @param string Data to encode + * @return string Encoded data + * @access private + */ + function _quotedPrintableEncode($input , $line_max = 76) + { + $lines = preg_split("/(\r\n|\r|\n)/", $input); + $eol = MAIL_MIME_CRLF; + $escape = '='; + $output = ''; + + while (list(, $line) = each($lines)) { + + $linlen = strlen($line); + $newline = ''; + + for ($i = 0; $i < $linlen; $i++) { + $char = substr($line, $i, 1); + $dec = ord($char); + + if (($dec == 32) AND ($i == ($linlen - 1))) { + $char = '=20'; + + } elseif($dec == 9) { + // Do nothing if a tab. + + } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) { + $char = $escape . strtoupper(sprintf('%02s', dechex($dec))); + } + + if ((strlen($newline) + strlen($char)) >= $line_max) { + $output .= $newline . $escape . $eol; + $newline = ''; + } + $newline .= $char; + } + + $output .= $newline . $eol; + } + + return substr($output, 0, -1*strlen($eol)); // Don't want the final CRLF + } + + /* + * Returns data passed to it based on the given encoding type. + * + * @param string Data to be encoded + * @param string Encoding type use, currently one of + * 7bit, quoted-printable, base64 + * @return string Encoded data + * @access private + */ + function _getEncodedData($data, $encoding) + { + $return = ''; + + switch($encoding){ + + case '7bit': + $return .= 'Content-Transfer-Encoding: 7bit' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + substr(chunk_split($data, $this->_build_params['7bit_wrap'], MAIL_MIME_CRLF), 0, -1 * strlen(MAIL_MIME_CRLF)); + break; + + case 'quoted-printable': + $return .= 'Content-Transfer-Encoding: quoted-printable' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + $this->_quotedPrintableEncode($data); + break; + + case 'base64': + $return .= 'Content-Transfer-Encoding: base64' . MAIL_MIME_CRLF . MAIL_MIME_CRLF . + substr(chunk_split(base64_encode($data), 76, MAIL_MIME_CRLF), 0, -1 * strlen(MAIL_MIME_CRLF)); + // The substr removes the last CRLF that chunk_split adds + break; + } + + return $return; + } + + /* * Builds the multipart message from the list ($this->_parts) and * returns the mime content. * + * @param array Build parameters that change the way the email + * is built. Should be associative. Can contain: + * text_encoding - What encoding to use for plain text + * Default is 7bit + * html_encoding - What encoding to use for html + * Default is quoted-printable + * 7bit_wrap - Number of characters before text is + * wrapped in 7bit encoding + * Default is 998 + * charset - The character set to use. + * Default is iso-8859-1 * @return string The mime content * @access public */ - function & get() + function & get($build_params = NULL) { + if (isset($build_params)) { + while (list($key, $value) = each($build_params)) { + $this->_build_params[$key] = $value; + } + } + + $do_text = isset($this->_txtbody) ? true : false; + $do_html = isset($this->_htmlbody) ? true : false; + $do_parts = count($this->_parts) > 0 ? true : false; + $boundary = $this->_boundary; - $this->_multipart = 'This is a MIME encoded message.' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; - // For HTML bodies and HTML Images - if (isset($this->_htmlbody)) { - $this->_buildHtml($boundary); - // For TXT bodies - } elseif (isset($this->_txtbody)) { - $part = array('body' => $this->_txtbody, - 'name' => '', - 'c_type' => 'text/plain'); - $this->_multipart .= '--' . $boundary . MAIL_MIME_CRLF . $this->_buildPart($part); - } - // For attachments - for ($i=(count($this->_parts)-1); $i>=0; $i--) { - $this->_multipart .= '--'.$boundary . MAIL_MIME_CRLF. - $this->_buildPart($this->_parts[$i]); + + // Need to make a multipart email? + if ($do_html OR $do_parts) { + $this->_multipart = 'This is a MIME encoded message.' . MAIL_MIME_CRLF . MAIL_MIME_CRLF; + + // For HTML bodies and HTML Images + if ($do_html) { + $this->_buildHtml($boundary); + + // For TXT bodies + } elseif ($do_text) { + $part = array( + 'body' => $this->_txtbody, + 'name' => '', + 'c_type' => 'text/plain' + ); + + $this->_multipart .= '--' . $boundary . MAIL_MIME_CRLF . $this->_buildPart($part); + } + + // Plain text email + } elseif ($do_text) { + $this->_multipart = $this->_txtbody; + } + + // Build any attachments + if ($do_parts) { + for ($i = 0; $i < count($this->_parts); $i++) { + $this->_multipart .= '--' . $boundary . MAIL_MIME_CRLF . $this->_buildPart($this->_parts[$i]); + } } - $this->_mime = $this->_multipart . '--' . $boundary . '--' . MAIL_MIME_CRLF; + $this->_mime = ($do_html OR $do_parts) ? $this->_multipart . '--' . $boundary . '--' . MAIL_MIME_CRLF : $this->_multipart; return $this->_mime; } @@ -353,16 +494,32 @@ * Returns an array with the headers needed to append to the email * (MIME-Version and Content-Type) * - * @return array Assoc array with the standar mime headers + * @param array Assoc array with any extra headers. Optional. + * @return array Assoc array with the standard mime headers * @access public */ - function & headers() + function & headers($xtra_headers = NULL) { - $headers = array(); - $headers['MIME-Version'] = '1.0'; - $headers['Content-Type'] = 'multipart/mixed;' . MAIL_MIME_CRLF . chr(9) . - 'boundary="'.$this->_boundary.'"'; - return $headers; + $do_text = isset($this->_txtbody) ? true : false; + $do_html = isset($this->_htmlbody) ? true : false; + $do_parts = count($this->_parts) > 0 ? true : false; + + // Add the mime headers + if ($do_html OR $do_parts) { + $this->_headers['MIME-Version'] = '1.0'; + $this->_headers['Content-Type'] = 'multipart/mixed;' . MAIL_MIME_CRLF . chr(9) . + 'boundary="' . $this->_boundary . '"'; + + // Just set the content-type to text/plain + } elseif ($do_text) { + $this->_headers['Content-Type'] = 'text/plain'; + } + + if (isset($xtra_headers)) { + $this->_headers = array_merge($this->_headers, $xtra_headers); + } + + return $this->_headers; } } -?> \ No newline at end of file +?>
« previous php.pear.cvs (#861) next »