RE: [PHP-PEAR] PEAR on Win32

From: Date: Mon, 28 Aug 2000 14:27:42 +0000
Subject: RE: [PHP-PEAR] PEAR on Win32
References: 1  Groups: php.pear 
Request: Send a blank email to php-pear+get-223@lists.php.net to get a copy of this message
Perhaps to complicate things, perhaps not to, I've rehashed the class I'd like to contribute (Variable and function names, commenting style etc). It's attached along with an example script that you can test it with if you so wish. I've labelled it mail_message_mime.php due to the recent discussion, but I've a feeling it may be more appropriate to be labelled mail_message_html.php. Comments most welcome! Take care. -- Richard Heyes

<?php /*************************************** ** Title.........: HTML Mime Mail class ** Version.......: 1.30 ** Author........: Richard Heyes <richard.heyes@heyes-computing.net> ** Filename......: pear.example.php ** Last changed..: 25/08/2000 ** Notes.........: Based upon mime_mail.class ** by Tobias Ratschiller <tobias@dnet.it> ** and Sascha Schumann <sascha@schumann.cx>. ***************************************/ error_reporting(63); include('mail_message_mime.php'); /*************************************** ** Example of usage. ***************************************/ /*************************************** ** Read the file background.gif into ** $backgrnd. ***************************************/ /*************************************** ** ** !! Important !! ** ** If running this script upon Windows ** then you may need to change the fopen() ** mode from 'r' to 'rb'. ** Thanks to Thomas Unger for this nugget. ***************************************/ $filename = 'background.gif'; $backgrnd = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp); /*************************************** ** Read the file test.zip into $attachment. ***************************************/ $filename = 'example.zip'; $attachment = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp); /*************************************** ** Create the mail object. Optional headers ** argument. Do not put From: here, this ** will be added when $mail->send ** Does not have to have trailing \r\n ** but if adding multiple headers, must ** be seperated by \r\n. ***************************************/ $mail = new mail_message_mime('X-Mailer: Html Mime Mail Class'); /*************************************** ** If sending an html email, then these ** two variables specify the text and ** html versions of the mail. Don't ** have to be named as these are. Just ** make sure the names tie in to the ** $mail->add_html() command further down. ***************************************/ $text = 'Success.'; $html = '<HTML><BODY BACKGROUND="background.gif">'."\r\n".'<FONT FACE="Verdana, Arial" COLOR="#FF0000">'."\r\n".'&nbsp;&nbsp;&nbsp;&nbsp;Success...</FONT>'."\r\n".'<P></BODY></HTML>'; /*************************************** ** Add the text, html and embedded images. ** Each embedded image has to be added ** using $mail->add_html_image() BEFORE ** calling $mail->add_html(). The name ** of the image should match exactly ** (case-sensitive) to the name in the html. ***************************************/ $mail->addHtmlImage($backgrnd, 'background.gif', 'image/gif'); $mail->addHtml($html, $text); /*************************************** ** If not sending an html email, then ** this is used to set the plain text ** body of the email. ***************************************/ //$mail->setBody('ftfuygfyugyguilgulghlgjhlgjhlkgjguilguilguil ghjli'); /*************************************** ** This is used to add an attachment to ** the email. ***************************************/ $mail->addAttachment($attachment, 'example.zip', 'application/octet-stream'); /*************************************** ** Builds the message. ***************************************/ $mail->buildMessage(); /*************************************** ** Sends the message. $mail->build_message() ** is seperate to $mail->send so that the ** same email can be sent many times to ** differing recipients simply by putting ** $mail->send() in a loop. ***************************************/ $mail->send('TO NAME', 'TO ADDRESS', 'FROM NAME', 'FROM ADDRESS', 'SUBJECT LINE'); /*************************************** ** Example of smtp_send() ** This requires Manuel Lemos' smtp mail ** sending class, available at: ** http://phpclasses.upperdesign.com ***************************************/ /* include('class.smtp.inc'); $smtp = new smtp_class; $smtp->host_name = 'mail.yourdomain.com'; // Address/host of mailserver $smtp->localhost = 'yourdomain.com'; // Address/host of this machine / HELO: $from = 'you@yourdomain.com'; $to = array('you@yourdomain.com'); // Can be more than one address in this array. $headers = array('To: "Your Name" <you@yourdomain.com>'); // A To: header is necessary, but does // not have to match the list in $to. $mail->smtpSend('smtp', $from, $to, 'Subject Line', $headers); */ /*************************************** ** Example of using get_rfc822() to return ** the entirety of the mail and then ** attaching it to another mail. Fun eh? ***************************************/ /* $rfc822_email = $mail->getRfc822('TO NAME', 'TO ADDRESS', 'FROM NAME', 'FROM ADDRESS', 'SUBJECT LINE'); $mail2 = new mail_message_mime('X-Mailer: Big red postbox'); $mail2->setBody("\r\n".'This is an email, that has another email attached.'); $mail2->addAttachment($rfc822_email, 'Forwarded Email', 'message/rfc822'); $mail2->buildMessage(); $mail2->send('TO NAME', 'TO ADDRESS', 'FROM NAME', 'FROM ADDRESS', 'SUBJECT LINE'); */ /*************************************** ** Debug stuff. Entirely unnecessary. ***************************************/ echo '<PRE>'.$mail->getRfc822('TO NAME', 'TO ADDRESS', 'FROM NAME', 'FROM ADDRESS', 'SUBJECT LINE').'</PRE>'; ?><?php // // HTML Mime Email class // // // +----------------------------------------------------------------------+ // | Author: Richard Heyes <richard.heyes@heyes-computing.net | // | Based on a class by Tobias Ratschiller <tobias@dnet.it> | // | and Sascha Schumann <sascha@schumann.cx>. | // | Version: 1.31 | // | Updated: 25/08/2000 | // | | // | See http://www.heyes-computing.net/scripts/ for full tar/zip | // | including example file. | // +----------------------------------------------------------------------+ class mail_message_mime{ // {{{ Properties (All private) var $_mime; var $_html; var $_body; var $_do_html; var $_multipart; var $_html_text; var $_html_images = array(); var $_headers = array(); var $_parts = array(); // }}} // {{{ Constructor /** * @param $headers A string (\r\n seperated) or an array of headers * to add. */ function mail_message_mime($headers = ''){ if($headers == '') return TRUE; if(is_string($headers)) $headers = explode("\r\n", trim($headers)); for($i=0; $i<count($headers); $i++){ if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $this->_headers[] = $headers[$i][$j]; if($headers[$i] != '') $this->_headers[] = $headers[$i]; } } // }}} // {{{ setBody() /** * Sets the body text in the event of the email being plain text * instead of HTML based. * * @param $text The string of text to set as the email body. * * @return bool TRUE on success, FALSE otherwise. */ function setBody($text = ''){ if(is_string($text)){ $this->_body = $text; return TRUE; } return FALSE; } // }}} // {{{ getMime() /** * Accessor function to return the mime class variable. Purely * for debug, and to fall inline with OOP "rules". * * @return string The body of the mime email (if constructed). */ function getMime(){ if(!isset($this->_mime)) $this->_mime = ''; return $this->_mime; } // }}} // {{{ addHeader() /** * Function to set a header. Shouldn't really be necessary as you * could use the constructor and send functions, it's here nonetheless. * Takes any number of arguments, which can be either strings or arrays * containing strings. This function is php4 only and will return false * otherwise. Will return true upon finishing. * * @param mixed String or array containing headers. If string, multiple * headers should be seperated by \r\n. * @return bool False if not php4, true upon finishing, ie success. */ function addHeader(){ if((int)phpversion() < 4) return FALSE; $args = func_get_args(); for($i=0; $i<count($args); $i++){ if(is_array($args[$i])) for($j=0; $j<count($args[$i]); $j++) if($args[$i][$j] != '') $this->_headers[] = $args[$i][$j]; if($args[$i] != '') $this->_headers[] = $args[$i]; } return TRUE; } // }}} // {{{ addHtml() /** * Adds a html part to the email. Also will replace image names with * appropriate content-ids. * * @param string $html HTML version of the email. * @param string $text Plain text version of the email. */ function addHtml($html, $text){ $this->_do_html = 1; $this->_html = $html; $this->_html_text = $text; if(is_array($this->_html_images) AND count($this->_html_images) > 0){ for($i=0; $i<count($this->_html_images); $i++) $this->_html = ereg_replace($this->_html_images[$i]['name'], 'cid:'.$this->_html_images[$i]['cid'], $this->_html); } } // }}} // {{{ buildHtml() /** * Builds html part of email. Private function * * @param string $orig_boundary Original (top level) boundary. */ function buildHtml($orig_boundary){ $sec_boundary = '=_'.md5(uniqid(time())); $thr_boundary = '=_'.md5(uniqid(time())); if(count($this->_html_images) == 0){ $this->_multipart.= '--'.$orig_boundary."\r\n"; $this->_multipart.= 'Content-Type: multipart/alternative;'.chr(13).chr(10).chr(9).'boundary="'.$sec_boundary."\"\r\n\r\n\r\n"; $this->_multipart.= '--'.$sec_boundary."\r\n"; $this->_multipart.= 'Content-Type: text/plain'."\r\n"; $this->_multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n"; $this->_multipart.= chunk_split(base64_encode($this->_html_text))."\r\n\r\n"; $this->_multipart.= '--'.$sec_boundary."\r\n"; $this->_multipart.= 'Content-Type: text/html'."\r\n"; $this->_multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n"; $this->_multipart.= chunk_split(base64_encode($this->_html))."\r\n\r\n"; $this->_multipart.= '--'.$sec_boundary."--\r\n\r\n"; }else{ $this->_multipart.= '--'.$orig_boundary."\r\n"; $this->_multipart.= 'Content-Type: multipart/related;'.chr(13).chr(10).chr(9).'boundary="'.$sec_boundary."\"\r\n\r\n\r\n"; $this->_multipart.= '--'.$sec_boundary."\r\n"; $this->_multipart.= 'Content-Type: multipart/alternative;'.chr(13).chr(10).chr(9).'boundary="'.$thr_boundary."\"\r\n\r\n\r\n"; $this->_multipart.= '--'.$thr_boundary."\r\n"; $this->_multipart.= 'Content-Type: text/plain'."\r\n"; $this->_multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n"; $this->_multipart.= chunk_split(base64_encode($this->_html_text))."\r\n\r\n"; $this->_multipart.= '--'.$thr_boundary."\r\n"; $this->_multipart.= 'Content-Type: text/html'."\r\n"; $this->_multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n"; $this->_multipart.= chunk_split(base64_encode($this->_html))."\r\n\r\n"; $this->_multipart.= '--'.$thr_boundary."--\r\n\r\n"; for($i=0; $i<count($this->_html_images); $i++){ $this->_multipart.= '--'.$sec_boundary."\r\n"; $this->buildHtmlImage($i); } $this->_multipart.= "--".$sec_boundary."--\r\n\r\n"; } } // }}} // {{{ addHtmlImage() /** * Adds an image to the list of embedded images. * * @param string $file The actual image itself, (ie. Read in using fread()). * @param string $name The name of the image. Also known as the filename. * @param string $c_type The content-type of the image. */ function addHtmlImage($file, $name = '', $c_type = 'application/octet-stream'){ $this->_html_images[] = array( 'body' => $file, 'name' => $name, 'c_type' => $c_type, 'cid' => md5(uniqid(time())) ); } // }}} // {{{ addAttachment() /** * Adds a file to the list of attachments. * * @param string $file The actual file itself, (ie. Read in using fread()). * @param string $name The name of the file. Also known as the filename. * @param string $c_type The content-type of the file. */ function addAttachment($file, $name = '', $c_type='application/octet-stream'){ $this->_parts[] = array( 'body' => $file, 'name' => $name, 'c_type' => $c_type ); } // }}} // {{{ buildHtmlImage() /** * Builds an embedded image part of an html mail. Private function. * * @param int $i Current position in the _html_images array. */ function buildHtmlImage($i){ $this->_multipart.= 'Content-Type: '.$this->_html_images[$i]['c_type']; if($this->_html_images[$i]['name'] != '') $this->_multipart .= '; name="'.$this->_html_images[$i]['name']."\"\r\n"; else $this->_multipart .= "\r\n"; $this->_multipart.= 'Content-Transfer-Encoding: base64'."\r\n"; $this->_multipart.= 'Content-ID: <'.$this->_html_images[$i]['cid'].">\r\n\r\n"; $this->_multipart.= chunk_split(base64_encode($this->_html_images[$i]['body']))."\r\n"; } // }}} // {{{ buildPart() /** * Builds a single part of a multipart message. Private function. * * @param int $i Current position in the _parts array. * @return string The constructed part of the mime email. */ function buildPart($i){ $message_part = ''; $message_part.= 'Content-Type: '.$this->_parts[$i]['c_type']; if($this->_parts[$i]['name'] != '') $message_part .= '; name="'.$this->_parts[$i]['name']."\"\r\n"; else $message_part .= "\r\n"; if($this->_parts[$i]['c_type'] == 'text/plain'){ $message_part.= 'Content-Transfer-Encoding: base64'."\r\n\r\n"; $message_part.= chunk_split(base64_encode($this->_parts[$i]['body']))."\r\n"; }elseif($this->_parts[$i]['c_type'] == 'message/rfc822'){ $message_part.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n"; $message_part.= $this->_parts[$i]['body']."\r\n"; }else{ $message_part.= 'Content-Transfer-Encoding: base64'."\r\n"; $message_part.= 'Content-Disposition: attachment; filename="'.$this->_parts[$i]['name']."\"\r\n\r\n"; $message_part.= chunk_split(base64_encode($this->_parts[$i]['body']))."\r\n"; } return $message_part; } // }}} // {{{ buildMessage() /** * Builds the multipart message from the list ($this->_parts). * */ function buildMessage(){ $boundary = '=_'.md5(uniqid(time())); $this->_headers[] = 'MIME-Version: 1.0'; $this->_headers[] = 'Content-Type: multipart/mixed;'.chr(13).chr(10).chr(9).'boundary="'.$boundary.'"'; $this->_multipart = "This is a MIME encoded message.\r\nCreated by mail_message_mime.php.\r\nSee http://www.heyes-computing.net/scripts/ for a copy.\r\n\r\n"; if(isset($this->_do_html) AND $this->_do_html == 1) $this->buildHtml($boundary); if(isset($this->_body) AND $this->_body != '') $this->_parts[] = array('body' => $this->_body, 'name' => '', 'c_type' => 'text/plain'); for($i=(count($this->_parts)-1); $i>=0; $i--){ $this->_multipart.= '--'.$boundary."\r\n".$this->buildPart($i); } $this->_mime = $this->_multipart."--".$boundary."--\r\n"; } // }}} // {{{ send() /** * Sends the mail using the php mail() function. * * @param string $to_name Name of recipient * @param string $to_addr Email address of recipient. * @param string $from_name Name of sender * @param string $from_addr Email address of sender * @param string $subject Subject of email. * @param mixed $headers Headers to add to this email */ function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){ if($to_name != '') $to = '"'.$to_name.'" <'.$to_addr.'>'; else $to = $to_addr; if($from_name != '') $from = '"'.$from_name.'" <'.$from_addr.'>'; else $from = $from_addr; if(is_string($headers)) $headers = explode("\r\n", trim($headers)); for($i=0; $i<count($headers); $i++){ if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $xtra_headers[] = $headers[$i][$j]; if($headers[$i] != '') $xtra_headers[] = $headers[$i]; } if(!isset($xtra_headers)) $xtra_headers = array(); mail($to, $subject, $this->_mime, 'From: '.$from."\r\n".implode("\r\n", $this->_headers)."\r\n".implode("\r\n", $xtra_headers)); } // }}} // {{{ smtpSend() /** * Use this method to deliver using direct smtp connection. * Relies upon Manuel Lemos' smtp mail delivery class * available at: http://phpclasses.upperdesign.com * * @param string $smtp_obj *Name* smtp object. * @param string $from_addr Email address of sender * @param array $to_addr Array of email addresses to send to. * @param string $subject Subject of the email. * @param array $xtra_headers Extra headers to add to this email. */ function smtpSend($smtp_obj, $from_addr, $to_addr, $subject, $xtra_headers = ''){ global $$smtp_obj; $smtp_obj = $$smtp_obj; $headers = $this->_headers; $headers[] = 'From: '.$from_addr; $headers[] = 'Subject: '.$subject; if(is_array($xtra_headers)) for(reset($xtra_headers); list(,$header) = each($xtra_headers); ) $headers[] = $header; // The following: sendmessage(string from address, array to addresses, array headers, string body) $smtp_obj->sendmessage($from_addr, $to_addr, $headers, $this->_mime); } // }}} // {{{ getRfc822() /** * Use this method to return the email in message/rfc822 format. * Useful for adding an email to another email as an attachment. * There's a commented out example in example.php. * * @param string $to_name Name of recipient * @param string $to_addr Email address of recipient. * @param string $from_name Name of sender * @param string $from_addr Email address of sender * @param string $subject Subject of email. * @param mixed $headers Headers to add to this email */ function getRfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){ // Make up the date header as according to RFC822 $date = 'Date: '.date('D, d M y H:i:s'); if($to_name != '') $to = 'To: "'.$to_name.'" <'.$to_addr.'>'; else $to = $to_addr; if($from_name != '') $from = 'From: "'.$from_name.'" <'.$from_addr.'>'; else $from = $from_addr; if(is_string($subject)) $subject = 'Subject: '.$subject; if(is_string($headers)) $headers = explode("\r\n", trim($headers)); for($i=0; $i<count($headers); $i++){ if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $xtra_headers[] = $headers[$i][$j]; if($headers[$i] != '') $xtra_headers[] = $headers[$i]; } if(!isset($xtra_headers)) $xtra_headers = array(); return $date."\r\n".$from."\r\n".$to."\r\n".$subject."\r\n".implode("\r\n", $this->_headers)."\r\n".implode("\r\n", $xtra_headers)."\r\n\r\n".$this->_mime; } // }}} } // End of class. ?>
« previous php.pear (#223) next »