PHP Write an attachment to send an instance of the program

Source: Internet
Author: User
Tags array base64 count fread mail md5 php write zip
Program

?
Error_reporting (63);
Include (' Class.html_mime_mail.inc ');

/***************************************
* * Example of usage.
***************************************/
/***************************************
* * Read the file background.gif into
* * $backgrnd.
***************************************/
$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. Don't put From:here, this
* * 'll is added when $mail->send
***************************************/
$mail = new Html_mime_mail ("x-mailer:html MIME Mail class\r\n");

/***************************************
* * If sending an HTML email, then
* * Two variables specify the text and
* * HTML versions of the mail. Don ' t
* * have to is named as these are. Just
* * Make sure the names tie into to the
* * $mail->add_html () command further down.
***************************************/
$text = ' This is a test. ';
$html = '
/***************************************
* * ADD the text, HTML and embedded images.
* * Each embedded image has to is 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 ($backgrnd, ' background.gif ', ' image/gif ');
$mail->add_html ($html, $text);

/***************************************
* * If not sending an HTML email, then
* * is used to set the plain text
* * Body of the email.
***************************************/
$mail->body = ' fsss ';

/***************************************
* * is used to add a attachment to
* * the email.
***************************************/
$mail->add_attachment ($attachment, ' example.zip ', ' application/octet-stream ');

/***************************************
* * builds the message.
***************************************/
$mail->build_message ();

/***************************************
* * sends the message. $mail->build_message ()
* * is seperate to $mail->send
* * Same email can be sent many
* * Differing recipients simply by putting
* * $mail->send () in a loop.
***************************************/
$mail->send (', ' szw@phpexe.com ', ' from Name ', ' szw@phpexe.com ', ' Subject ', ');

/***************************************
* * Debug stuff. Entirely unnecessary.
***************************************/
Echo ' <PRE> ';
Echo $mail->mime;
Echo ' </PRE> ';
?>

<?php
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.