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

來源:互聯網
上載者:User
下面這個類的功能則很強大,不但能發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

http://www.bkjia.com/PHPjc/531850.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/531850.htmlTechArticle下面這個類的功能則很強大,不但能發html格式的郵件,還可以發附件 checkEmail($addressArray[$i])==false) return false; } //--所有合法的email地址存入數組...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.