搜尋
首頁後端開發php教程詳細說明 功能齊全的PHP發送郵件類別代碼附詳細說明

class Email { 
//---設定全域變數 
var $mailTo = ""; // 收件者 
var $CC"; 🎜>var $mailBCC = ""; // 秘密抄送 
var $mailFrom = ""; // 寄件人 
var $mailSubject // 寄件人 
var $mailSubject =no""; "; // 文字格式的信件主體 
var $mailHTML = ""; // html格式的信件主體 
var $mailAttachments = ""; // 1 為包涵一個或多個字符串,email地址變數,使用逗號來分割多個郵件地址 
預設回傳值為true 
***** ************************************************** ***/ 
function setTo($inAddress){ 
//--用explode()函數依據」,」分割郵件地址進行分割 
$addressArray = explode( ",",$inAddress) ; 
//--透過循環對郵件地址的合法性進行檢查 
for($i=0;$icheckEmail($ addressArray[$i])==false) return false; } 
//--所有合法的email地址存入數組中 
$this->mailTo = implode($addressArray, ","); 🎜>return true; } 
/************************************************** 
函數 setCC($inAddress) 設定抄送人郵件地址 
參數 $inAddress 為包涵一個或多個郵件地址的字符串,email地址變數, 
使用逗號來分割多個郵件地址 預設回傳值為true 
********************************************** ****************/ 
function setCC($inAddress){ 
//--用explode()函數根據」,”分割郵件地址>$addressArray = explode( ",",$inAddress); 
//--透過循環對郵件地址的合法性進行檢查 
for($i=0;$icheckEmail($addressArray[$i])==false) return false; } 
//--所有合法的email地址存入數組中 
$this-> mailCC = implode($addressArray, ","); 
return true; } 
/*************************************************** 
函數setBCC($inAddress) 設定秘密副本位址 參數 $inAddress 為包涵一個或多 
個郵件地址的字串,email位址變數,使用逗號來分割多個郵件地址 預設回傳值為 
true 
******************************************/ 
function setBCC($inAddress){  / )函數依據」,」分割郵件位址 
$addressArray = explode( ",",$inAddress); 
//--透過循環檢查郵件位址的合法性 
for($ i=0;$i{ if($this->checkEmail($addressArray[$i])==false) 
return false; 

//--所有合法的email地址存入數組中 
$this->mailBCC = implode($addressArray, ","); 
return true; 

/**** ************************************************** ************* 
函數setFrom($inAddress):設定寄件者位址 參數 $inAddress 為包涵郵件 
位址的字串預設回傳值為true 
* **************************************/ 
function setFrom($inAddress){ 
if($this->checkEmail($inAddress)){ 
$this->mailFrom  $inAddress; 🎜>} return false; } 
/********************** 
函數 setSubject($inSubject) 用於設定郵件主題參數$inSubject為字串, 
預設回傳的是true 
*******************************************/ 
function setSubject($inSubject){ 
if(strlen(trim($inSubject)){ 
if(strlen(trim($inSubject))  ->mailSubject = ereg_replace( "n", "",$inSubject); 
return true; } 
return false; } ){ 
if(strlen(trim($inText)) > 0){ 
$this->mailText = $inText; 
return true; } 
return 
return true; } 
return 
/********************************** 
函數setHTML($inHTML) 設定html格式的郵件主體參數$inHTML為html格式, 
預設回傳值為true 
************************************/ 
function setHTML($inHTML){ 
if(strlen(trim($inHTML)) > 0){ 
$this->HTHTML 
return true; } 
return false; } 
/********************** 
函數 setAttachments($inAttachments) 設定郵件的附件 參數$inAttachments 
為一個包涵目錄的字串,也可以包涵多個檔案以逗號進行分割 預設回傳值為true 
*********************************** ********/ 
function setAttachments($inAttachments){ 
function setAttachments($inAttachments){ 
if. ){ 
$this->mailAttachments = $inAttachments; 
return true; } 
return false; }  return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov| mil|org|edu|int)$",$inAddress)); 

/********************************* 
函數 checkEmail($inAddress) :這個函數我們前面已經呼叫過了,主要是 
用來檢查email地址的合法性 
********************************** *******/ 
function loadTemplate($inFileLocation,$inHash,$inFormat){ 
/* 例如郵件內有以下內容: Dear ~!UserName~, 
Your address is ~!UserAddress~ */ 
//--其中」~!」為起始標誌」~」為結束標誌 
//--其中」~!」為起始標誌」~」為結束標誌 
$templateDelim = "~"; 
$templateNameStart = "!"; 
//--找出這些地方並把他們替換掉 
$templateLine = Out"; /--";檔案 
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){ 
//--取代對應的值 
$hashName = substr($templateLineArray[$i],1); 
 //--取代對應的值 
$templateLineArray[$i] = ereg_replace($hashName,(string)$inHash[$hashName],$hashName); 


// --輸出字元陣列並疊加 
$templateLineOut .= implode($templateLineArray, ""); 
} //--關閉檔案fclose($templateFile); 
//--設定主體格式(文字或html) 
if( strtoupper($inFormat)== "TEXT" ) 
return($this->setText($templateLineOut)); 
else if( strtoupper($inFormtoupper($inFormat)== HTML" ) 
return($this->setHTML($templateLineOut)); 
} return false; 

/**//--隨機數產生 
srand(time()+$offset); 
//--回傳 md5 編碼的32位元 字元長度的字串 
return ( " ----".(md5(rand()))); } 
/************************************************* 
函數loadTemplate($inFileLocation,$inHash,$inFormat) 讀取臨時檔案並且 
取代無用資訊的參數$inFileLocation用於定位檔案的目錄 
$inHash 由於儲存暫時的值 $inFormat 
$inHash 由於儲存臨時的值 $inFormat 🎜>************************************************ ***********/ 
function getContentType($inFileName){ 
//--移除路徑 
$inFileName = basename($inFileName); 
//--移除沒有副檔名的檔案 
if(strrchr($inFileName, ".") == false){ 
return ".") == false){ 
return "application/oct-oapp stream"; 
}
//--提區副檔名並判斷
$extension = strrchr($inFileName, "."); 
switch($extension){ 
case  ".gif":return " image/gif"; 
case“.gz”:返回“application/x-gzip”; 
case“.htm”:返回“text/html”; 
case“.html”:返回“ text/html”; 
case“.jpg”:返回“image/jpeg”; 
case“.tar”:返回“application/x-tar”; 
case“.txt”:返回“ text/plain”; 
case「.zip」:回傳「application/zip」; 
預設值:回傳「application/octet-stream」; 

回傳「application/octet-stream ”; 

/********************************************** 
函數formatTextHeader把文字內容加上text的文件頭 
*************************************** **************/ 
function formatTextHeader(){ $outTextHeader = ""; 
$outTextHeader .= = ""; 
$outTextHeader .= charset=us-asciin」; 
$outTextHeader .=「內容傳輸編碼:7bitnn」; 
$outTextHeader .= $this->mailText. 「n」; 
回傳$outHeader; /************************************************ 
函數formatHTMLHeader()把郵件主體內容加上html的文件頭 
**************************************** ********/ 
函數formatHTMLHeader(){ 
$outHTMLHeader = ""; 
$outHTMLHeader .= 「內容類型:text/html;
$outHTMLHeader .=「內容傳輸編碼:7bitnn」; 
$outHTMLHeader .= $this->mailHTML. 「n」; 
返回$outHTMLHeader; 🎝>*/ 
函數formatAttachmentHeader($inFileLocation){ 
$outAttachmentHeader = ""; 
//--用上面的函數? ->getContentType($inFileLocation); 
//--如果附件是文字型則用標準的7位編碼
if(ereg("text",$contentType)){ 
$outAttachmentHeader .= "Content-Type: ".$內容類型。 “;n”; 
$outAttachmentHeader .= ' name="'.basename($inFileLocation).'"'. 「n」; 
$outAttachmentHeader .=「內容編碼:7bitn」;>$outAttachmentHeader .=「內容編碼:7bitn」; > $outAttachmentHeader .=「內容處置:附件;n」; 
$outAttachmentHeader .= ' filename="'.basename($inFileLocation).'"'. 「nn」; 
$textFile = fopen($inFileation , "r"); 
while(!feof($textFile)){ 
$outAttachmentHeader .= fgets($textFile,1000); 

//-- textFile); 
$outAttachmentHeader .= 「n」; 

//--非文字格式則以64位元進行編碼
else{ $outAttachmentHeader .= $contentType. 「;n」; 
$outAttachmentHeader .= ' name="'.basename($inFileLocation).'"'. 「n」; 
$outAttachmentHeader .= 「n」; 
$outAttachmentHeader .= 「內容編碼:64nn」);
$outAttachmentHeader .= 「內容處置:附件;n」; 
$outAttachmentHeader .= ' filename="'.basename($inFileLocation).'"'. 「nn」; filename="'.basename($inFileLocation).'"'. 「nn」; 
/------呼叫外部指令uuencode編碼
exec("uuencode -m $inFileLocation nothing_out",$returnArray); 
for ($i = 1; $i$outAttachmentHeader.= $returnArray[$i]. 「n」; 

} 回傳$outAttachmentHeader; 
}
/****************************** 
函數 send()用於傳送郵件,傳送成功回傳值為true 
* ***********************************/ 
function send(){ 
//--設定郵件頭為空 
$mailHeader = ""; 
//--新增副本>$mailHeader = ""; 
//--新增副本人 
if($this->mailCC != "") 
$mailHeader .= "CC: ".$this->mailCC. "n"; 
//--新增秘密副本人
if($this->mailBCC != "") 
$mailHeader .= "BCC: ".$this->mailBCC. "n"; 
//--新增寄件者 
if($this->mailFrom != "") 
$mailHeader .= "FROM: ".$this->mailFrom. "n"; 
//---------- -----------------郵件格式------------------------------ 
//--文字格式 
if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){ 
$this->mailTo,$this->mailSubject,$this->mailText,$mailHeader); 

//--html或text格式 
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){ 
$bodyBoundary = $this->getRandom ary(); ); 
$htmlHeader = $this->formatHTMLHeader(); 
//--設定 MIME-版本 
$mailHeader .= "MIME-Version:ader1.0n"; "Content-Type: multipart/alternative;n"; 
$mailHeader .= ' boundary="'.$bodyBoundary. '"'; 
$mailHeader -- "nnn"; 新增郵件主體與邊界 
$mailHeader .= "--".$bodyBoundary. "n"; 
$mailHeader .= $textHeader; 
$mailHeader n"; 
//--新增html標籤 
$mailHeader .= $htmlHeader; 
$mailHeader .= "n--".$bodyBoundary. "--"; 
///- -寄email 
return mail($this->mailTo,$this->mailSubject, "",$mailHeader); 

//--文字加html加上附件
else if(>//--文字加html加上附件
else if( $this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){ 
$attachmentBoundary =$mail$->getRandomBoundary(); .= "Content-Type: multipart/mixed;n"; 
$mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";  -part message in MIME format.n"; 
$mailHeader .= "--".$attachmentBoundary. "n"; 
$bodyBoundary  $this->getRandomaderBoundary formatTextHeader(); 
$htmlHeader = $this->formatHTMLHeader(); 
$mailHeader .= "MIME-Version: 1.0n"; 
$Header.com multipart/alternative;n"; 
$mailHeader .= ' boundary="'.$bodyBoundary. '"'; 
$mailHeader .= "nnn"; $bodyBoundary. "n"; 
$mailHeader .= $textHeader; 
$mailHeader .= "--".$bodyBoundary. "n"; 
$mailHeader.].$bodyBoundary. "n"; 
$mailHeader. mailHeader .= "n--".$bodyBoundary. "--"; 
//--取得附件值 
$attachmentArray = explode( ",",$this-mail>mailAttachments); = explode( ",",$this-mail>mailAttachments); 
/ /--依附件的數量進行循環 
for($i=0;$i//--分割 $mailHeader .= "n--" .$attachmentBoundary. "n"; 
//--附件資訊 
$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]); 

return mail($this->mailTo,$this->mailSubject, "",$mailHeader); 

return false; 

return false; > } 

?>
使用方法: 



複製程式碼

程式碼如下:


 
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”);//寄件者 
$mail->setSubject(“主題”) ; //主題 
$mail->setText(“文字格式” ) ;//傳送文字格式也可以是變數 
$mail->setHTML(「html格式」) ;//傳送html格式也可以是變數 
$mail->setAttachments(「c:a.jpg ”) ;//新增附件,需表明路徑 
$mail->send(); //發送郵件 
?>

以上就介紹了詳細說明 功能齊全的PHP發送郵件類代碼附詳細說明,包括了詳細說明方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
unset()和session_destroy()有什麼區別?unset()和session_destroy()有什麼區別?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

在負載平衡的情況下,什麼是粘性會話(會話親和力)?在負載平衡的情況下,什麼是粘性會話(會話親和力)?May 04, 2025 am 12:16 AM

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

PHP中有哪些不同的會話保存處理程序?PHP中有哪些不同的會話保存處理程序?May 04, 2025 am 12:14 AM

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的會話是什麼?為什麼使用它們?PHP中的會話是什麼?為什麼使用它們?May 04, 2025 am 12:12 AM

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

說明PHP會話的生命週期。說明PHP會話的生命週期。May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!