Example of sending an email with an attachment using the MAIL function
Last Update:2014-07-17
Source: Internet
Author: User
Classmime_mail {var $ parts; var $ to; var $ from; var $ headers; var $ subject; var $ body;/** voidmime_mail () * classconstructor */functionmime_mail () {$ this-partsarray (); $ this-to; $ 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