程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 轉帖PHP的類--功能齊全的發送郵件類---抱歉作者不好意思我忘了地址了,我從本地拷貝上來的

轉帖PHP的類--功能齊全的發送郵件類---抱歉作者不好意思我忘了地址了,我從本地拷貝上來的

編輯:關於PHP編程

下面這個類的功能則很強大,不但能發html格式的郵件,還可以發附件 checkEmail($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){ //--用explode()函數根據”,”對郵件地址進行分割 $addressArray = explode( ",",$inAddress); //--通過循環對郵件地址的合法性進行檢查 for($i=0;$icheckEmail($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 true; } return false; } /********************** 函數 setSubject($inSubject) 用於設置郵件主題參數$inSubject為字串, 默認返回的是true *******************************************/ function setSubject($inSubject){ if(strlen(trim($inSubject)) > 0){ $this->mailSubject = ereg_replace( "n", "",$inSubject); return true; } return false; } /**************************************************** 函數setText($inText) 設置文本格式的郵件主體參數 $inText 為文本內容默 認返回值為true ****************************************/ function setText($inText){ if(strlen(trim($inText)) > 0){ $this->mailText = $inText; return true; } return false; } /********************************** 函數setHTML($inHTML) 設置html格式的郵件主體參數$inHTML為html格式, 默認返回值為true ************************************/ function setHTML($inHTML){ if(strlen(trim($inHTML)) > 0){ $this->mailHTML = $inHTML; return true; } return false; } /********************** 函數 setAttachments($inAttachments) 設置郵件的附件 參數$inAttachments 為一個包涵目錄的字串,也可以包涵多個文件用逗號進行分割 默認返回值為true *******************************************/ function setAttachments($inAttachments){ if(strlen(trim($inAttachments)) > 0){ $this->mailAttachments = $inAttachments; return true; } return false; } /********************************* 函數 checkEmail($inAddress) :這個函數我們前面已經調用過了,主要就是 用於檢查email地址的合法性 *****************************************/ function checkEmail($inAddress){ return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inAddress)); } /************************************************* 函數loadTemplate($inFileLocation,$inHash,$inFormat) 讀取臨時文件並且 替換無用的信息參數$inFileLocation用於定位文件的目錄 $inHash 由於存儲臨時的值 $inFormat 由於放置郵件主體 ***********************************************************/ function loadTemplate($inFileLocation,$inHash,$inFormat){ /* 比如郵件內有如下內容: Dear ~!UserName~, Your address is ~!UserAddress~ */ //--其中”~!”為起始標志”~”為結束標志 $templateDelim = "~"; $templateNameStart = "!"; //--找出這些地方並把他們替換掉 $templateLineOut = ""; //--打開臨時文件 if($templateFile = fopen($inFileLocation, "r")){ while(!feof($templateFile)){ $templateLine = fgets($templateFile,1000); $templateLineArray = explode($templateDelim,$templateLine); for( $i=0; $isetText($templateLineOut)); else if( strtoupper($inFormat)== "HTML" ) return($this->setHTML($templateLineOut)); } return false; } /***************************************** 函數 getRandomBoundary($offset) 返回一個隨機的邊界值 參數 $offset 為整數 – 用於多管道的調用 返回一個md5()編碼的字串 ****************************************/ function getRandomBoundary($offset = 0){ //--隨機數生成 srand(time()+$offset); //--返回 md5 編碼的32位 字符長度的字串 return ( "----".(md5(rand()))); } /******************************************** 函數: getContentType($inFileName)用於判斷附件的類型 **********************************************/ function getContentType($inFileName){ //--去除路徑 $inFileName = basename($inFileName); //--去除沒有擴展名的文件 if(strrchr($inFileName, ".") == false){ return "application/octet-stream"; } //--提區擴展名並進行判斷 $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"; } /********************************************** 函數formatTextHeader把文本內容加上text的文件頭 *****************************************************/ function formatTextHeader(){ $outTextHeader = ""; $outTextHeader .= "Content-Type: text/plain; charset=us-asciin"; $outTextHeader .= "Content-Transfer-Encoding: 7bitnn"; $outTextHeader .= $this->mailText. "n"; return $outTextHeader; } /************************************************ 函數formatHTMLHeader()把郵件主體內容加上html的文件頭 ******************************************/ function formatHTMLHeader(){ $outHTMLHeader = ""; $outHTMLHeader .= "Content-Type: text/html; charset=us-asciin"; $outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn"; $outHTMLHeader .= $this->mailHTML. "n"; return $outHTMLHeader; } /********************************** 函數 formatAttachmentHeader($inFileLocation) 把郵件中的附件標識出來 ********************************/ function for

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved