PHP Tutorial. application instance 10

Source: Internet
Author: User
PHP Tutorial. application instance 10php program for sending attachments (2) <br/> & lt ;? Php <br/> classhtml_mime_mail {<br/> php program for sending attachments (2)
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
** 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 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 ";

$ This-> multipart. = '--'. $ sec_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: text/plain '. "\ r \ n ";
$ This-> multipart. = 'content-Transfer-Encoding: 7bit '. "\ r \ n ";
$ This-> multipart. = $ this-> html_text. "\ r \ n ";

$ This-> multipart. = '--'. $ sec_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: text/html'. "\ r \ n ";
$ This-> multipart. = 'content-Transfer-Encoding: 7bit '. "\ r \ n ";
$ This-> multipart. = $ this-> html. "\ r \ n ";
$ This-> multipart. = '--'. $ sec_boundary. "-- \ r \ n ";
} Else {
$ This-> multipart. = '--'. $ orig_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: multipart/related; boundary = "'. $ sec_boundary. "\" \ r \ n ";

$ This-> multipart. = '--'. $ sec_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: multipart/alternative; boundary = "'. $ thr_boundary. "\" \ r \ n ";

$ This-> multipart. = '--'. $ thr_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: text/plain '. "\ r \ n ";
$ This-> multipart. = 'content-Transfer-Encoding: 7bit '. "\ r \ n ";
$ This-> multipart. = $ this-> html_text. "\ r \ n ";

$ This-> multipart. = '--'. $ thr_boundary. "\ r \ n ";
$ This-> multipart. = 'content-Type: text/html'. "\ r \ n ";
$ This-> multipart. = 'content-Transfer-Encoding: 7bit '. "\ r \ n ";
$ This-> multipart. = $ this-> html. "\ r \ n ";
$ This-> multipart. = '--'. $ thr_boundary. "-- \ r \ n ";

For ($ I = 0; $ I Html_images); $ I ++ ){
$ This-> multipart. = '--'. $ sec_boundary. "\ r \ n ";
$ This-> build_html_image ($ I );
}

$ This-> multipart. = "--". $ sec_boundary. "-- \ 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 ";
$ 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 ";
$ 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 ";
$ Message_part. = chunk_split (base64_encode ($ this-> parts [$ I] ['body']). "\ r \ n ";
}

Return $ message_part;
}

/***************************************
** Builds the multipart message from
** 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 ";

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 this method to deliver using direct
** Smtp connection. Relies upon Manuel Lemos'
** Smtp mail delivery class available:
** 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.