php mail函数类

来源:互联网 发布:两个表格查找相同数据 编辑:程序博客网 时间:2024/05/21 17:35
class.email.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])==falsereturn false; }
    
//--所有合法的email地址存入数组中
    $this->mailto = implode($addressarray, ",");
//    echo "<b>TO:" . $addressarray[0] ."</b>";
    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])==falsereturn 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" ", "",$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)

    ifstrtoupper($informat)== "text" )
    
return($this->settext($templatelineout));
    
else ifstrtoupper($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-ascii
";
    
$outtextheader .= " content-transfer-encoding: 7bit ";
    
$outtextheader .= $this->mailtext. " ";
    
return $outtextheader;
    } 
/************************************************
    函数formathtmlheader()把邮件主体内容加上html的文件头
    *****************************************
*/
    
function formathtmlheader(){
    
$outhtmlheader = "";
    
$outhtmlheader .= "content-type: text/html;
    charset=us-ascii
";
    
$outhtmlheader .= " content-transfer-encoding: 7bit ";
    
$outhtmlheader .= $this->mailtext . " " . $this->mailhtml . " ";
    
return $outhtmlheader;
    }
    
/**********************************
    函数 formatattachmentheader($infilelocation) 把邮件中的附件标识出来
    *******************************
*/
    
function formatattachmentheader($infilelocation){
    
$outattachmentheader = "";
    
//--用上面的函数getcontenttype($infilelocation)得出附件类型
    $contenttype = $this->getcontenttype($infilelocation);
    
//--如果附件是文本型则用标准的7位编码
    if(ereg"text",$contenttype)){
    
$outattachmentheader .= "content-type: ".$contenttype. "; ";
    
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. " ";
    
$outattachmentheader .= "content-transfer-encoding: 7bit ";
    
$outattachmentheader .= "content-disposition: attachment; ";
    
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. " ";
    
$textfile = fopen($infilelocation, "r");
    
while(!feof($textfile)){
    
$outattachmentheader .= fgets($textfile,1000);
    }
    
//--关闭文件 fclose($textfile);
    $outattachmentheader .= " ";
    }
    
//--非文本格式则用64位进行编码
    else$outattachmentheader .= "content-type: ".$contenttype. "; ";
    
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. " ";
    
$outattachmentheader .= "content-transfer-encoding: base64 ";
    
$outattachmentheader .= "content-disposition: attachment; ";
    
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. " ";
    
//--调用外部命令uuencode进行编码
    exec"uuencode -m $infilelocation nothing_out",$returnarray);
    
for ($i = 1$i<(count($returnarray)); $i++){
    
$outattachmentheader .= $returnarray[$i]. " ";
    }
    } 
return $outattachmentheader;
    }
    
/******************************
    函数 send()用于发送邮件,发送成功返回值为true
    ***********************************
*/
    
function send(){
    
//--设置邮件头为空
    $mailheader = "";
    
//邮件体
    $mailbody = "";
    
//--添加抄送人
    if($this->mailcc != "")
    
$mailheader .= "cc: ".$this->mailcc. " ";
    
//--添加秘密抄送人
    if($this->mailbcc != "")
    
$mailheader .= "bcc: ".$this->mailbcc. " ";
    
//--添加发件人
    if($this->mailfrom != "")
    
$mailheader .= "from: ".$this->mailfrom. " ";
    
//--设置 mime-版本
    $mailheader .= "mime-version: 1.0 ";

    
//---------------------------邮件格式------------------------------
    //--文本格式

    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();
   
    
$mailheader .= "content-type: multipart/alternative; ";
    
$mailheader .= ' boundary="'.$bodyboundary. '"';
    
$mailheader .= " ";
    
//--添加邮件主体和边界
    $mailbody .= "--".$bodyboundary. " ";
    
$mailbody .= $textheader;
    
$mailbody .= " --".$bodyboundary. " ";
    
//--添加html标签
    $mailbody .= $htmlheader;
    
$mailbody .= " --".$bodyboundary. "-- ";
    
//--发送邮件
    return mail($this->mailto,$this->mailsubject, $mailbody,$mailheader);
    }
    
//--文本加html加附件
    else if($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments != ""){
    
$attachmentboundary = $this->getrandomboundary();
    
$mailheader .= "content-type: multipart/mixed; ";
    
$mailheader .= ' boundary="'.$attachmentboundary. '"'. " ";
    
$mailheader .= "this is a multi-part message in mime format. ";
    
$mailheader .= "--".$attachmentboundary. " ";
    
$mailbody = $mailheader;

    
$bodyboundary = $this->getrandomboundary(1);
    
$textheader = $this->formattextheader();
    
$htmlheader = $this->formathtmlheader();
    
$mailbody .= "content-type: multipart/alternative; ";
    
$mailbody .= ' boundary="'.$bodyboundary. '"';
    
$mailbody .= " ";
    
$mailbody .= "--".$bodyboundary. " ";
    
$mailbody .= $textheader;
    
$mailbody .= " --".$bodyboundary. " ";
    
$mailbody .= $htmlheader;
    
$mailbody .= " --".$bodyboundary. "-- ";

    
//--获取附件值
    $attachmentarray = explode",",$this->mailattachments);
    
//--根据附件的个数进行循环
    for($i=0;$i<count($attachmentarray);$i++){
        
//--分割 $mailbody .= " --".$attachmentboundary. " ";
        $mailbody .= "--".$attachmentboundary. " ";
        
//--附件信息
        $mailbody .= $this->formatattachmentheader($attachmentarray[$i]);
    }
        
$mailbody .= "--".$attachmentboundary. "--";
        
return mail($this->mailto,$this->mailsubject, $mailbody,$mailheader);
    }
        
return false;
    }
}
?>


mailtest.php
<? 
include ("class.email.php");

 
echo realpath("f1.txt");

$mail = new email();
$mail->setto("xxx@xxx"); //收件人 
//$mail->setcc("b@b.com,c@c.com"); //抄送 
//$mail->setcc("d@b.com,e@c.com"); //秘密抄送 

$mail->setfrom("xa@xd.com");//发件人 
$mail->setsubject("subject") ; //主题 
$mail->settext("test mail") ;//发送文本格式也可以是变量 
$mail->sethtml("html part") ;//发送html格式也可以是变量 
$mail->setattachments(  realpath("f1.txt". "," .   realpath("f2.txt")  ) ;//添加附件,需表明路径 
$mail->send(); //发送邮件 
echo "<br><b>Mail send!</b>";

?>
原创粉丝点击