I will repost an article "transfer a file via EMAIL attachment (via mail )"

Source: Internet
Author: User
? /** Classmime_mail * OriginalimplementationbySaschaSchumannsascha@schumann.cx * ModifiedbyTobiasRatschillertobias@dnet.it: *-Generalcodeclean-up *-separatebody-andfro
/*
* Class mime_mail
* Original implementation by Sascha Schumann <sascha@schumann.cx>
* Modified by Tobias Ratschiller <tobias@dnet.it>:
*-General code clean-up
*-Separate body-and from-property
*-Killed some mostly un-necessary stuff
*/

Class mime_mail
{
Var $ parts;
Var $;
Var $ from;
Var $ headers;
Var $ subject;
Var $ body;

/*
* Void mime_mail ()
* Class constructor
*/
Function mime_mail ()
{
$ This-> parts = array ();
$ This-> to = "";
$ This-> from = "";
$ This-> subject = "";
$ This-> body = "";
$ This-> headers = "";
}

/*
* Void add_attachment (string message, [string name], [string ctype])
* Add an attachment to the mail object
*/
Function add_attachment ($ message, $ name = "", $ ctype = "application/octet-stream ")
{
$ This-> parts [] = array (
"Ctype" => $ ctype,
"Message" => $ message,
"Encode" => $ encode,
"Name" => $ name
);
}

/*
* Void build_message (array part =
* Build message parts of an multipart mail
*/
Function build_message ($ part)
{
$ Message = $ part ["message"];
$ Message = chunk_split (base64_encode ($ message ));
$ Encoding = "base64 ";
Return "Content-Type:". $ part ["ctype"].
($ Part ["name"]? "; Name = \" ". $ part [" name "]." \ "": "").
"\ NContent-Transfer-Encoding: $ encoding \ n $ message \ n ";
}

/*
* Void build_multipart ()
* Build a multipart mail
*/
Function build_multipart ()
{
$ Boundary = "B". md5 (uniqid (time ()));
$ Multipart = "Content-Type: multipart/mixed; boundary = $ boundary \ n \ nThis is a MIME
Encoded message. \ n -- $ boundary ";

For ($ I = sizeof ($ this-> parts)-1; $ I >=0; $ I --)
{
$ Multipart. = "\ n". $ this-> build_message ($ this-> parts [$ I]). "-- $ boundary ";
}
Return $ multipart. = "-- \ n ";
}

/*
* Void send ()
* Send the mail (last class-function to be called)
*/
Function send ()
{
$ Mime = "";
If (! Empty ($ this-> from ))
$ Mime. = "From:". $ this-> from. "\ n ";
If (! Empty ($ this-> headers ))
$ Mime. = $ this-> headers. "\ n ";

If (! Empty ($ this-> body ))
$ This-> add_attachment ($ this-> body, "", "text/plain ");
$ Mime. = "MIME-Version: 1.0 \ n". $ this-> build_multipart ();
Mail ($ this-> to, $ this-> subject, "", $ mime );
}
}; // End of class

/*
* Example usage
*

$ Attachment = fread (fopen ("test.jpg", "r"), filesize ("test.jpg "));

$ Mail = new mime_mail ();
$ Mail-> from = "foo@bar.com ";
$ Mail-> headers = "Errors-To: foo@bar.com ";
$ Mail-> to = "bar@foo.com ";
$ Mail-> subject = "Testing ...";
$ Mail-> body = "This is just a test .";
$ Mail-> add_attachment ("$ attachment", "test.jpg", "image/jpeg ");
$ Mail-> send ();

*/
?>

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.