Usage:
<? <Br/> include "email. class "<br/> $ mail-> setto (" a@a.com "); // recipient <br/> $ mail-> setcc (" B @ B .com, c@c.com "); // CC <br/> $ mail-> setcc ("d@ B .com, e@c.com"); // Private CC <br/> $ mail-> setfrom ("f@f.com "); // sender <br/> $ mail-> setsubject ("topic"); // subject <br/> $ mail-> settext ("text format "); // The sending text format can also be a variable <br/> $ mail-> sethtml ("HTML format "); // The HTML format can also be a variable <br/> $ mail-> setattachments ("C: a.jpg"); // Add an attachment, path <br/> $ mail-> send (); // send email <br />?>
Code:
<? Php <br/> class email {<br/> // --- set the global variable <br/> var $ mailto = ""; // recipient <br/> var $ mailcc = ""; // CC <br/> var $ mailbcc = ""; // secretly CC <br/> var $ mailfrom = ""; // sender <br/> var $ mailsubject = ""; // subject <br/> var $ mailtext = ""; // subject of the text format <br/> var $ mailhtml = ""; // html-format subject <br/> var $ mailattachments = ""; // attachment <br/>/* function setto ($ inaddress ): the $ inaddress <br/> parameter used to process emails contains one or more strings. the email address variable is separated by commas (,). Email address <br/> the default return value is true <br/> ************************** * *****************************/<br/> function setto ($ inaddress) {<br/> // -- use the explode () function to separate the email addresses according to "," <br/> $ addressarray = explode (",", $ inaddress ); <br/> // -- check the validity of the email address cyclically <br/> for ($ I = 0; $ I <count ($ addressarray ); $ I ++) {if ($ this-> checkemail ($ addressarray [$ I]) = false) return false ;} <br/> // -- store all valid email addresses in an array <br/> $ this-> mailto = implode ($ addh Ssarray, ","); <br/> return true ;} <br/> /*********************************** * ************** <br/> setcc ($ inaddress) Function) set the CC email address <br/> parameter $ inaddress is a string containing one or more email addresses, which is an email address variable, <br/> Use commas to separate multiple email addresses. The default return value is true. <br/> ******************** **************************************** **/<br/> function setcc ($ inaddress) {<br/> // -- use the explode () function to separate the email addresses according to "," <br/> $ addressarray = explode (",", $ inaddress ); <br/> // -- pass Check the validity of the email address cyclically <br/> for ($ I = 0; $ I <count ($ addressarray); $ I ++) {if ($ this-> checkemail ($ addressarray [$ I]) = false) return false ;} <br/> // -- store all valid email addresses in an array <br/> $ this-> mailcc = implode ($ addressarray ,","); <br/> return true ;} <br/> /*********************************** * *************** <br/> setbcc ($ inaddress) Function) set the parameter $ inaddress to include one or more <br/> email address strings and email address variables, use commas to separate multiple email addresses. The default return value is <br/> true <br/> *** ***************************************/ <Br/> function setbcc ($ inaddress) {<br/> // -- use the explode () function to separate the email addresses according to "," <br/> $ addressarray = explode (",", $ inaddress ); <br/> // -- check the validity of the email address cyclically <br/> for ($ I = 0; $ I <count ($ addressarray ); $ I ++) <br/> {if ($ this-> checkemail ($ addressarray [$ I]) = false) <br/> return false; <br/>}< br/> // -- all valid email addresses are stored in the array. <br/> $ this-> mailbcc = implode ($ addressarray ,","); <br/> return True; <br/>}< br/> /****************************** * ********************************* <br/> Function setfrom ($ inaddress): set the sender address parameter $ inaddress to the string containing the email address <br/> the default return value is true <br/> **************** * *********************/<br/> function setfrom ($ inaddress) {<br/> if ($ this-> checkemail ($ inaddress) {<br/> $ this-> mailfrom = $ inaddress; <br/> return true; <br/>} return false ;} <br/>/********************** <br/> function setsu Bject ($ insubject) is used to set the email subject parameter $ insubject as a string, <br/> true is returned by default. <br/> **************************** * **************/<br/> function setsubject ($ insubject) {<br/> If (strlen (TRIM ($ insubject)> 0) {<br/> $ this-> mailsubject = ereg_replace ("N ","", $ insubject); <br/> return true ;}< br/> return false ;} <br/> /*********************************** * **************** <br/> settext ($ intext) Function) set the email subject parameter $ intext to text content in the text format. <Br/> returns true <br/> ***************************** * **********/<br/> function settext ($ intext) {<br/> If (strlen (TRIM ($ intext)> 0) {<br/> $ this-> mailtext = $ intext; <br/> return true ;} <br/> return false; <br/>}< br/> /****************************** * *** <br/> function sethtml ($ inhtml) set the email subject parameter $ inhtml in HTML format to HTML, <br/> the default return value is true <br/> **************************** * *******/<br/> function sethtml ($ inhtml) {<br/> If (strlen (TRIM ($ inhtml)> 0) {<br/> $ this-> mailhtml = $ inhtml; <br/> return true ;} <br/> return false ;} <br/>/********************* <br/> setattachments ($ inattachments) set the email attachment parameter $ inattachments <br/> to a string containing the directory, you can also include multiple files separated by commas. The default return value is true <br/> ********************** * *******************/<br/> function setattachments ($ inattachments) {<br/> If (strlen (TRIM ($ inattachments)> 0) {<br/> $ this-> mailatta Chments = $ inattachments; <br/> return true ;}< br/> return false ;} <br/>/******************************** <br /> function checkemail ($ inaddress): We have called this function before, <br/> used to check the validity of the email address <br/> ************************ * ***************/<br/> function checkemail ($ inaddress) {<br/> return (ereg ("^ [^ @] + @ ([a-zA-Z0-9-] + .) + ([a-zA-Z0-9-] {2} | net | com | Gov | mil | org | Edu | INT) $ ", $ inaddress )); <br/>}< br/> /************** * ********************************* <Br/> Function loadtemplate ($ infilelocation, $ inhash, $ hash) read the temporary file and <br/> Replace the useless information parameter $ infilelocation is used to locate the directory of the file <br/> $ inhash because the temporary value is stored $ ignore because the email subject is placed <br/> *************************************** * ******************/<br/> function loadtemplate ($ infilelocation, $ inhash, $ hash) {<br/>/* For example, the email contains the following content: Dear ~! Username ~, <Br/> your address is ~! Useraddress ~ */<Br/> "~!" It is the starting sign "~" Is the end flag <br/> $ templatedelim = "~ "; <Br/> $ templatenamestart = "! "; <Br/> // -- find these locations and replace them <br/> $ templatelineout = ""; // -- open the temporary file <br/> if ($ templatefile = fopen ($ infilelocation, "R") {<br/> while (! Feof ($ templatefile) {<br/> $ templateline = fgets ($ templatefile, 1000); <br/> $ templatelinearray = explode ($ templatedelim, $ templateline ); <br/> for ($ I = 0; $ I <count ($ templatelinearray); $ I ++) {<br/> // -- find the starting position <br/> If (strcspn ($ templatelinearray [$ I], $ templatenamestart) = 0) {<br/> // -- replace the value <br/> $ hashname = substr ($ templatelinearray [$ I], 1 ); <br/> // -- replace the value <br/> $ templatelinearray [$ I] = ereg_replace ($ hashnam E, (string) $ inhash [$ hashname], $ hashname ); <br/>}< br/> // outputs an array of characters and overlays them. <br/> $ templatelineout. = implode ($ templatelinearray, ""); <br/>}// -- close the file fclose ($ templatefile ); <br/> // -- set the subject format (text or HTML) <br/> If (strtoupper ($ subject) = "text ") <br/> return ($ this-> settext ($ templatelineout); <br/> else if (strtoupper ($ dependency) = "html ") <br/> return ($ this-> sethtml ($ templatelineout); <br/>}return false; <B R/>}< br/> /******************************* * ********* <br/> function getrandomboundary ($ offset) returns a random boundary value. <br/> the parameter $ offset is an integer.-an MD5 () value is returned when multiple pipelines are called () encoded string <br/> ********************************* * *****/<br/> function getrandomboundary ($ offset = 0) {<br/> // -- Random Number Generation <br/> srand (Time () + $ offset ); <br/> // -- returns a string of 32 characters encoded with MD5. <br/> return ("----". (MD5 (RAND ())));} <br/> /*********************************** ******* ** <Br/> function: getcontenttype ($ infilename) used to determine the attachment type <br/> ******************************* * **************/<br/> function getcontenttype ($ infilename) {<br/> // -- remove path <br/> $ infilename = basename ($ infilename ); <br/> // -- Remove files without an extension <br/> If (strrchr ($ infilename ,". ") = false) {<br/> return" application/octet-stream "; <br/>}< br/> // -- raise the name extension and determine <br/> $ extension = strrchr ($ infilename ,". "); <br/> switch ($ Extension) {<Br/> case ". GIF ": Return" image/GIF "; <br/> case ". GZ ": Return" application/X-gzip "; <br/> case ". htm ": Return" text/html "; <br/> case ". html ": Return" text/html "; <br/> case ". jpg ": Return" image/JPEG "; <br/> case ". tar ": Return" application/X-tar "; <br/> case ". TXT ": Return" text/plain "; <br/> case ". zip ": Return" application/zip "; <br/> default: Return" application/octet-stream "; <br/>}< br/> return "Application/octet-stream "; <br/>}< br/> /****************************** * ************* <br/> the formattextheader function adds the text content to the text file header. <br/> ******* **************************************** * *****/<br/> function formattextheader () {$ outtextheader = ""; <br/> $ outtextheader. = "Content-Type: text/plain; <br/> charset = US-asciin"; <br/> $ outtextheader. = "content-transfer-encoding: 7 bitnn"; <br/> $ outtextheader. = $ This-> mailtext. "N"; <br/> return $ outtextheader; <br/> }/********************************** * ************* <br/> function formathtmlheader () add the subject content to the HTML file header <br/> **************************** * *************/<br/> function formathtmlheader () {<br/> $ outhtmlheader = ""; <br/> $ outhtmlheader. = "Content-Type: text/html; <br/> charset = US-asciin"; <br/> $ outhtmlheader. = "content-transfer-encoding: 7 bitnn"; <br/> $ o Uthtmlheader. = $ this-> mailhtml. "N"; <br/> return $ outhtmlheader; <br/>}< br/> /****************************** * *** <br/> function formatattachmentheader ($ infilelocation) mark the attachments in the email <br/> ****************************** **/<br/> function formatattachmentheader ($ infilelocation) {<br/> $ outattachmentheader = ""; <br/> // -- use the above function getcontenttype ($ infilelocation) obtain the attachment type <br/> $ contenttype = $ this-> getcontenttype ($ INFI Lelocation); <br/> // -- if the attachment is text, Standard 7-bit encoding is used. <br/> If (ereg ("text", $ contenttype )) {<br/> $ outattachmentheader. = "Content-Type :". $ contenttype. "; n"; <br/> $ outattachmentheader. = 'name = "'. basename ($ infilelocation ). '"'. "N"; <br/> $ outattachmentheader. = "content-transfer-encoding: 7 bitn"; <br/> $ outattachmentheader. = "content-Disposition: attachment; n"; <br/> $ outattachmentheader. = 'filename= "'. basenam E ($ infilelocation). '"'." Nn "; <br/> $ textfile = fopen ($ infilelocation," R "); <br/> while (! Feof ($ textfile) {<br/> $ outattachmentheader. = fgets ($ textfile, 1000); <br/>}< br/> // -- close the file fclose ($ textfile); <br/> $ outattachmentheader. = "N"; <br/>}< br/> // -- 64-bit encoding for non-text formats <br/> else {$ outattachmentheader. = "Content-Type :". $ contenttype. "; n"; <br/> $ outattachmentheader. = 'name = "'. basename ($ infilelocation ). '"'. "N"; <br/> $ outattachmentheader. = "content-transfer-encoding: base64n"; <br/> $ outat Tachmentheader. = "content-Disposition: attachment; n"; <br/> $ outattachmentheader. = 'filename= "'. basename ($ infilelocation ). '"'. "Nn"; <br/> // -- call the external command uuencode for encoding <br/> exec ("uuencode-M $ infilelocation nothing_out", $ returnarray ); <br/> for ($ I = 1; $ I <(count ($ returnarray); $ I ++) {<br/> $ outattachmentheader. = $ returnarray [$ I]. "N"; <br/>}< br/>}return $ outattachmentheader; <br/>}< br/> /********* * ******************* <Br/> the send () function is used to send emails, the returned value is true. <br/> ******************************* * ***/<br/> function send () {<br/> // -- set the email header to null <br/> $ mailheader = ""; <br/> // -- add CC users <br/> if ($ this-> mailcc! = "") <Br/> $ mailheader. = "cc :". $ this-> mailcc. "N"; <br/> // -- add a secret CC <br/> if ($ this-> mailbcc! = "") <Br/> $ mailheader. = "BCC :". $ this-> mailbcc. "N"; <br/> // -- add a sender <br/> if ($ this-> mailfrom! = "") <Br/> $ mailheader. = "from :". $ this-> mailfrom. "N"; <br/> // --------------------------- mail format ------------------------------ <br/> // -- text format <br/> if ($ this-> mailtext! = "" & $ This-> mailhtml = "" & $ this-> mailattachments = "") {<br/> return mail ($ this-> mailto, $ this-> mailsubject, $ this-> mailtext, $ mailheader ); <br/>}< br/> // -- HTML or text format <br/> else if ($ this-> mailtext! = "" & $ This-> mailhtml! = "" & $ This-> mailattachments = "") {<br/> $ bodyboundary = $ this-> getrandomboundary (); <br/> $ textheader = $ this-> formattextheader (); <br/> $ htmlheader = $ this-> formathtmlheader (); <br/> // -- set mime-version <br/> $ mailheader. = "mime-version: 1.0n"; <br/> $ mailheader. = "Content-Type: multipart/alternative; n"; <br/> $ mailheader. = 'boundary = "'. $ bodyboundary. '"'; <br/> $ mailheader. = "nnn"; <br/> // -- add email subject and edge <Br/> $ mailheader. = "--". $ bodyboundary. "N"; <br/> $ mailheader. = $ textheader; <br/> $ mailheader. = "--". $ bodyboundary. "N"; <br/> // -- add an HTML tag <br/> $ mailheader. = $ htmlheader; <br/> $ mailheader. = "n --". $ bodyboundary. "--"; <br/> // -- send an email <br/> return mail ($ this-> mailto, $ this-> mailsubject, "", $ mailheader ); <br/>}< br/> // Add HTML and attachments to the text <br/> else if ($ this-> mailtext! = "" & $ This-> mailhtml! = "" & $ This-> mailattachments! = "") {<Br/> $ attachmentboundary = $ this-> getrandomboundary (); <br/> $ mailheader. = "Content-Type: multipart/mixed; n"; <br/> $ mailheader. = 'boundary = "'. $ attachmentboundary. '"'. "Nn"; <br/> $ mailheader. = "this is a multi-part message in MIME format. N "; <br/> $ mailheader. = "--". $ attachmentboundary. "N"; <br/> $ bodyboundary = $ this-> getrandomboundary (1); <br/> $ textheader = $ this-> formattextheader (); <br /> $ Htmlheader = $ this-> formathtmlheader (); <br/> $ mailheader. = "mime-version: 1.0n"; <br/> $ mailheader. = "Content-Type: multipart/alternative; n"; <br/> $ mailheader. = 'boundary = "'. $ bodyboundary. '"'; <br/> $ mailheader. = "nnn"; <br/> $ mailheader. = "--". $ bodyboundary. "N"; <br/> $ mailheader. = $ textheader; <br/> $ mailheader. = "--". $ bodyboundary. "N"; <br/> $ mailheader. = $ htmlheader; <br/> $ mailheader. = "N --". $ bodyboundary. "--"; <br/> // -- Obtain the attachment value <br/> $ attachmentarray = explode (",", $ this-> mailattachments ); <br/> // -- loop based on the number of attachments <br/> for ($ I = 0; $ I <count ($ attachmentarray); $ I ++) {<br/> // -- separate $ mailheader. = "n --". $ attachmentboundary. "N"; <br/> // -- Attachment Information <br/> $ mailheader. = $ this-> formatattachmentheader ($ attachmentarray [$ I]); <br/>}< br/> $ mailheader. = "--". $ attachmentboundary. "--"; <br/> return mail ($ This-> mailto, $ this-> mailsubject, "", $ mailheader); <br/>}< br/> return false; <br/>}< br/>?> <Br/> detailed source reference: http://www.jb51.net/article/15107.htm