I borrowed the MIME class from the essence. wrote a basic letter to the small Dongdong (2)

Source: Internet
Author: User
Tags array base64 exit copy count fread return window
mime| Essence SENDMAIL.PHP4

?
Load the class that sent the attachment
Require (' html_mime_mail.inc ');

if ($update!= "") {//If with attachment

Upload attachment
if (!file_exists ("c:/ftptmp/". $myfile _name)) {
if (Copy ($myfile, "c:/ftptmp/". $myfile _name)) {
Reading attachments
$attachment = fread ($fp = fopen ("c:/ftptmp/". $myfile _name, ' R '), FileSize ($myfile));
Fclose ($FP);
Unlink ("c:/ftptmp/". $myfile _name);
}
else {
echo "Upload file failed!" ";
Exit ();
}
}
else{
echo File Duplicate name! ";
Exit ();
}


Creates an instance of a class and joins the attachment
$mail = new Html_mime_mail ();
$mail->add_attachment ($attachment, $myfile _name, ' Application/octet-stream ');

/*---------------------This section demonstrates how to send an HTML letter---------------------------------------
$filename = ' background.gif ';
$backgrnd = fread ($fp = fopen ($filename, ' R '), FileSize ($filename));
Fclose ($FP);

$text = ' This is a test. ';
$html = '
$mail->add_html_image ($backgrnd, ' background.gif ', ' image/gif ');
$mail->add_html ($html, $text);
-------------------------------------------------------------------------------------*/

Read the body, encapsulate and send the letters
$mail->body= $bodytext;
$mail->build_message ();

$backvalue = $mail->send (', $receivemailbox, ', $sendmailbox, $subject, ');

Send a hint message
echo "<script Language=javascript>alert (' Letter ok, press OK to return!") '); window.location= ' default.htm ' </script> ';
}
else{//If no attachments
$backvalue =mail ($receivemailbox, $subject, $bodytext, "from:". $sendmailbox. "\nreply-to:". $sendmailbox. "\nx-mailer:php/". Phpversion ());

if ($result) {
echo "<script Language=javascript>alert (' Letter ok, press OK to return!") '); window.location= ' default.htm ' </script> ';
}
else{
echo "<script language=javascript>backvalue=window.confirm (' Letter failed! Maybe the server is too busy! Do you want to send it again? '); if (Backvalue) {location.reload ()}else{window.location= ' default.htm '}</script> ';
}
}

?>

Html_mime_mail.inc This class is available in the elite area. I'll borrow it for a while.

?
Class html_mime_mail{

var $headers;
var $body;
var $multipart;
var $mime;
var $html;
var $html _text;
var $html _images = Array ();
var $cids = array ();
var $do _html;
var $parts = array ();

/***************************************
* * Constructor function. Sets the Headers
* * if supplied.
***************************************/
function Html_mime_mail ($headers = ' ") {
$this->headers = $headers;
}

/***************************************
* * Adds a HTML part to the mail.
* * Also replaces image names with
* * content-id ' s.
***************************************/
function add_html ($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);
}
}
}

/***************************************
* * Builds HTML part of email.
***************************************/
function build_html ($orig _boundary) {
$sec _boundary = ' =_ '. MD5 (Uniqid (Time ()));
$thr _boundary = ' =_ '. MD5 (Uniqid (Time ()));

if (!is_array ($this->html_images)) {
$this->multipart.= '--'. $orig _boundary. " \ r \ n ";
$this->multipart.= ' content-type:multipart/alternative; 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:7bit '. \r\n\r\n ";
$this->multipart.= $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:7bit '. \r\n\r\n ";
$this->multipart.= $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; Boundary= "'. $sec _boundary." \ "\r\n\r\n\r\n";

$this->multipart.= '--'. $sec _boundary. " \ r \ n ";
$this->multipart.= ' content-type:multipart/alternative; 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:7bit '. \r\n\r\n ";
$this->multipart.= $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:7bit '. \r\n\r\n ";
$this->multipart.= $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->build_html_image ($i);
}

$this->multipart.= "--" $sec _boundary. " --\r\n\r\n ";
}
}
/***************************************
* * 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);
}

/***************************************
* * Builds an embedded image part of
* * 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 ']." \ "\ r \ n";
else $this->multipart. = "\ r \ n";

$this->multipart.= ' Content-id: < '. $this->html_images[$i [' CID ']. " >\r\n ";
$this->multipart.= ' content-transfer-encoding:base64 '. \r\n\r\n ";
$this->multipart.= chunk_split (Base64_encode ($this->html_images[$i] [' body ']). " \ r \ n ";
}

/***************************************
* * Builds a single part of a multipart
* * message.
***************************************/
function Build_part ($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";

Determine content encoding.
if ($this->parts[$i] [' c_type '] = = ' Text/plain ') {
$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;
}

/***************************************
* * Builds the multipart message from the
* * List ($this->parts).
***************************************/
function Build_message () {
$boundary = ' =_ '. MD5 (Uniqid (Time ()));

$this->headers.= "mime-version:1.0\r\n";
$this->headers.= "content-type:multipart/mixed; Boundary=\ "". $boundary. " \ "\ r \ n";
$this->multipart = ';
$this->multipart.= "This is a MIME encoded message.\r\ncreated by Html_mime_mail.class.\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->build_html ($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->build_part ($i);
}

$this->mime = $this->multipart. " --". $boundary." --\r\n ";
}

/***************************************
* * Sends the mail.
***************************************/
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;
$this->headers.= ' from: '. $from. " \ r \ n ";
$this->headers.= $headers;
Mail ($to, $subject, $this->mime, $this->headers);
}

/***************************************
* * Use the use of 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 Headers,
* * String The body)
***************************************/
function Smtp_send ($smtp _obj, $from _addr, $to _addr) {
Global $ $smtp _obj;
$SMTP _obj = $ $smtp _obj;

if (substr ($this->headers,-2) = = "\ r \ n") $this->headers = substr ($this->headers,0,-2);
$this->headers = Explode ("\ r \ n", $this->headers);

$smtp _obj->sendmessage ($from _addr, $to _addr, $this->headers, $this->mime);
}

}//End of class.
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.