+ -
当前位置:首页 → 问答吧 → 发送HTML格式的邮件

发送HTML格式的邮件

时间:2008-09-02

来源:互联网




<?php

/***************************************

** Title.........: HTML Mime Mail class

** Version.......: 1.38

** Author........: Richard Heyes <[email protected]>

** Filename......: class.html.mime.mail.class

** Last changed..: 28 August 2001

** License.......: Free to use. If you find it useful

** though, feel free to buy me something

** from my wishlist

** http://www.amazon.co.uk/exec/obidos/wishlist/S8H2UOGMPZK6

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



class html_mime_mail{



var $mime;

var $html;

var $body;

var $do_html;

var $multipart;

var $html_text;

var $html_images;

var $image_types;

var $build_params;

var $headers;

var $parts;

var $charset;



/***************************************

** C****tructor function. Sets the headers

** if supplied.

***************************************/



function html_mime_mail($headers = ''){



/***************************************

** Make sure this is defined. This should

** be rn, but due to many people having

** trouble with that, it is by default n

** If you leave it as is, you will be breaking

** quite a few standards.

***************************************/

if(!defined('CRLF'))

define('CRLF', \"n\", TRUE);



/***************************************

** Initialise some variables.

***************************************/

$this->html_images = array();

$this->headers = array();

$this->parts = array();



/***************************************

** If you want the auto load functionality

** to find other image.file types, and the

** extension and content type here.

***************************************/

$this->image_types = array(

'gif' => 'image/gif',

'jpg' => 'image/jpeg',

'jpeg' => 'image/jpeg',

'jpe' => 'image/jpeg',

'bmp' => 'image/bmp',

'png' => 'image/png',

'tif' => 'image/tiff',

'tiff' => 'image/tiff',

'swf' => 'application/x-shockwave-flash'

);

$this->charset = 'iso-8859-1';



/***************************************

** Set these up

***************************************/

$this->build_params['html_encoding'] = 'quoted-printable';

$this->build_params['text_encoding'] = '7bit';

$this->build_params['text_wrap'] = 998;



/***************************************

** Make sure the MIME version header is first.

***************************************/

$this->headers[] = 'MIME-Version: 1.0';



if($headers == '')

return TRUE;



if(is_string($headers))

$headers = explode(CRLF, 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];

}

}



/***************************************

** Accessor function to set the body text.

** Body text is used if it's not an html

** mail being sent.

***************************************/



function set_body($text = ''){

if(is_string($text)){

$this->body = $text;

return TRUE;

}

return FALSE;

}



/***************************************

** Accessor function to return the mime

** class variable. Purely for debug.

***************************************/



function get_mime(){

if(!isset($this->mime))

$this->mime = '';

return $this->mime;

}



/***************************************

** Function to set a header. Shouldn't

** really be necessary as you could use

** the c****tructor and send functi****,

** it's here nonetheless. Takes any number

** of arguments, which can be either

** strings or arrays full of strings.

** this function is php4 only and will

** return false otherwise. Will return

** true upon finishing.

***************************************/



function add_header(){

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;

}



/***************************************

** Accessor function to set the content charset.

***************************************/



function set_charset($charset = 'iso-8859-1'){



if(is_string($charset)){

$this->charset = $charset;

return TRUE;

}

return FALSE;

}



/***************************************

** This function will read a file in

** from a supplied filename and return

** it. This can then be given as the first

** argument of the the functi****

** add_html_image() or add_attachment().

***************************************/



function get_file($filename){



if($fp = fopen($filename, 'rb')){

$return = fread($fp, filesize($filename));

fclose($fp);

return $return;



}else

return FALSE;

}



/***************************************

** Function for extracting images from

** html source. This function will look

** through the html code supplied by add_html()

** and find any file that ends in one of the

** extensi**** defined in $obj->image_types.

** If the file exists it will read it in and

** embed it, (not an attachment).

**

** Function contributed by Dan Allen

***************************************/



function find_html_images($images_dir) {



// Build the list of image extensi****

while(list($key,) = each($this->image_types))

$extensi****[] = $key;



preg_match_all('/\"([^\"]+.('.implode('|', $extensi****).'))\"/Ui', $this->html, $images);



for($i=0; $i<count($images[1]); $i++){

if(file_exists($images_dir.$images[1][$i])){

$html_images[] = $images[1][$i];

$this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);

}

}



if(!empty($html_images)){



// If duplicate images are embedded, they may show up as attachments, so remove them.

$html_images = array_unique($html_images);

sort($html_images);



for($i=0; $i<count($html_images); $i++){

if($image = $this->get_file($images_dir.$html_images[$i])){

$content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];

$this->add_html_image($image, basename($html_images[$i]), $content_type);

}

}

}

}



/***************************************

** Adds a html part to the mail.

** Also replaces image names with

** content-id's.

***************************************/



function add_html($html, $text, $images_dir = NULL){



$this->do_html = 1;

$this->html = $html;

$this->html_text = ($text == '') ? 'No text version was provided' : $text;



if(isset($images_dir))

$this->find_html_images($images_dir);



if(is_array($this->html_images) AND count($this->html_images) > 0){

for($i=0; $i<count($this->html_images); $i++)

$this->html = str_replace($this->html_images[$i]['name'], 'cid:'.$this->html_images[$i]['cid'], $this->html);

}

}



/***************************************

** Adds an image to the list of embedded

** images.

***************************************/



function add_html_image($file, $name = '', $c_type='application/octet-stream'){

$this->html_images[] = array(

'body' => $file,

'name' => $name,

'c_type' => $c_type,

'cid' => md5(uniqid(time()))

);

}





/***************************************

** Adds a file to the list of attachments.

***************************************/



function add_attachment($file, $name = '', $c_type='application/octet-stream'){

$this->parts[] = array(

'body' => $file,

'name' => $name,

'c_type' => $c_type

);

}



/***************************************

** Encodes text to quoted printable standard.

**

** Function contributed by Allan Hansen

***************************************/



function quoted_printable_encode($input , $line_max = 76){



$lines = preg_split(\"/(?:rn|r|n)/\", $input);

$eol = 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))) // convert space at eol only

$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){ // CRLF is not counted

$output .= $newline.$escape.$eol; // soft line break; \" =rn\" is okay

$newline = '';

}

$newline .= $char;

} // end of for

$output .= $newline.$eol;

}

return $output;

}



/***************************************

** Function to return encoded text/html

** based upon the build params. Don't

** like this function name

***************************************/



function get_encoded_data($data, $encoding){



$return = '';



switch($encoding){



case '7bit':

$return .= 'Content-Transfer-Encoding: 7bit'.CRLF.CRLF.

chunk_split($data, $this->build_params['text_wrap']);

break;



case 'quoted-printable':

$return .= 'Content-Transfer-Encoding: quoted-printable'.CRLF.CRLF.

$this->quoted_printable_encode($data);

break;



case 'base64':

$return .= 'Content-Transfer-Encoding: base64'.CRLF.CRLF.

chunk_split(base64_encode($data));

break;

}



return $return;

}



/***************************************

** Builds html part of email.

***************************************/



function build_html($orig_boundary){

$sec_boundary = '=_'.md5(uniqid(time()));

$thr_boundary = '=_'.md5(uniqid(time()));



if(count($this->html_images) == 0){

$this->multipart .= '--'.$orig_boundary.CRLF.

'Content-Type: multipart/alternative;'.CRLF.chr(9).'boundary=\"'.$sec_boundary.'\"'.CRLF.CRLF.

'--'.$sec_boundary.CRLF.

'Content-Type: text/plain; charset=\"'.$this->charset.'\"'.CRLF.

$this->get_encoded_data($this->html_text, $this->build_params['text_encoding']).CRLF.

'--'.$sec_boundary.CRLF.

'Content-Type: text/html; charset=\"'.$this->charset.'\"'.CRLF.

$this->get_encoded_data($this->html, $this->build_params['html_encoding']).CRLF.

'--'.$sec_boundary.'--'.CRLF.CRLF;



}else{



$this->multipart .= '--'.$orig_boundary.CRLF.

'Content-Type: multipart/related;'.CRLF.chr(9).'boundary=\"'.$sec_boundary.'\"'.CRLF.CRLF.

'--'.$sec_boundary.CRLF.

'Content-Type: multipart/alternative;'.CRLF.chr(9).'boundary=\"'.$thr_boundary.'\"'.CRLF.CRLF.

'--'.$thr_boundary.CRLF.

'Content-Type: text/plain; charset=\"'.$this->charset.'\"'.CRLF.

$this->get_encoded_data($this->html_text, $this->build_params['text_encoding']).CRLF.

'--'.$thr_boundary.CRLF.

'Content-Type: text/html; charset=\"'.$this->charset.'\"'.CRLF.

$this->get_encoded_data($this->html, $this->build_params['html_encoding']).CRLF.

'--'.$thr_boundary.'--'.CRLF;



for($i=0; $i<count($this->html_images); $i++){

$this->multipart .= '--'.$sec_boundary.CRLF;

$this->build_html_image($i);

}



$this->multipart .= '--'.$sec_boundary.'--'.CRLF;

}

}



/***************************************

** Builds an embedded image part of an

** html mail.

***************************************/



function build_html_image($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'].'\"'.CRLF;

else

$this->multipart .= CRLF;



$this->multipart .= 'Content-ID: <'.$this->html_images[$i]['cid'].'>'.CRLF;

$this->multipart .= $this->get_encoded_data($this->html_images[$i]['body'], 'base64').CRLF;

}



/***************************************

** Builds a single part of a multipart

** message.

***************************************/



function build_part($input){

$message_part = '';

$message_part .= 'Content-Type: '.$input['c_type'];

if($input['name'] != '')

$message_part .= ';'.CRLF.chr(9).'name=\"'.$input['name'].'\"'.CRLF;

else

$message_part .= CRLF;



// Determine content encoding.

if($input['c_type'] == 'text/plain'){

$message_part.= $this->get_encoded_data($input['body'], 'quoted-printable').CRLF;



}elseif($input['c_type'] == 'message/rfc822'){

$message_part .= 'Content-Disposition: attachment'.CRLF;

$message_part .= $this->get_encoded_data($input['body'], '7bit').CRLF;



}else{

$message_part .= 'Content-Disposition: attachment; filename=\"'.$input['name'].'\"'.CRLF;

$message_part .= $this->get_encoded_data($input['body'], 'base64').CRLF;

}



return $message_part;

}



/***************************************

** Builds the multipart message from the

** list ($this->_parts). $params is an

** array of parameters that shape the building

** of the message. Currently supported are:

**

** $params['html_encoding'] - The type of encoding to use on html. Valid opti**** are

** \"7bit\", \"quoted-printable\" or \"base64\" (all without quotes).

** 7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable

** $params['text_encoding'] - The type of encoding to use on plain text Valid opti**** are

** \"7bit\", \"quoted-printable\" or \"base64\" (all without quotes).

** Default is 7bit

** $params['text_wrap'] - The character count at which to wrap 7bit encoded data. By

** default this is 998.

***************************************/



