首頁 後端開發 php教程 PHP的类 功能齐全的发送邮件类_php基础

PHP的类 功能齐全的发送邮件类_php基础

May 17, 2016 am 09:48 AM
傳送郵件

复制代码 代码如下:

class Email {
//---设置全局变量
var $mailTo = ""; // 收件人
var $mailCC = ""; // 抄送
var $mailBCC = ""; // 秘密抄送
var $mailFrom = ""; // 发件人
var $mailSubject = ""; // 主题
var $mailText = ""; // 文本格式的信件主体
var $mailHTML = ""; // html格式的信件主体
var $mailAttachments = ""; // 附件
/* 函数setTo($inAddress) :用于处理邮件的地址 参数 $inAddress
为包涵一个或多个字串,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){
//--用explode()函数根据”,”对邮件地址进行分割
$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 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; $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($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 formatAttachmentHeader($inFileLocation){
$outAttachmentHeader = "";
//--用上面的函数getContentType($inFileLocation)得出附件类型
$contentType = $this->getContentType($inFileLocation);
//--如果附件是文本型则用标准的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);
}
//--关闭文件 fclose($textFile);
$outAttachmentHeader .= "n";
}
//--非文本格式则用64位进行编码
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";
//--调用外部命令uuencode进行编码
exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
for ($i = 1; $i$outAttachmentHeader .= $returnArray[$i]. "n";
}
} return $outAttachmentHeader;
}
/******************************
函数 send()用于发送邮件,发送成功返回值为true
************************************/
function send(){
//--设置邮件头为空
$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 == ""){
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();
//--设置 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标签
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--发送邮件
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);
//--根据附件的个数进行循环
for($i=0;$i//--分割 $mailHeader .= "n--".$attachmentBoundary. "n";
//--附件信息
$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
}
$mailHeader .= "--".$attachmentBoundary. "--";
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
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(); //发送邮件
?>
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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

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

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

PHP使用PHPMailer傳送多人郵件的方法和步驟 PHP使用PHPMailer傳送多人郵件的方法和步驟 May 22, 2023 pm 06:10 PM

在Web應用程式中,往往需要將郵件一次發送給多個收件者。 PHP是一種很流行的Web開發語言,而PHPMailer是一種常見的發送郵件的PHP類別庫。 PHPMailer提供了豐富的接口,使得在PHP應用程式中發送郵件變得更加方便和易於使用。在本篇文章中,我們將介紹如何使用PHPMailer向多個收件者傳送郵件的方法和步驟。下載PHPMailer首先需要在官網(

PHP開發實務:使用PHPMailer傳送郵件到MySQL資料庫的用戶 PHP開發實務:使用PHPMailer傳送郵件到MySQL資料庫的用戶 Aug 05, 2023 pm 06:21 PM

PHP開發實務:使用PHPMailer發送郵件到MySQL資料庫中的使用者引言:在現代互聯網建設中,郵件是一種重要的溝通工具。無論是用戶註冊、密碼重置,還是電子商務中的訂單確認,發送電子郵件都是必不可少的功能。本文將介紹如何使用PHPMailer來傳送電子郵件,並將郵件資訊儲存到MySQL資料庫中的使用者資訊表中。一、安裝PHPMailer庫PHPMailer是

如何使用Flask-Mail發送電子郵件 如何使用Flask-Mail發送電子郵件 Aug 02, 2023 am 10:17 AM

如何使用Flask-Mail發送電子郵件隨著互聯網的發展,電子郵件已經成為了人們溝通的重要工具。在開發網路應用程式中,有時候我們需要在特定的場景下發送電子郵件,例如用戶註冊成功後發送歡迎郵件,或是用戶忘記密碼時發送重設密碼郵件等。 Flask是一款簡單又靈活的PythonWeb框架,而Flask-Mail是Flask框架下用於發送郵件的擴充庫,本文將介紹如何

Python連接阿里雲接口,實現郵件發送功能 Python連接阿里雲接口,實現郵件發送功能 Jul 05, 2023 pm 04:33 PM

Python連接阿里雲接口,實現郵件發送功能阿里雲提供了一系列的服務接口,其中包括了郵件發送服務。透過Python腳本連接阿里雲接口,我們可以實現郵件的快速發送。本篇文章將向您展示如何使用Python腳本連接阿里雲接口,並實現郵件發送功能。首先,我們需要在阿里雲上申請郵件發送服務,以取得對應的介面資訊。在阿里雲端管理控制台中,選擇郵件推播服務,然後建立新的郵

如何使用PHP佇列發送郵件? 如何使用PHP佇列發送郵件? Sep 13, 2023 am 08:00 AM

如何使用PHP佇列發送郵件?在現代的Web開發中,我們經常需要發送大量的電子郵件。無論是大量發送電子郵件給大量用戶,還是根據用戶行為發送個人化的電子郵件,使用佇列來發送郵件是一個非常好的實踐。佇列可以幫助我們提高郵件發送的效率和穩定性,避免因為發送太多郵件而導致伺服器負載過高,同時還可以處理發送失敗的場景。在PHP開發中,我們可以使用常見的佇列工具,如Rab

PHP使用PHPMailer傳送郵件的方法和注意事項 PHP使用PHPMailer傳送郵件的方法和注意事項 May 22, 2023 pm 11:40 PM

隨著互聯網技術的發展和網路的普及,越來越多的應用程式需要使用電子郵件進行通訊。而PHP作為一種流行的伺服器端程式語言,自然也需要在網站開發中使用到發送郵件的功能。而PHPMailer作為一個開源的PHP郵件類別庫,可以方便快速地在PHP程式中傳送郵件。本文將介紹如何使用PHPMailer發送郵件以及注意事項。一、PHPMailer簡介PHP

PHP使用email()函數傳送郵件的方法 PHP使用email()函數傳送郵件的方法 May 22, 2023 pm 03:10 PM

隨著網路的不斷發展和普及,電子郵件成為人們日常交流中不可或缺的一部分。在網站後台開發過程中,很多時候都需要使用PHP發送郵件,以滿足郵件通知、註冊驗證等功能。 PHP提供了email()函數來實作郵件的傳送,而且使用起來也非常簡單。本文將詳細介紹如何使用PHP的email()函數傳送郵件。一、SMTP配置在使用email()函數傳送郵件前,需要先對SMTP進行設定。

PHP使用mail函數發送郵件的完整過程 PHP使用mail函數發送郵件的完整過程 May 22, 2023 am 08:00 AM

PHP使用mail函數發送郵件的完整過程隨著網路科技的發展,電子郵件在日常生活中扮演著越來越重要的角色,人們發送和接收郵件已經成為必不可少的工作和生活方式。而在網站開發中,也經常需要透過郵件的形式進行各種通知、驗證、註冊等等操作。本篇文章將介紹PHP使用mail函數傳送郵件的完整流程。一、mail函數的基本形式在PHP中,用來發送郵件的函數是mail()。

See all articles