PHP class full-featured Send mail class _php tutorial

Source: Internet
Author: User
Tags ereg php class
Copy CodeThe code is as follows:
Class Email {
---setting global variables
var $mailTo = ""; Recipient
var $mailCC = ""; Cc
var $mailBCC = ""; Secret CC
var $mailFrom = ""; Sender
var $mailSubject = ""; Theme
var $mailText = ""; Text-Formatted letter body
var $mailHTML = ""; HTML-formatted letter body
var $mailAttachments = ""; Attachment
/* Function Setto ($inAddress): Address parameter used to process the message $inAddress
To contain one or more strings, email address variables, use commas to split multiple e-mail addresses
The default return value is True
**********************************************************/
function Setto ($inAddress) {
--Use the explode () function to split the e-mail address according to ","
$addressArray = Explode (",", $inAddress);
--checking the legality of e-mail addresses through loops
for ($i =0; $i Checkemail ($addressArray [$i]) ==false) return false; }
--all legitimate email addresses are stored in the array
$this->mailto = implode ($addressArray, ",");
return true; }
/**************************************************
function SETCC ($inAddress) Set cc person email address
The parameter $inAddress is a string with one or more email 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 e-mail address according to ","
$addressArray = Explode (",", $inAddress);
--checking the legality of e-mail addresses through loops
for ($i =0; $i Checkemail ($addressArray [$i]) ==false) return false; }
--all legitimate email addresses are stored in the array
$this->MAILCC = implode ($addressArray, ",");
return true; }
/***************************************************
The function setbcc ($inAddress) sets the secret cc address parameter $inAddress to encompass one or more
e-mail address strings, 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 e-mail address according to ","
$addressArray = Explode (",", $inAddress);
--checking the legality of e-mail addresses through loops
for ($i =0; $i {if ($this->checkemail ($addressArray [$i]) ==false)
return false;
}
--all legitimate email addresses are stored in the array
$this->MAILBCC = implode ($addressArray, ",");
return true;
}
/*****************************************************************
function Setfrom ($inAddress): Set Sender address parameter $inAddress as inclusion message
The default return value of the address string is true
***************************************/
function Setfrom ($inAddress) {
if ($this->checkemail ($inAddress)) {
$this->mailfrom = $inAddress;
return true;
} return false; }
/**********************
The function Setsubject ($inSubject) is used to set the message 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 function SetText ($inText) sets the message body parameter in text format $inText as text content
The return value is True
****************************************/
function SetText ($inText) {
if (strlen (Trim ($inText)) > 0) {
$this->mailtext = $inText;
return true; }
return false;
}
/**********************************
The function sethtml ($inHTML) sets the HTML-formatted message body parameter $inhtml to HTML format,
The default return value is True
************************************/
function sethtml ($inHTML) {
if (strlen (Trim ($inHTML)) > 0) {
$this->mailhtml = $inHTML;
return true; }
return false; }
/**********************
The function setattachments ($inAttachments) sets the attachment parameter of the message $inattachments
As a string of inclusion directories, you can also contain multiple files separated by 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
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));
}
/*************************************************
The function loadtemplate ($inFileLocation, $inHash, $inFormat) reads the temporary file and
Replace the useless information parameter $infilelocation the directory used to locate the file
$inHash because a temporary value is stored $inFormat because the message body is placed
***********************************************************/
function LoadTemplate ($inFileLocation, $inHash, $inFormat) {
/* such as the message contains the following content: Dear ~! username~,
Your address is ~! useraddress~ * *
--Among them "~ ~" End flag for Start flag "~"
$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 --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 File fclose ($templateFile);
--Set the body format (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 integer – Call for multi-pipe Returns a MD5 () encoded string
****************************************/
function getrandomboundary ($offset = 0) {
--Random number generation
Srand (Time () + $offset);
--Returns a string of MD5 encoded 32-bit character length
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";
}
--Extension and judgment of the zone
$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 to add text content to the file header
*****************************************************/
function Formattextheader () {$outTextHeader = "";
$outTextHeader. = "Content-type:text/plain;
Charset=us-asciin ";
$outTextHeader. = "CONTENT-TRANSFER-ENCODING:7BITNN";
$outTextHeader. = $this->mailtext. "N";
return $outTextHeader;
} /************************************************
function Formathtmlheader () adds the message body content to the HTML file header
******************************************/
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) identify the attachments in the message
********************************/
function Formatattachmentheader ($inFileLocation) {
$outAttachmentHeader = "";
--Using the above function getContentType ($inFileLocation) to derive the attachment type
$contentType = $this->getcontenttype ($inFileLocation);
--If the attachment is text, use the standard 7-bit encoding
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-bit
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 external command uuencode to encode
EXEC ("Uuencode-m $inFileLocation nothing_out", $returnArray);
for ($i = 1; $i < (count ($returnArray)); $i + +) {
$outAttachmentHeader. = $returnArray [$i]. "N";
}
} return $outAttachmentHeader;
}
/******************************
The function send () is used to send the message, and the Send success return value is True
************************************/
function Send () {
--Set the message header to empty
$mailHeader = "";
--Add cc person
if ($this-&GT;MAILCC! = "")
$mailHeader. = "CC:". $this-&GT;MAILCC. " n ";
--Add a 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";
--add 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 was 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 --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 CodeThe 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"); 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 an attachment to indicate the path
$mail->send (); Send mail
?>

http://www.bkjia.com/PHPjc/315502.html www.bkjia.com true http://www.bkjia.com/PHPjc/315502.html techarticle Copy the code as follows:? PHP class Email {//---Set global variable var $mailTo = ""; The recipient var $mailCC = ""; CC var $mailBCC = ""; Secret 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.