function build_message($params = array()){



if(count($params) > 0)

while(list($key, $value) = each($params))

$this->build_params[$key] = $value;



$boundary = '=_'.md5(uniqid(time()));



// Determine what needs building

$do_html = (isset($this->do_html) AND $this->do_html == 1) ? 1 : 0;

$do_text = (isset($this->body)) ? 1 : 0;

$do_parts = (count($this->parts) > 0) ? 1 : 0;



// Need to make this a multipart email?

if($do_html OR $do_parts){

$this->headers[] = 'Content-Type: multipart/mixed;'.CRLF.chr(9).'boundary=\"'.$boundary.'\"';

$this->multipart = \"This is a MIME encoded message.\".CRLF.CRLF;



// Build html parts

if($do_html)

$this->build_html($boundary);



// Build plain text part

elseif($do_text)

$this->multipart .= '--'.$boundary.CRLF.$this->build_part(array('body' => $this->body, 'name' => '', 'c_type' => 'text/plain'));



// No attachments or html, plain text

}elseif($do_text AND !$do_parts){

$this->headers[] = 'Content-Type: text/plain;'.CRLF.chr(9).'charset=\"'.$this->charset.'\"';

$this->multipart = $this->body.CRLF.CRLF;

}



// Build all attachments

if($do_parts)

for($i=0; $i<count($this->parts); $i++)

$this->multipart.= '--'.$boundary.CRLF.$this->build_part($this->parts[$i]);



// Add closing boundary

$this->mime = ($do_parts OR $do_html) ? $this->multipart.'--'.$boundary.'--'.CRLF : $this->multipart;

}



/***************************************

** Sends the mail.

***************************************/



function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){



$to = ($to_name != '') ? '\"'.$to_name.'\" <'.$to_addr.'>' : $to_addr;

$from = ($from_name != '') ? '\"'.$from_name.'\" <'.$from_addr.'>' : $from_addr;



if(is_string($headers))

$headers = explode(CRLF, 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 mail($to, $subject, $this->mime, 'From: '.$from.CRLF.implode(CRLF, $this->headers).CRLF.implode(CRLF, $xtra_headers));

}



/***************************************

** Use this method to deliver using direct

** smtp connection. Relies upon Manuel Lemos'

** smtp mail delivery class available at:

**http://phpclasses.upperdesign.com

**

** void smtp_send( string *Name* of smtp object,

** string From address,

** array To addresses,

** array Extra headers)

***************************************/



function smtp_send(&$smtp_obj, $from_addr, $to_addr, $xtra_headers = ''){



$headers = $this->headers;

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);

}



/***************************************

** 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.

**

** string get_rfc822(string To name,

** string To email,

** string From name,

** string From email,

** [string Subject,

** string Extra headers])

***************************************/



function get_rfc822($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');



$to = ($to_name != '') ? 'To: \"'.$to_name.'\" <'.$to_addr.'>' : 'To: '.$to_addr;

$from = ($from_name != '') ? 'From: \"'.$from_name.'\" <'.$from_addr.'>' : 'From: '.$from_addr;



if(is_string($subject))

$subject = 'Subject: '.$subject;



if(is_string($headers))

$headers = explode(CRLF, 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.CRLF.$from.CRLF.$to.CRLF.$subject.CRLF.implode(CRLF, $this->headers).CRLF.implode(CRLF, $xtra_headers).CRLF.CRLF.$this->mime;

}





} // End of class.

?>


作者: souku   发布时间: 2008-09-02

example.1.php




<?php

/***************************************

** Filename.......: example.1.php

** Project........: HTML Mime Mail class

** Last Modified..: 27 August 2001

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



/*



Having trouble? Read this article on HTML email: http://www.arsdigita.com/asj/mime/



*/



error_reporting(63);

include('class.html.mime.mail.inc');



/***************************************

** Example of usage. This example shows

** how to use the class with html,

** embedded images, and an attachment,

** using the usual methods.

***************************************/



/***************************************

** This is optional, it will default to n

** If you're having problems, try changing

** this to either n (unix) or r (Mac)

***************************************/



