Class for Sending html, attachments, text files, and html images
Last Update:2018-04-06
Source: Internet
Author: User
Upload the file email. php and emailclass. php to the same directory, and create a subdirectory temp under this directory. set the permission to 0777, and execute email. php to send an email. This emailclass. php function is already quite complete. 1. common text can be sent; 2. HTML text can be sent. 3. text and attachments can be sent simultaneously. 4. you can send an upload file email at the same time. php, emailclass. php to the same directory
Create the subdirectory temp in this directory, set the permission to 0777, and execute email. php to send emails.
This emailclass. php function is already quite complete.
1. common text can be sent;
2. HTML text can be sent;
3. text and attachments can be sent simultaneously;
4. HTML and attachments can be sent simultaneously;
5. You can include images in the HTML when sending HTML messages (this function is available in the class, but you need to design and send PHP programs );
6. the EMAIL can be sent to multiple people, separated;
7. There may be cc recipients, forwarder, or multiple recipients, separated by commas;
If you have any questions, contact William:
William.cn@163.com
/*************************************** ****************************************
Emailclass. php
Name: Email
Description: This class is used for sending emails.
These emails can be
Plain Text, HTML, or Both. Other uses include file
Attachments and email Templates (from a file ).
Testing:
Test_email.php3:
$ Mail-> setTo ("myEmail@yo.com ");
$ Mail-> send ();
Changelog:
Date Name Description
----------------------------------------------------------------------
10/21/1999 R. Chambers created
**************************************** ***************************************/
/*************************************** ****************************************
Issues:
No error reporting
Can only send HTML with TEXT
Can only send attachements with HTML and TEXT
**************************************** ***************************************/
/*************************************** ****************************************
Function Listing:
SetTo ($ inAddress)
SetCC ($ inAddress)
SetBCC ($ inAddress)
SetFrom ($ inAddress)
SetSubject ($ inSubject)
SetText ($ inText)
SetHTML ($ inHTML)
SetAttachments ($ inAttachments)
CheckEmail ($ inAddress)
LoadTemplate ($ inFileLocation, $ inHash, $ delimiter)
GetRandomBoundary ($ offset)
GetContentType ()
FormatTextHeader ()
FormatHTMLHeader ()
FormatAttachmentHeader ($ inFileLocation)
Send ()
**************************************** ***************************************/
Class Email
{
// --- Global Variables
Var $ mailTo = ""; // array of To addresses
Var $ mailCC = ""; // copied recipients
Var $ mailBCC = ""; // hidden recipients
Var $ mailFrom = ""; // from address
Var $ mailSubject = ""; // email subject
Var $ mailText = ""; // plain text message
Var $ mailHTML = ""; // html message
Var $ mailImg = ""; // images of html file
Var $ mailAttachments = ""; // array of attachments
/*************************************** ****************************************
Function: setTo ($ inAddress)
Description: sets the email To address
Arguments: $ inAddress as string
Separate multiple values with comma
Returns: true if set
**************************************** ***************************************/
Function setTo ($ inAddress ){
// -- Split addresses at commas
$ AddressArray = explode (",", $ inAddress );
// -- Loop through each address and exit on error
For ($ I = 0; $ I
If ($ this-> checkEmail ($ addressArray [$ I]) = false) return false;
}
// -- All values are OK so implode array into string
$ This-> mailTo = implode ($ addressArray ,",");
Return true;
}
/*************************************** ****************************************
Function: setCC ($ inAddress)
Description: sets the email cc address
Arguments: $ inAddress as string
Separate multiple values with comma
Returns: true if set
**************************************** ***************************************/
Function setCC ($ inAddress ){
// -- Split addresses at commas
$ AddressArray = explode (",", $ inAddress );
// -- Loop through each address and exit on error
For ($ I = 0; $ I
If ($ this-> checkEmail ($ addressArray [$ I]) = false) return false;
}
// -- All values are OK so implode array into string
$ This-> mailCC = implode ($ addressArray ,",");
Return true;
}
/*************************************** ****************************************
Function: setBCC ($ inAddress)
Description: sets the email bcc address
Arguments: $ inAddress as string
Separate multiple values with comma
Returns: true if set
**************************************** ***************************************/
Function setBCC ($ inAddress ){
// -- Split addresses at commas
$ AddressArray = explode (",", $ inAddress );
// -- Loop through each address and exit on error
For ($ I = 0; $ I
If ($ this-> checkEmail ($ addressArray [$ I]) = false) return false;
}
// -- All values are OK so implode array into string
$ This-> mailBCC = implode ($ addressArray ,",");
Return true;
}
/*************************************** ****************************************
Function: setFrom ($ inAddress)
Description: sets the email FROM address
Arguments: $ inAddress as string (takes single email address)
Returns: true if set
**************************************** ***************************************/
Function setFrom ($ inAddress ){
If ($ this-> checkEmail ($ inAddress )){
$ This-> mailFrom = $ inAddress;
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: setSubject ($ inSubject)
Description: sets the email subject
Arguments: $ inSubject as string
Returns: true if set
**************************************** ***************************************/
Function setSubject ($ inSubject ){
If (strlen (trim ($ inSubject)> 0 ){
$ This-> mailSubject = ereg_replace ("n", "", $ inSubject );
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: setText ($ inText)
Description: sets the email text
Arguments: $ inText as string
Returns: true if set
**************************************** ***************************************/
Function setText ($ inText ){
If (strlen (trim ($ inText)> 0 ){
$ This-> mailText = $ inText;
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: setHTML ($ inHTML)
Description: sets the email HMTL
Arguments: $ inHTML as string
Returns: true if set
**************************************** ***************************************/
Function setHTML ($ inHTML ){
If (strlen (trim ($ inHTML)> 0 ){
$ This-> mailHTML = $ inHTML;
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: setHtmlImages ($ images)
Description: stores the Images string
Arguments: $ images as string with directory encoded ded
Separate multiple values with comma
Returns: true if stored
**************************************** ***************************************/
Function setHtmlImages ($ images ){
If (strlen (trim ($ images)> 0 ){
$ This-> mailImg = $ images;
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: setAttachments ($ inAttachments)
Description: stores the Attachment string
Arguments: $ inAttachments as string with directory encoded ded
Separate multiple values with comma
Returns: true if stored
**************************************** ***************************************/
Function setAttachments ($ inAttachments ){
If (strlen (trim ($ inAttachments)> 0 ){
$ This-> mailAttachments = $ inAttachments;
Return true;
}
Return false;
}
/*************************************** ****************************************
Function: checkEmail ($ inAddress)
Description: checks for valid email
Arguments: $ inAddress as string
Returns: true if valid
**************************************** ***************************************/
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)
Description: reads in a template file and replaces hash values
Arguments: $ inFileLocation as string with relative directory
$ InHash as Hash with populated values
$ Response as string either "text" or "html"
Returns: true if loaded
**************************************** ***************************************/
Function loadTemplate ($ inFileLocation, $ inHash, $ delimiter ){
/*
Template files have lines such:
Dear ~! UserName ~,
Your address is ~! UserAddress ~
*/
// -- Specify template delimeters
$ TemplateDelim = "~ ";
$ TemplateNameStart = "! ";
// -- Set out string
$ TemplateLineOut = "";
// -- Open template file
If ($ templateFile = fopen ($ inFileLocation, "r ")){
// -- Loop through file, line by line
While (! Feof ($ templateFile )){
// -- Get 1000 chars or (line break internal to fgets)
$ TemplateLine = fgets ($ templateFile, 1000 );
// -- Split line into array of hashNames and regular sentences
$ TemplateLineArray = explode ($ templateDelim, $ templateLine );
// -- Loop through array
For ($ I = 0; $ I
// -- Look for $ templateNameStart at position 0
If (strcspn ($ templateLineArray [$ I], $ templateNameStart) = 0 ){
// -- Get hashName after $ templateNameStart
$ HashName = substr ($ templateLineArray [$ I], 1 );
// -- Replace hashName with acual value in $ inHash
// -- (String) casts all values as "strings"
$ TemplateLineArray [$ I] = ereg_replace ($ hashName, (string) $ inHash [$ hashName], $ hashName );
}
}
// -- Output array as string and add to out string
$ TemplateLineOut. = implode ($ templateLineArray ,"");
}
// -- Close file
Fclose ($ templateFile );
// -- Set Mail body to proper format
If (strtoupper ($ vertex) = "TEXT") return ($ this-> setText ($ templateLineOut ));
Else if (strtoupper ($ plugin) = "HTML") return ($ this-> setHTML ($ templateLineOut ));
}
Return false;
}
/*************************************** ****************************************
Function: getRandomBoundary ($ offset)
Description: returns a random boundary
Arguments: $ offset as integer-used for multiple cballs
Returns: string
**************************************** ***************************************/
Function getRandomBoundary ($ offset = 0 ){
// -- Seed random number generator
Srand (time () + $ offset );
// -- Return md5 32 bits plus 4 dashes to make 38 chars
Return ("----". (md5 (rand ())));
}
/*************************************** ****************************************
Function: getContentType ($ inFileName)
Description: returns content type for the file type
Arguments: $ inFileName as file name string (can include path)
Returns: string
**************************************** ***************************************/
Function getContentType ($ inFileName ){
// -- Strip path
$ InFileName = basename ($ inFileName );
// -- Check for no extension
If (strrchr ($ inFileName, ".") = false ){
Return "application/octet-stream ";
}
// -- Get extension and check cases
$ Extension = strrchr ($ inFileName ,".");
Switch ($ extension ){
Case ". gif": return "image/gif ";
Case ". gz": return "application/x-gzip ";
Case ". htm": return "text/html ";
Case ". php": return "text/html ";
Case ". shtml": 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 ";
}
/*************************************** ****************************************
Function: formatTextHeader
Description: returns a formated header for text
Arguments: none
Returns: string
**************************************** ***************************************/
Function formatTextHeader (){
$ OutTextHeader = "";
$ OutTextHeader. = "Content-Type: text/plain; charset = gb2312n ";
$ OutTextHeader. = "Content-Transfer-Encoding: 7 bitnn ";
$ OutTextHeader. = $ this-> mailText. "n ";
Return $ outTextHeader;
}
/*************************************** ****************************************
Function: formatHTMLHeader
Description: returns a formated header for HTML
Arguments: none
Returns: string
**************************************** ***************************************/
Function formatHTMLHeader (){
$ OutHTMLHeader = "";
$ OutHTMLHeader. = "Content-Type: text/html; charset = gb2312n ";
/* $ OutHTMLHeader. = "Content-Type: text/html; charset = us-asciin ";*/
$ OutHTMLHeader. = "Content-Transfer-Encoding: 7 bitnn ";
$ OutHTMLHeader. = $ this-> mailHTML. "n ";
Return $ outHTMLHeader;
}
/*************************************** ****************************************
Function: formatImgHeader ($ inFileLocation)
Description: returns a formated header for an Img
Arguments: $ inFileLocation as string with relative directory
Returns: string
**************************************** ***************************************/
Function formatImgHeader ($ inFileLocation ){
$ OutImgHeader = "";
// -- Get content type based on file extension
$ ContentType = $ this-> getContentType ($ inFileLocation );
// -- Format header
$ OutImgHeader. = "Content-Type:". $ contentType. "; n ";
$ OutImgHeader. = 'name = "'. basename ($ inFileLocation).'" '. "n ";
$ OutImgHeader. = "Content-Transfer-Encoding: base64 n ";
$ OutImgHeader. = "Content-ID: <". basename ($ inFileLocation). "> nn ";
Exec ("uuencode-m $ inFileLocation nothing_out", $ returnArray );
// -- Add each line returned
For ($ I = 1; $ I <(count ($ returnArray); $ I ++ ){
$ OutImgHeader. = $ returnArray [$ I]. "n ";
}
$ OutImgHeader. = "n ";
Return $ outImgHeader;
}
/*************************************** ****************************************
Function: formatAttachmentHeader ($ inFileLocation)
Description: returns a formated header for an Attachment
Arguments: $ inFileLocation as string with relative directory
Returns: string <