Example of sending an email with an attachment using the MAIL function

Source: Internet
Author: User
Classmime_mail {var $ parts; var $ to; var $ from; var $ headers; var $ subject; var $ body; ** voidmime_mail () * classconstructor * functionmime_mail () {$ this 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: $ encodingnn $ messagen ";

}



/** Void build_multipart () * Build a multipart mail */

Function build_multipart ()

{

$ Boundary = "B". md5 (uniqid (time ()));

$ Multipart = "Content-Type: multipart/mixed; boundary = $ boundarynnThis is a MIME

Encoded message. nn -- $ 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.0n". $ 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 ();

*/

? & G

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.