define('CRLF', \"rn\", TRUE);



/***************************************

** 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 rn

** but if adding multiple headers, must

** be seperated by whatever you're using

** as line ending (usually either rn or n)

***************************************/



$mail = new html_mime_mail('X-Mailer: Html Mime Mail Class');



/***************************************

** Read the image background.gif into

** $background

***************************************/



$background = $mail->get_file('background.gif');



/***************************************

** Read the file test.zip into $attachment.

***************************************/



$attachment = $mail->get_file('example.zip');



/***************************************

** If sending an html email, then these

** two variables specify the text and

** html versi**** of the mail. Don't

** have to be named as these are. Just

** make sure the names tie in to the

** $mail->add_html() call further down.

***************************************/



$text = $mail->get_file('example.txt');

$html = $mail->get_file('example.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->add_html_image($background, 'background.gif', 'image/gif');

$mail->add_html($html, $text);



/***************************************

** This is used to add an attachment to

** the email.

***************************************/



$mail->add_attachment($attachment, 'example.zip', 'application/zip');



/***************************************

** Builds the message.

***************************************/



$mail->build_message();



/***************************************

** Send the email using smtp method.

** This is the preferred method of sending.

***************************************/



include('class.smtp.inc');

$smtp = new smtp_class();



$smtp->host_name = '10.1.1.2'; // Address/host of mailserver

$smtp->localhost = 'localhost'; // Address/host of this machine (HELO)



$from = '[email protected]';

$to = array('richard@[10.1.1.2]'); // Can be more than one address in this array.



$headers = array( 'From: \"Joe\" <[email protected]>', // A To: header is necessary, but does

'Subject: Example email using HTML Mime Mail class', // not have to match the list in $to.

'To: \"Richard\" <richard@[10.1.1.2]>');



$mail->smtp_send($smtp, $from, $to, $headers);



/***************************************

** Debug stuff. Entirely unnecessary.

***************************************/



echo '<PRE>'.htmlentities($mail->get_rfc822('Richard', 'richard@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class')).'</PRE>';

?>

作者: souku   发布时间: 2008-09-02

example.2.php




<?php

/***************************************

** Filename.......: example.2.php

** Project........: HTML Mime Mail class

** Last Modified..: 27 August 2001

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



/*



Having trouble? Read this article on HTML email: http://www.arsdigita.com/asj/mime/



*/



error_reporting(63);

include('class.html.mime.mail.inc');



/***************************************

** Example of usage. This example shows

** how to use the class with html,

** embedded images, no attachments, but

** using the third argument of add_html(),

** which will try to automatically find the

** images (though not limited to images),

** and embed them. It will send the mail

** using the send() function, which will

** use the built in php mail() function.

***************************************/



/***************************************

** This is optional, it will default to n

** If you're having problems, try changing

** this to either n (unix) or r (Mac)

***************************************/



define('CRLF', \"rn\", TRUE);



/***************************************

** 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 rn

** but if adding multiple headers, must

** be seperated by whatever you're using

** as line ending (usually either rn or n)

***************************************/



$mail = new html_mime_mail('X-Mailer: Html Mime Mail Class');



/***************************************

** Read the image background.gif into

** $background

***************************************/



$background = $mail->get_file('background.gif');



/***************************************

** If sending an html email, then these

** two variables specify the text and

** html versi**** of the mail. Don't

** have to be named as these are. Just

** make sure the names tie in to the

** $mail->add_html() call further down.

***************************************/



$text = $mail->get_file('example.txt');

$html = $mail->get_file('example.html');



/***************************************

** Add the text, html and embedded images.

** Here we're using the third argument of

** add_html(), which is the path to the

** directory that holds the images. By

** adding this third argument, the class

** will try to find all the images in the

** html, and auto load them in. not 100%

** accurate, and you MUST enclose your

** image references in quotes, so src=\"img.jpg\"

** and NOT src=img.jpg. Also, where possible,

** duplicates will be avoided.

***************************************/



$mail->add_html($html, $text, './');



/***************************************

** Set character set. Not necessary, unless

** of course you need to specify one.

***************************************/



$mail->set_charset('iso-8859-1');



/***************************************

** Builds the message.

***************************************/



$mail->build_message();



/***************************************

** 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('Richard', 'richard.heyes@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class');



/***************************************

** Debug stuff. Entirely unnecessary.

***************************************/



echo '<PRE>'.htmlentities($mail->get_rfc822('Richard', 'richard.heyes@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class')).'</PRE>';

?>

作者: souku   发布时间: 2008-09-02

example.3.php




<?php

/***************************************

** Filename.......: example.3.php

** Project........: HTML Mime Mail class

** Last Modified..: 27 August 2001

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



/*



Having trouble? Read this article on HTML email: http://www.arsdigita.com/asj/mime/



*/



error_reporting(63);

include('class.html.mime.mail.inc');



/***************************************

** Example of usage. This example shows

** how to use the class to send a plain

** text email with an attachment. No html,

** or embedded images. Uses the send()

** method to send the email.

***************************************/



/***************************************

** This is optional, it will default to n

** If you're having problems, try changing

** this to either n (unix) or r (Mac)

***************************************/



define('CRLF', \"rn\", TRUE);



/***************************************

** 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 rn

** but if adding multiple headers, must

** be seperated by whatever you're using

** as line ending (usually either rn or n)

***************************************/



$mail = new html_mime_mail('X-Mailer: Html Mime Mail Class');



/***************************************

** Read the file test.zip into $attachment.

***************************************/



$attachment = $mail->get_file('example.zip');



/***************************************

** Since we're sending a plain text email,

** we only need to read in the text file.

***************************************/



$text = $mail->get_file('example.txt');



/***************************************

** To set the text body of the email, we

** are using the set_body() function. This

** is an alternative to the add_html() function

** which would obviously be inappropriate here.

***************************************/



$mail->set_body($text);



/***************************************

** This is used to add an attachment to

** the email.

***************************************/



$mail->add_attachment($attachment, 'example.zip', 'application/zip');



/***************************************

** Builds the message.

***************************************/



$mail->build_message();



/***************************************

** 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('Richard', 'richard.heyes@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class');



/***************************************

** Debug stuff. Entirely unnecessary.

***************************************/



echo '<PRE>'.htmlentities($mail->get_rfc822('Richard', 'richard.heyes@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class')).'</PRE>';

?>

作者: souku   发布时间: 2008-09-02

example.4.php




<?php

/***************************************

** Filename.......: example.4.php

** Project........: HTML Mime Mail class

** Last Modified..: 08 September 2001

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



/*



Having trouble? Read this article on HTML email: http://www.arsdigita.com/asj/mime/



*/



error_reporting(63);

include('class.html.mime.mail.inc');



/***************************************

** Example of usage. This example shows

** how to use the class to send Bcc:

** and/or Cc: recipients.

***************************************/



/***************************************

** This is optional, it will default to n

** If you're having problems, try changing

** this to either n (unix) or r (Mac)

***************************************/



define('CRLF', \"rn\", TRUE);



/***************************************

** 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 rn

** but if adding multiple headers, must

** be seperated by whatever you're using

** as line ending (usually either rn or n)

***************************************/



$mail = new html_mime_mail('X-Mailer: Html Mime Mail Class');



/***************************************

** We will just send a text email

***************************************/



$text = $mail->get_file('example.txt');

$mail->set_body($text);



/***************************************

** Builds the message.

***************************************/



$mail->build_message();



/***************************************

** Send the email using smtp method.

** This is the preferred method of sending.

***************************************/



include('class.smtp.inc');

$smtp = new smtp_class();



$smtp->host_name = '10.1.1.2'; // Address/host of mailserver

$smtp->localhost = 'localhost'; // Address/host of this machine (HELO)



$from = '[email protected]';

$to = array(

'richard@[10.1.1.2]',

'cc-recipient@[10.1.1.2]', // This recipient has a corresponding Cc: header, so it will

// effectively be a Cc: recipient.

'bcc-recipient@[10.1.1.2]' // Since this recipient has no corresponding to: or Cc: header,

// it will effectively be a Bcc: recipient.

);



$headers = array( 'From: \"Joe\" <[email protected]>', // A To: header is necessary, but does

'Subject: Example email using HTML Mime Mail class', // not have to match the list in $to.

'To: \"Richard\" <richard@[10.1.1.2]>',

'Cc: cc-recipient@[10.1.1.2]');



$mail->smtp_send($smtp, $from, $to, $headers);



/***************************************

** Debug stuff. Entirely unnecessary.

***************************************/



echo '<PRE>'.htmlentities($mail->get_rfc822('Richard', 'richard@[10.1.1.2]', 'Joe', '[email protected]', 'Example email using HTML Mime Mail class')).'</PRE>';

?>

作者: souku   发布时间: 2008-09-02

example.5.php




<?php

/***************************************

** Filename.......: example.5.php

** Project........: HTML Mime Mail class

** Last Modified..: 15 September 2001

** PHP中文社区收集整理:www.phpNet.cn

***************************************/



/*



Having trouble? Read this article on HTML email: http://www.arsdigita.com/asj/mime/



*/



error_reporting(63);

include('class.html.mime.mail.inc');



/***************************************

** Example of usage. This example shows

** how to use the class to send an email

** attached to another email. First email

** built is plain text with an attachment.

** This is then attached to the second email

** which is also plain text.

***************************************/



/***************************************

** This is optional, it will default to n

** If you're having problems, try changing

** this to either n (unix) or r (Mac)

***************************************/



define('CRLF', \"rn\", TRUE);



/***************************************

** 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 rn

** but if adding multiple headers, must

** be seperated by whatever you're using

** as line ending (usually either rn or n)

***************************************/



$mail_1 = new html_mime_mail('X-Mailer: Html Mime Mail Class');



/***************************************

** First email.

***************************************/



$text = $mail_1->get_file('example.txt');

$mail_1->set_body($text);



/***************************************

** Add the attachment

***************************************/



$file = $mail_1->get_file('example.zip');

$mail_1->add_attachment($file, 'example.zip', 'application/zip');



/***************************************

** Builds the message.

***************************************/



$mail_1->build_message();



/***************************************

** Don't send this email, but use the

** get_rfc822() method to assign it to a

** variable.

***************************************/



$mail = $mail_1->get_rfc822('John Doe', '[email protected]', 'Some one', '[email protected]', 'Test for attached email');



/***************************************

** Now start a new mail, and add the first

** (which is now built and contained in

** $mail) to it.

***************************************/



$mail_2 = new html_mime_mail('X-Mailer: Html Mime Mail Class');



$mail_2->set_body('This email has an attached email');

$mail_2->add_attachment($mail, 'Test for attached email', 'message/rfc822');

$mail_2->build_message();



$mail_2->send('Richard Heyes', 'richard@[10.1.1.2]', 'A Nother', '[email protected]', 'This email has another email attached');



/***************************************

** Debug stuff. Entirely unnecessary.

***************************************/



echo '<PRE>'.htmlentities($mail_2->get_rfc822('Richard Heyes', 'richard@[10.1.1.2]', 'A Nother', '[email protected]', 'This email has another email attached')).'</PRE>';

?>

作者: souku   发布时间: 2008-09-02

example.html




<HTML>

<HEAD>

<STYLE TYPE=\"text/css\">

<!--

H1 {font-family: Verdana; font-size: 18pt}

BODY {font-family: Verdana; font-size: 10pt; margin-left: 30px}

// -->

</STYLE>

</HEAD>

<BODY BGCOLOR=\"beige\" BACKGROUND=\"background.gif\">



<H1>Success!</H1>



You've successfully sent an HTML email.



</BODY>

</HTML>



example.txt


    Success!

    [*]This is the plain text portion of the email.

作者: souku   发布时间: 2008-09-02