Full-featured PHP send mail class code with detailed instructions _php tips

Source: Internet
Author: User
Tags ereg explode html tags md5 php send mail zip
<?php
Class Email {
---set global variables
var $mailTo = ""; Recipient
var $mailCC = ""; Cc
var $mailBCC = ""; Secret CC
var $mailFrom = ""; Sender
var $mailSubject = ""; Theme
var $mailText = ""; Letter Body in text format
var $mailHTML = ""; Letter body in HTML format
var $mailAttachments = ""; Attachment
/* Function Setto ($inAddress): Address parameters for processing messages $inAddress
For inclusion of one or more strings, email address variables, using commas to split multiple email addresses
The default return value is True
**********************************************************/
function Setto ($inAddress) {
--Use the explode () function to split the email address according to ","
$addressArray = Explode (",", $inAddress);
--Check the legality of the email address by looping
For ($i =0 $i <count ($addressArray); $i + +) {if ($this->checkemail ($addressArray [$i]) ==false) return false;
--all legitimate email addresses are stored in an array
$this->mailto = implode ($addressArray, ",");
return true; }
/**************************************************
function SETCC ($inAddress) set CC email address
Parameter $inAddress is a string containing one or more e-mail addresses, an email address variable,
Use commas to split multiple mail addresses the default return value is True
**************************************************************/
function SETCC ($inAddress) {
--Use the explode () function to split the email address according to ","
$addressArray = Explode (",", $inAddress);
--Check the legality of the email address by looping
For ($i =0 $i <count ($addressArray); $i + +) {if ($this->checkemail ($addressArray [$i]) ==false) return false;
--all legitimate email addresses are stored in an array
$this-&GT;MAILCC = implode ($addressArray, ",");
return true; }
/***************************************************
function SETBCC ($inAddress) Set the secret cc address parameter $inAddress for inclusion of one or more
mail address of the string, email address variables, using commas to split multiple mail addresses the default return value is
True
******************************************/
function Setbcc ($inAddress) {
--Use the explode () function to split the email address according to ","
$addressArray = Explode (",", $inAddress);
--Check the legality of the email address by looping
for ($i =0; $i <count ($addressArray); $i + +)
{if ($this->checkemail ($addressArray [$i]) ==false)
return false;
}
--all legitimate email addresses are stored in an array
$this-&GT;MAILBCC = implode ($addressArray, ",");
return true;
}
/*****************************************************************
function Setfrom ($inAddress): Set Sender address parameters $inAddress for inclusion messages
The string default return value of the address is true
***************************************/
function Setfrom ($inAddress) {
if ($this->checkemail ($inAddress)) {
$this->mailfrom = $inAddress;
return true;
return false; }
/**********************
function Setsubject ($inSubject) is used to set the message subject parameter $insubject as a string.
The default return is True
*******************************************/
function Setsubject ($inSubject) {
if (strlen ($inSubject) > 0) {
$this->mailsubject = ereg_replace ("n", "", $inSubject);
return true; }
return false; }
/****************************************************
function SetText ($inText) sets the message body argument for the text format $inText default for text content
Acknowledge return value is True
****************************************/
function SetText ($inText) {
if (strlen ($inText) > 0) {
$this->mailtext = $inText;
return true; }
return false;
}
/**********************************
function sethtml ($inHTML) sets the message body parameter in HTML format $inhtml to HTML format,
The default return value is True
************************************/
function sethtml ($inHTML) {
if (strlen ($inHTML) > 0) {
$this->mailhtml = $inHTML;
return true; }
return false; }
/**********************
function setattachments ($inAttachments) to set the attachment parameters for a message $inattachments
As a inclusion directory string, can also contain multiple files separated by commas the default return value is True
*******************************************/
function Setattachments ($inAttachments) {
if (strlen ($inAttachments) > 0) {
$this->mailattachments = $inAttachments;
return true; }
return false; }
/*********************************
function Checkemail ($inAddress): This function has been called before, mainly
Used to check the legality of an 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, $inFormat) reads temporary files and
Replace the useless information parameter $infilelocation the directory used to locate the file
$inHash because storing a temporary value $inFormat because the drop message body
***********************************************************/
function LoadTemplate ($inFileLocation, $inHash, $inFormat) {
/* such as the following content in the message: Dear ~! username~,
Your address is ~! useraddress~ *
--"~!" End flag for Start sign "~"
$templateDelim = "~";
$templateNameStart = "!";
--Find these places and replace them.
$templateLineOut = ""; --Open temporary file
if ($templateFile = fopen ($inFileLocation, "R")) {
while (!feof ($templateFile)) {
$templateLine = Fgets ($templateFile, 1000);
$templateLineArray = Explode ($templateDelim, $templateLine);
for ($i =0; $i <count ($templateLineArray); $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, "");
//--Closes the file fclose ($templateFile);
--Set body formatting (text or HTML)
if (Strtoupper ($inFormat) = = "TEXT")
Return ($this->settext ($templateLineOut));
else if (strtoupper ($inFormat) = = "HTML")
Return ($this->sethtml ($templateLineOut));
return false;
}
/*****************************************
function Getrandomboundary ($offset) returns a random boundary value
Parameter $offset to integers – calls for multiple pipes return a MD5 () encoded string
****************************************/
function getrandomboundary ($offset = 0) {
--Random number generation
Srand (Time () + $offset);
--Returns a string of 32-bit characters length encoded by MD5
Return ("----". MD5 (rand ())); }
/********************************************
Function: getContentType ($inFileName) to determine the type of attachment
**********************************************/
function getContentType ($inFileName) {
--Remove path
$inFileName = basename ($inFileName);
--Remove files with no extension
if (STRRCHR ($inFileName, ".") = = False) {
return "Application/octet-stream";
}
--the extension and judgment of the lifting area
$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";
}
/**********************************************
function Formattextheader file header with text content added
*****************************************************/
function Formattextheader () {$outTextHeader = "";
$outTextHeader. = "Content-type:text/plain;
Charset=us-asciin ";
$outTextHeader. = "CONTENT-TRANSFER-ENCODING:7BITNN";
$outTextHeader. = $this->mailtext. "N";
return $outTextHeader;
} /************************************************
function Formathtmlheader () file header with HTML for the message body content
******************************************/
function Formathtmlheader () {
$outHTMLHeader = "";
$outHTMLHeader. = "content-type:text/html;
Charset=us-asciin ";
$outHTMLHeader. = "CONTENT-TRANSFER-ENCODING:7BITNN";
$outHTMLHeader. = $this->mailhtml. "N";
return $outHTMLHeader;
}
/**********************************
function Formatattachmentheader ($inFileLocation) identifies the attachment in the message
********************************/
function Formatattachmentheader ($inFileLocation) {
$outAttachmentHeader = "";
--Use the above function getContentType ($inFileLocation) to derive the attachment type
$contentType = $this->getcontenttype ($inFileLocation);
--Standard 7-bit encoding if the attachment is a text type
if (Ereg ("text", $contentType)) {
$outAttachmentHeader. = "Content-type:". $contentType. "; n ";
$outAttachmentHeader. = ' name= '. basename ($inFileLocation). '"'. " n ";
$outAttachmentHeader. = "CONTENT-TRANSFER-ENCODING:7BITN";
$outAttachmentHeader. = "Content-disposition:attachment;n";
$outAttachmentHeader. = ' filename= '. basename ($inFileLocation). '"'. " nn ";
$textFile = fopen ($inFileLocation, "R");
while (!feof ($textFile)) {
$outAttachmentHeader. = Fgets ($textFile, 1000);
}
--Close File fclose ($textFile);
$outAttachmentHeader. = "n";
}
--Non-text format is encoded with 64 bits
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 ";
--Invoke 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;
}
/******************************
function send () sends a message with a successful return value of True
************************************/
function Send () {
--Set message headers to empty
$mailHeader = "";
--Add cc person
if ($this-&GT;MAILCC!= "")
$mailHeader. = "CC:". $this-&GT;MAILCC. " n ";
--Add Secret cc person
if ($this->mailbcc!= "")
$mailHeader. = "BCC:". $this->mailbcc. " n ";
--Add Sender
if ($this->mailfrom!= "")
$mailHeader. = "From:" $this->mailfrom. " n ";
---------------------------Message 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 mime-version
$mailHeader. = "mime-version:1.0n";
$mailHeader. = "Content-type:multipart/alternative;n";
$mailHeader. = ' boundary= '. $bodyBoundary. '"';
$mailHeader. = "nnn";
--adding message bodies and boundaries
$mailHeader. = "--" $bodyBoundary. "N";
$mailHeader. = $textHeader;
$mailHeader. = "--" $bodyBoundary. "N";
--Add HTML tags
$mailHeader. = $htmlHeader;
$mailHeader. = "n--". $bodyBoundary. "--";
--Send mail
Return mail ($this->mailto, $this->mailsubject, "", $mailHeader);
}
--text plus HTML plus attachments
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. "--";
--Get Attachment value
$attachmentArray = Explode (",", $this->mailattachments);
--loop According to the number of attachments
for ($i =0; $i <count ($attachmentArray); $i + +) {
--split $mailHeader. = "n--". $attachmentBoundary. "N";
--Attachment information
$mailHeader. = $this->formatattachmentheader ($attachmentArray [$i]);
}
$mailHeader. = "--" $attachmentBoundary. "--";
Return mail ($this->mailto, $this->mailsubject, "", $mailHeader);
}
return false;
}
}
?>
How to use:
Copy Code code 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"); Secret CC
$mail->setfrom ("f@f.com")//Sender
$mail->setsubject ("subject"); Theme
$mail->settext ("text Format");//Send Text format can also be a variable
$mail->sethtml ("HTML format");//Send HTML format can also be a variable
$mail->setattachments ("c:a.jpg");//Add attachments to indicate path
$mail->send (); Send mail
?>

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.