phpmailer发送邮件代码
发布时间:2022-01-21 00:41:44 所属栏目:PHP教程 来源:互联网
导读:本文章收藏了两款利用phpmailer来发送邮件,当前如果你的机器配置好了php自带的邮件发送功能那更好哦,mail()这个那就更方便了。 */ function smtp_mail ($sendto_email,$subject,$body) { global $mailconfig,$_cfg; $mail = new phpmailer(); $mail-issmtp()
本文章收藏了两款利用phpmailer来发送邮件,当前如果你的机器配置好了php自带的邮件发送功能那更好哦,mail()这个那就更方便了。 */ function smtp_mail ($sendto_email,$subject,$body) { global $mailconfig,$_cfg; $mail = new phpmailer(); $mail->issmtp(); $mail->host = $mailconfig['smtpservers'];//smtp servers $mail->smtpauth = true;// 启用smtp验证功能 $mail->username = $mailconfig['smtpusername']; $mail->password = $mailconfig['smtppassword']; $mail->from =$mailconfig['smtpfrom']; $mail->fromname =$_cfg['site_name']; $mail->charset = "gb2312"; $mail->encoding = "base64"; $mail->addaddress($sendto_email,"");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是addaddress("收件人email","收件人姓名") $mail->addreplyto($mailconfig['addreplyto'],"");//增加回复标签addreplyto $mail->ishtml(true); $mail->subject = $subject; $mail->body =$body; $mail->altbody ="text/html";//该属性的设置是在邮件正文不支持html的备用显示 if(!$mail->send()) { return false; //echo "邮件发送有误 "; //echo "邮件错误信息: " . $mail->errorinfo; } else { return true; } } 方法二 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<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; } } ?> 使用方法: include "email.class" $mail->setto("a@a.com"); //收件人 $mail-> setcc("b@b.com,[url=mailto:c@c.com]c@c.com[/url]"); //抄送 $mail-> setcc("d@b.com,[url=mailto:e@c.com]e@c.com[/url]"); //秘密抄送 $mail->setfrom("[url=mailto:f@f.com]f@f.com[/url]");//发件人 $mail->setsubject("主题") ; //主题 $mail->settext("文本格式") ;//发送文本格式也可以是变量 $mail->sethtml("html格式") ;//发送html格式也可以是变量 $mail->setattachments("c:a.jpg") ;//添加附件,需表明路径 $mail->send(); //发送邮件 ?> ![]() (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |