PHP class full-featured email sending class _ PHP Tutorial

Source: Internet
Author: User
Tags ereg
PHP provides a full range of email sending functions. Copy the code as follows :? PhpclassEmail {--- set the global variable var $ mailTo; recipient var $ mailCC; CC var $ mailBCC; secretly CC var $ mailFrom The code is as follows:


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 // -- Search for 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:

The code is as follows:


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
?>

The http://www.bkjia.com/PHPjc/315502.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/315502.htmlTechArticle code is as follows :? Php class Email {// --- set the global variable var $ mailTo = ""; // The recipient var $ mailCC = ""; // CC var $ mailBCC = ""; // secretly CC var $ mailFrom =...

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.