Introduction: This is a full-featured page for sending emails in PHP. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324427 'rolling = 'no'>
This type of function is very powerful, not only can send HTML format emails, but also can send attachments
Note: When the author published the article, it was changed 《
《? PHP
Class email {
// --- Set global variables
VaR $ mailto = ""; // recipient
VaR $ mailcc = ""; // CC
VaR $ mailbcc = ""; // Private CC
VaR $ mailfrom = ""; // sender
VaR $ mailsubject = ""; // topic
VaR $ mailtext = ""; // subject in text format
VaR $ mailhtml = ""; // subject in HTML Format
VaR $ mailattachments = ""; // attachment
/* Function setto ($ inaddress): Address parameter used to process emails $ inaddress
Contains one or more strings. the email address variable uses commas to separate multiple email addresses.
The default return value is true.
**************************************** ******************/
Function setto ($ inaddress ){
// -- Use the explode () function to separate the email addresses according ","
$ Addressarray = explode (",", $ inaddress );
// -- Check the validity of the email address cyclically
For ($ I = 0; $ I checkemail ($ addressarray [$ I]) = false) return false ;}
// -- All valid email addresses are stored in the array
$ This-> mailto = implode ($ addressarray ,",");
Return true ;}
/*************************************** ***********
The setcc ($ inaddress) function sets the CC email address.
The $ inaddress parameter is a string containing one or more email addresses. It is an email address variable,
Use commas to separate multiple email addresses. The default return value is true.
**************************************** **********************/
Function setcc ($ inaddress ){
// -- Use the explode () function to separate the email addresses according ","
$ Addressarray = explode (",", $ inaddress );
// -- Check the validity of the email address cyclically
For ($ I = 0; $ I checkemail ($ addressarray [$ I]) = false) return false ;}
// -- All valid email addresses are stored in the array
$ This-> mailcc = implode ($ addressarray ,",");
Return true ;}
/*************************************** ************
The setbcc ($ inaddress) function sets the parameter $ inaddress to include one or more
Strings of mail addresses, email address variable. Use commas to separate multiple mail addresses. The default return value is
True
**************************************** **/
Function setbcc ($ inaddress ){
// -- Use the explode () function to separate the email addresses according ","
$ Addressarray = explode (",", $ inaddress );
// -- Check the validity of the email address cyclically
For ($ I = 0; $ I {if ($ this-> checkemail ($ addressarray [$ I]) = false)
Return false;
}
// -- All valid email addresses are stored in the array
$ This-> mailbcc = implode ($ addressarray ,",");
Return true;
}
/*************************************** **************************
Function setfrom ($ inaddress): sets the sender address parameter $ inaddress to include emails.
The default return value of an address string is true.
***************************************/
Function setfrom ($ inaddress ){
If ($ this-> checkemail ($ inaddress )){
$ This-> mailfrom = $ inaddress;
Return true;
} Return false ;}
/**********************
The setsubject ($ insubject) function is used to set the email subject parameter $ insubject as a string,
True is returned by default.
**************************************** ***/
Function setsubject ($ insubject ){
If (strlen (TRIM ($ insubject)> 0 ){
$ This-> mailsubject = ereg_replace ("N", "", $ insubject );
Return true ;}
Return false ;}
/*************************************** *************
The settext ($ intext) function sets the email subject parameter $ intext as the text content
The return value is true.
****************************************/
Function settext ($ intext ){
If (strlen (TRIM ($ intext)> 0 ){
$ This-> mailtext = $ intext;
Return true ;}
Return false;
}
/**********************************
The sethtml ($ inhtml) function sets the email subject parameter $ inhtml in HTML format,
The default return value is true.
************************************/
Function sethtml ($ inhtml ){
If (strlen (TRIM ($ inhtml)> 0 ){
$ This-> mailhtml = $ inhtml;
Return true ;}
Return false ;}
/**********************
The setattachments ($ inattachments) function sets the email attachment parameter $ inattachments
It is a string containing a inclusion directory. You can also include multiple files and separate them with commas. The default return value is true.
**************************************** ***/
Function setattachments ($ inattachments ){
If (strlen (TRIM ($ inattachments)> 0 ){
$ This-> mailattachments = $ inattachments;
Return true ;}
Return false ;}
/*********************************
Function checkemail ($ inaddress): this function has been called before, mainly
Used to check the validity of the email address
**************************************** */
Function checkemail ($ inaddress ){
Return (ereg ("^ [^ @] + @ ([a-zA-Z0-9-] + .) + ([a-zA-Z0-9-] {2} | net | com | Gov | mil | org | Edu | INT) $ ", $ inaddress ));
}
/*************************************** **********
Function loadtemplate ($ infilelocation, $ inhash, $ delimiter) to read temporary files and
Replace the useless information parameter $ infilelocation to locate the file directory
$ Inhash because temporary values are stored $ scheme because the email subject is placed
**************************************** *******************/
Function loadtemplate ($ infilelocation, $ inhash, $ delimiter ){
/* For example, the email contains the following content: Dear ~! Username ~,
Your address is ~! Useraddress ~ */
"~!" It is the starting sign "~" Is the end mark
$ Templatedelim = "~ ";
$ Templatenamestart = "! ";
// -- Find these places and replace them
$ Templatelineout = ""; // -- open a temporary file
If ($ templatefile = fopen ($ infilelocation, "R ")){
While (! Feof ($ templatefile )){
$ Templateline = fgets ($ templatefile, 1000 );
$ Templatelinearray = explode ($ templatedelim, $ templateline );
For ($ I = 0; $ I // -- find the starting position
If (strcspn ($ templatelinearray [$ I], $ templatenamestart) = 0 ){
// -- Replace the corresponding value
$ Hashname = substr ($ templatelinearray [$ I], 1 );
// -- Replace the corresponding value
$ Templatelinearray [$ I] = ereg_replace ($ hashname, (string) $ inhash [$ hashname], $ hashname );
}
}
// -- Output character array and overlay
$ Templatelineout. = implode ($ templatelinearray ,"");
} // -- Close the file fclose ($ templatefile );
// -- Set the subject format (text or HTML)
If (strtoupper ($ vertex) = "text ")
Return ($ this-> settext ($ templatelineout ));
Else if (strtoupper ($ notebook) = "html ")
Return ($ this-> sethtml ($ templatelineout ));
} Return false;
}
/*************************************** **
The getrandomboundary ($ offset) function returns a random boundary value.
The parameter $ offset is an integer. A MD5 () encoded string is returned for calling multiple pipelines.
****************************************/
Function getrandomboundary ($ offset = 0 ){
// -- Random Number Generation
Srand (Time () + $ offset );
// -- Returns an MD5 encoded string of 32 characters.
Return ("----". (MD5 (RAND ())));}
/*************************************** *****
Function: getcontenttype ($ infilename) is used to determine the attachment type.
**************************************** ******/
Function getcontenttype ($ infilename ){
// -- Remove the path
$ Infilename = basename ($ infilename );
// -- Remove files without extension
If (strrchr ($ infilename, ".") = false ){
Return "application/octet-stream ";
}
// -- Raise the name extension and make a judgment
$ Extension = strrchr ($ infilename ,".");
Switch ($ extension ){
Case ". GIF": Return "image/GIF ";
Case ". GZ": Return "application/X-gzip ";
Case ". htm": Return "text/html ";
Case ". html": Return "text/html ";
Case ". jpg": Return "image/JPEG ";
Case ". tar": Return "application/X-tar ";
Case ". txt": Return "text/plain ";
Case ". Zip": Return "application/zip ";
Default: Return "application/octet-stream ";
}
Return "application/octet-stream ";
}
/*************************************** *******
The formattextheader function adds the text content to the text file header.
**************************************** *************/
Function formattextheader () {$ outtextheader = "";
$ Outtextheader. = "Content-Type: text/plain;
Charset = US-asciin ";
$ Outtextheader. = "content-transfer-encoding: 7 bitnn ";
$ Outtextheader. = $ this-> mailtext. "N ";
Return $ outtextheader;
}/************************************** **********
The formathtmlheader () function adds the content of the subject to the HTML file header.
**************************************** **/
Function formathtmlheader (){
$ Outhtmlheader = "";
$ Outhtmlheader. = "Content-Type: text/html;
Charset = US-asciin ";
$ Outhtmlheader. = "content-transfer-encoding: 7 bitnn ";
$ Outhtmlheader. = $ this-> mailhtml. "N ";
Return $ outhtmlheader;
}
/**********************************
Function formatattachmentheader ($ infilelocation) identifies attachments in the email
********************************/
Function formatattachmentheader ($ infilelocation ){
$ Outattachmentheader = "";
// -- Use the above function getcontenttype ($ infilelocation) to obtain the attachment type.
$ Contenttype = $ this-> getcontenttype ($ infilelocation );
// -- If the attachment is text, the Standard 7-bit encoding is used.
If (ereg ("text", $ contenttype )){
$ Outattachmentheader. = "Content-Type:". $ contenttype. "; n ";
$ Outattachmentheader. = "name =". basename ($ infilelocation). "." N ";
$ Outattachmentheader. = "content-transfer-encoding: 7 bitn ";
$ Outattachmentheader. = "content-Disposition: attachment; n ";
$ Outattachmentheader. = "filename =". basename ($ infilelocation). "Nn ";
$ Textfile = fopen ($ infilelocation, "R ");
While (! Feof ($ textfile )){
$ Outattachmentheader. = fgets ($ textfile, 1000 );
}
// -- Close the file fclose ($ textfile );
$ Outattachmentheader. = "N ";
}
// -- 64-bit encoding for non-text format
Else {$ outattachmentheader. = "Content-Type:". $ contenttype. "; n ";
$ Outattachmentheader. = "name =". basename ($ infilelocation). "." N ";
$ Outattachmentheader. = "content-transfer-encoding: base64n ";
$ Outattachmentheader. = "content-Disposition: attachment; n ";
$ Outattachmentheader. = "filename =". basename ($ infilelocation). "Nn ";
// -- Call the external command uuencode for encoding
Exec ("uuencode-M $ infilelocation nothing_out", $ returnarray );
For ($ I = 1; $ I <(count ($ returnarray); $ I ++ ){
$ Outattachmentheader. = $ returnarray [$ I]. "N ";
}
} Return $ outattachmentheader;
}
/******************************
The send () function is used to send an email. If the email is sent successfully, the return value is true.
************************************/
Function send (){
// -- Set the mail header to null
$ Mailheader = "";
// -- Add CC recipients
If ($ this-> mailcc! = "")
$ Mailheader. = "cc:". $ this-> mailcc. "N ";
// -- Add a secret CC
If ($ this-> mailbcc! = "")
$ Mailheader. = "BCC:". $ this-> mailbcc. "N ";
// -- Add a sender
If ($ this-> mailfrom! = "")
$ Mailheader. = "from:". $ this-> mailfrom. "N ";
// --------------------------- Mail format ------------------------------
// -- Text format
If ($ this-> mailtext! = "" & $ This-> mailhtml = "" & $ this-> mailattachments = ""){
Return mail ($ this-> mailto, $ this-> mailsubject, $ this-> mailtext, $ mailheader );
}
// -- HTML or text format
Else if ($ this-> mailtext! = "" & $ This-> mailhtml! = "" & $ This-> mailattachments = ""){
$ Bodyboundary = $ this-> getrandomboundary ();
$ Textheader = $ this-> formattextheader ();
$ Htmlheader = $ this-> formathtmlheader ();
// -- Set the mime-version
$ Mailheader. = "mime-version: 1.0n ";
$ Mailheader. = "Content-Type: multipart/alternative; n ";
$ Mailheader. = "boundary =" ". $ bodyboundary." ";
$ Mailheader. = "nnn ";
// -- Add email subject and Boundary
$ Mailheader. = "--". $ bodyboundary. "N ";
$ Mailheader. = $ textheader;
$ Mailheader. = "--". $ bodyboundary. "N ";
// -- Add HTML tags
$ Mailheader. = $ htmlheader;
$ Mailheader. = "n --". $ bodyboundary ."--";
// -- Send an email
Return mail ($ this-> mailto, $ this-> mailsubject, "", $ mailheader );
}
// -- Add HTML and attachments to text
Else if ($ this-> mailtext! = "" & $ This-> mailhtml! = "" & $ This-> mailattachments! = ""){
$ Attachmentboundary = $ this-> getrandomboundary ();
$ Mailheader. = "Content-Type: multipart/mixed; n ";
$ Mailheader. = "boundary =" ". $ attachmentboundary." Nn ";
$ Mailheader. = "this is a multi-part message in MIME format. N ";
$ Mailheader. = "--". $ attachmentboundary. "N ";
$ Bodyboundary = $ this-> getrandomboundary (1 );
$ Textheader = $ this-> formattextheader ();
$ Htmlheader = $ this-> formathtmlheader ();
$ Mailheader. = "mime-version: 1.0n ";
$ Mailheader. = "Content-Type: multipart/alternative; n ";
$ Mailheader. = "boundary =" ". $ bodyboundary." ";
$ Mailheader. = "nnn ";
$ Mailheader. = "--". $ bodyboundary. "N ";
$ Mailheader. = $ textheader;
$ Mailheader. = "--". $ bodyboundary. "N ";
$ Mailheader. = $ htmlheader;
$ Mailheader. = "n --". $ bodyboundary ."--";
// -- Obtain the attachment value
$ Attachmentarray = explode (",", $ this-> mailattachments );
// -- Cycle according to the number of Attachments
For ($ I = 0; $ I // -- split $ mailheader. = "n --". $ attachmentboundary. "N ";
// -- Attachment Information
$ Mailheader. = $ this-> formatattachmentheader ($ attachmentarray [$ I]);
}
$ Mailheader. = "--". $ attachmentboundary ."--";
Return mail ($ this-> mailto, $ this-> mailsubject, "", $ mailheader );
}
Return false;
}
}
?
Usage:
《?
Include "email. Class"
$ Mail-> setto ("a@a.com"); // recipient
$ Mail-> setcc ("B @ B .com, c@c.com"); // CC
$ Mail-> setcc ("d@ B .com, e@c.com"); // Private CC
$ Mail-> setfrom ("f@f.com"); // sender
$ Mail-> setsubject ("topic"); // topic
$ Mail-> settext ("text format"); // The sent text format can also be a variable
$ Mail-> sethtml ("HTML format"); // The HTML format for sending can also be a variable
$ Mail-> setattachments ("C: a.jpg"); // Add an attachment. The path must be specified.
$ Mail-> send (); // send an email
?
Reprinted: http://nczzf.51.net
More articles on "PHP-full-featured email sending"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/324427.html pageno: 14