欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

推荐个功能齐全的发送PHP邮件类

程序员文章站 2023-12-28 09:31:22
php--下面这个类的功能则很强大,不但能发html格式的邮件,还可以发附件 使用方法:复制代码 代码如下:
php--下面这个类的功能则很强大,不但能发html格式的邮件,还可以发附件
使用方法:
复制代码 代码如下:
<?   
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   
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;$i<count($addressarray);$i++){ if($this->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;$i<count($addressarray);$i++){ if($this->checkemail($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<count($addressarray);$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<count($templatelinearray);$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<(count($returnarray)); $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<count($attachmentarray);$i++){   
//--分割 $mailheader .= "n--".$attachmentboundary. "n";   
//--附件信息   
$mailheader .= $this->formatattachmentheader($attachmentarray[$i]);   
}   
$mailheader .= "--".$attachmentboundary. "--";   
return mail($this->mailto,$this->mailsubject, "",$mailheader);   
}   
return false;   
}   
}   
?>

上一篇:

下一篇: