国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP的類 功能齊全的發(fā)送郵件類

PHP的類 功能齊全的發(fā)送郵件類

Jun 13, 2016 pm 12:41 PM
class email php code overall situation send variable copy of kind set up mail

復(fù)制代碼 代碼如下:


class Email {
//---設(shè)置全局變量
var $mailTo = ""; // 收件人
var $mailCC = ""; // 抄送
var $mailBCC = ""; // 秘密抄送
var $mailFrom = ""; // 發(fā)件人
var $mailSubject = ""; // 主題
var $mailText = ""; // 文本格式的信件主體
var $mailHTML = ""; // html格式的信件主體
var $mailAttachments = ""; // 附件
/* 函數(shù)setTo($inAddress) :用于處理郵件的地址 參數(shù) $inAddress
為包涵一個或多個字串,email地址變量,使用逗號來分割多個郵件地址
默認(rèn)返回值為true
**********************************************************/
function setTo($inAddress){
//--用explode()函數(shù)根據(jù)”,”對郵件地址進(jìn)行分割
$addressArray = explode( ",",$inAddress);
//--通過循環(huán)對郵件地址的合法性進(jìn)行檢查
for($i=0;$icheckEmail($addressArray[$i])==false) return false; }
//--所有合法的email地址存入數(shù)組中
$this->mailTo = implode($addressArray, ",");
return true; }
/**************************************************
函數(shù) setCC($inAddress) 設(shè)置抄送人郵件地址
參數(shù) $inAddress 為包涵一個或多個郵件地址的字串,email地址變量,
使用逗號來分割多個郵件地址 默認(rèn)返回值為true
**************************************************************/
function setCC($inAddress){
//--用explode()函數(shù)根據(jù)”,”對郵件地址進(jìn)行分割
$addressArray = explode( ",",$inAddress);
//--通過循環(huán)對郵件地址的合法性進(jìn)行檢查
for($i=0;$icheckEmail($addressArray[$i])==false) return false; }
//--所有合法的email地址存入數(shù)組中
$this->mailCC = implode($addressArray, ",");
return true; }
/***************************************************
函數(shù)setBCC($inAddress) 設(shè)置秘密抄送地址 參數(shù) $inAddress 為包涵一個或多
個郵件地址的字串,email地址變量,使用逗號來分割多個郵件地址 默認(rèn)返回值為
true
******************************************/
function setBCC($inAddress){
//--用explode()函數(shù)根據(jù)”,”對郵件地址進(jìn)行分割
$addressArray = explode( ",",$inAddress);
//--通過循環(huán)對郵件地址的合法性進(jìn)行檢查
for($i=0;$i{ if($this->checkEmail($addressArray[$i])==false)
return false;
}
//--所有合法的email地址存入數(shù)組中
$this->mailBCC = implode($addressArray, ",");
return true;
}
/*****************************************************************
函數(shù)setFrom($inAddress):設(shè)置發(fā)件人地址 參數(shù) $inAddress 為包涵郵件
地址的字串默認(rèn)返回值為true
***************************************/
function setFrom($inAddress){
if($this->checkEmail($inAddress)){
$this->mailFrom = $inAddress;
return true;
} return false; }
/**********************
函數(shù) setSubject($inSubject) 用于設(shè)置郵件主題參數(shù)$inSubject為字串,
默認(rèn)返回的是true
*******************************************/
function setSubject($inSubject){
if(strlen(trim($inSubject)) > 0){
$this->mailSubject = ereg_replace( "n", "",$inSubject);
return true; }
return false; }
/****************************************************
函數(shù)setText($inText) 設(shè)置文本格式的郵件主體參數(shù) $inText 為文本內(nèi)容默
認(rèn)返回值為true
****************************************/
function setText($inText){
if(strlen(trim($inText)) > 0){
$this->mailText = $inText;
return true; }
return false;
}
/**********************************
函數(shù)setHTML($inHTML) 設(shè)置html格式的郵件主體參數(shù)$inHTML為html格式,
默認(rèn)返回值為true
************************************/
function setHTML($inHTML){
if(strlen(trim($inHTML)) > 0){
$this->mailHTML = $inHTML;
return true; }
return false; }
/**********************
函數(shù) setAttachments($inAttachments) 設(shè)置郵件的附件 參數(shù)$inAttachments
為一個包涵目錄的字串,也可以包涵多個文件用逗號進(jìn)行分割 默認(rèn)返回值為true
*******************************************/
function setAttachments($inAttachments){
if(strlen(trim($inAttachments)) > 0){
$this->mailAttachments = $inAttachments;
return true; }
return false; }
/*********************************
函數(shù) checkEmail($inAddress) :這個函數(shù)我們前面已經(jīng)調(diào)用過了,主要就是
用于檢查email地址的合法性
*****************************************/
function checkEmail($inAddress){
return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inAddress));
}
/*************************************************
函數(shù)loadTemplate($inFileLocation,$inHash,$inFormat) 讀取臨時文件并且
替換無用的信息參數(shù)$inFileLocation用于定位文件的目錄
$inHash 由于存儲臨時的值 $inFormat 由于放置郵件主體
***********************************************************/
function loadTemplate($inFileLocation,$inHash,$inFormat){
/* 比如郵件內(nèi)有如下內(nèi)容: Dear ~!UserName~,
Your address is ~!UserAddress~ */
//--其中”~!”為起始標(biāo)志”~”為結(jié)束標(biāo)志
$templateDelim = "~";
$templateNameStart = "!";
//--找出這些地方并把他們替換掉
$templateLineOut = ""; //--打開臨時文件
if($templateFile = fopen($inFileLocation, "r")){
while(!feof($templateFile)){
$templateLine = fgets($templateFile,1000);
$templateLineArray = explode($templateDelim,$templateLine);
for( $i=0; $i//--尋找起始位置
if(strcspn($templateLineArray[$i],$templateNameStart)==0){
//--替換相應(yīng)的值
$hashName = substr($templateLineArray[$i],1);
//--替換相應(yīng)的值
$templateLineArray[$i] = ereg_replace($hashName,(string)$inHash[$hashName],$hashName);
}
}
//--輸出字符數(shù)組并疊加
$templateLineOut .= implode($templateLineArray, "");
} //--關(guān)閉文件fclose($templateFile);
//--設(shè)置主體格式(文本或html)
if( strtoupper($inFormat)== "TEXT" )
return($this->setText($templateLineOut));
else if( strtoupper($inFormat)== "HTML" )
return($this->setHTML($templateLineOut));
} return false;
}
/*****************************************
函數(shù) getRandomBoundary($offset) 返回一個隨機(jī)的邊界值
參數(shù) $offset 為整數(shù) – 用于多管道的調(diào)用 返回一個md5()編碼的字串
****************************************/
function getRandomBoundary($offset = 0){
//--隨機(jī)數(shù)生成
srand(time()+$offset);
//--返回 md5 編碼的32位 字符長度的字串
return ( "----".(md5(rand()))); }
/********************************************
函數(shù): getContentType($inFileName)用于判斷附件的類型
**********************************************/
function getContentType($inFileName){
//--去除路徑
$inFileName = basename($inFileName);
//--去除沒有擴(kuò)展名的文件
if(strrchr($inFileName, ".") == false){
return "application/octet-stream";
}
//--提區(qū)擴(kuò)展名并進(jìn)行判斷
$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";
}
/**********************************************
函數(shù)formatTextHeader把文本內(nèi)容加上text的文件頭
*****************************************************/
function formatTextHeader(){ $outTextHeader = "";
$outTextHeader .= "Content-Type: text/plain;
charset=us-asciin";
$outTextHeader .= "Content-Transfer-Encoding: 7bitnn";
$outTextHeader .= $this->mailText. "n";
return $outTextHeader;
} /************************************************
函數(shù)formatHTMLHeader()把郵件主體內(nèi)容加上html的文件頭
******************************************/
function formatHTMLHeader(){
$outHTMLHeader = "";
$outHTMLHeader .= "Content-Type: text/html;
charset=us-asciin";
$outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
$outHTMLHeader .= $this->mailHTML. "n";
return $outHTMLHeader;
}
/**********************************
函數(shù) formatAttachmentHeader($inFileLocation) 把郵件中的附件標(biāo)識出來
********************************/
function formatAttachmentHeader($inFileLocation){
$outAttachmentHeader = "";
//--用上面的函數(shù)getContentType($inFileLocation)得出附件類型
$contentType = $this->getContentType($inFileLocation);
//--如果附件是文本型則用標(biāo)準(zhǔn)的7位編碼
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);
}
//--關(guān)閉文件 fclose($textFile);
$outAttachmentHeader .= "n";
}
//--非文本格式則用64位進(jìn)行編碼
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";
//--調(diào)用外部命令uuencode進(jìn)行編碼
exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
for ($i = 1; $i$outAttachmentHeader .= $returnArray[$i]. "n";
}
} return $outAttachmentHeader;
}
/******************************
函數(shù) send()用于發(fā)送郵件,發(fā)送成功返回值為true
************************************/
function send(){
//--設(shè)置郵件頭為空
$mailHeader = "";
//--添加抄送人
if($this->mailCC != "")
$mailHeader .= "CC: ".$this->mailCC. "n";
//--添加秘密抄送人
if($this->mailBCC != "")
$mailHeader .= "BCC: ".$this->mailBCC. "n";
//--添加發(fā)件人
if($this->mailFrom != "")
$mailHeader .= "FROM: ".$this->mailFrom. "n";
//---------------------------郵件格式------------------------------
//--文本格式
if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
}
//--html或text格式
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){
$bodyBoundary = $this->getRandomBoundary();
$textHeader = $this->formatTextHeader();
$htmlHeader = $this->formatHTMLHeader();
//--設(shè)置 MIME-版本
$mailHeader .= "MIME-Version: 1.0n";
$mailHeader .= "Content-Type: multipart/alternative;n";
$mailHeader .= ' boundary="'.$bodyBoundary. '"';
$mailHeader .= "nnn";
//--添加郵件主體和邊界
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $textHeader;
$mailHeader .= "--".$bodyBoundary. "n";
//--添加html標(biāo)簽
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--發(fā)送郵件
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
//--文本加html加附件
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. "--";
//--獲取附件值
$attachmentArray = explode( ",",$this->mailAttachments);
//--根據(jù)附件的個數(shù)進(jìn)行循環(huán)
for($i=0;$i//--分割 $mailHeader .= "n--".$attachmentBoundary. "n";
//--附件信息
$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
}
$mailHeader .= "--".$attachmentBoundary. "--";
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
return false;
}
}
?>

??

使用方法:

復(fù)制代碼 代碼如下:



Include “email.class”

$mail->setTo("a@a.com"); //收件人
$mail-> setCC("b@b.com,c@c.com"); //抄送
$mail-> setCC("d@b.com,e@c.com"); //秘密抄送
$mail->setFrom(“f@f.com”);//發(fā)件人
$mail->setSubject(“主題”) ; //主題
$mail->setText(“文本格式”) ;//發(fā)送文本格式也可以是變量
$mail->setHTML(“html格式”) ;//發(fā)送html格式也可以是變量
$mail->setAttachments(“c:a.jpg”) ;//添加附件,需表明路徑
$mail->send(); //發(fā)送郵件
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

What is Late Static Binding in PHP? What is Late Static Binding in PHP? Jul 13, 2025 am 02:36 AM

LateStaticBindinginPHPallowsstatic::torefertotheclassinitiallycalledatruntimeininheritancescenarios.BeforePHP5.3,self::alwaysreferencedtheclasswherethemethodwasdefined,causingChildClass::sayHello()tooutput"ParentClass".Withlatestaticbinding

See all articles