php实现的基于文本模板的邮件发送类

发布时间:2019-07-13编辑:脚本学堂
本文介绍下,php实现的一个邮件发送类,基于文本模板来发送邮件,挺不错的,有需要的朋友参考下吧。

一个php email邮件发送类,可以构造简单的邮件格式,效果不错。
代码:

<? 
/**
* php email邮件发送类
* 编辑:www.jb200.com
*/
class cls_email_module 
{ 
   function mailSend($tomail,$frommail,$subj,$message,$html="") 
   { 
    if($html) 
 { 
  $headers  = "MIME-Version: 1.0rn"; 
  $headers .= "Content-type: text/html; charset=iso-8859-1rn"; 
  $headers .="From:$frommail"; 
 } 
   else 
   { 
 $headers .="From:$frommail"; 
   } 
   
 $sSubject = $subj; 
   $sBody = $message; 
   
   //    Parsing body 
   
   $body = $sBody; 
   
      for($ptr = 0; $ptr < strlen($body); $ptr++) 
       { 
       switch($body[ $ptr ]) 
   { 
        case "{": 
    $is_token = 1; 
    $token = ""; 
    break; 
       case "}"; 
    $is_token = 0; 
    eval("global $".strtolower($token).";"); 
    eval("$sVal = $".strtolower($token).";"); 
    $sBody = str_replace("{{$token}}", "$sVal", $sBody); 
   
    $token = ""; 
    break; 
   } 
   
    if(($body[ $ptr ] != "{" && $body[ $ptr ] != "}") && $is_token) 
   { 
        $token .= $body[ $ptr ]; 
   
       } 
   } 
     $re=mail($tomail, $sSubject,$sBody,$headers); 
     $toShow="<table cellpadding=2 cellspacing=0 border=0 bordercolor=#ffffff>"; 
     $toShow.="<tr>"; 
     $toShow.="<td bgcolor=#84C7FF><font face=arial size=2><b>Subject</b></font></td>"; 
     $toShow.="<td bgcolor=#84C7FF><font face=arial size=2>$subj</font></td>"; 
     $toShow.="</tr>"; 
     $toShow.="<tr>"; 
     $toShow.="<td bgcolor=#84C7FF><font face=arial size=2><b>Message</b></font></td>"; 
     $toShow.="<td bgcolor=#84C7FF valign=top><font face=arial size=2>$sBody</font></td>"; 
     $toShow.="</tr>"; 
     $toShow.="</table>";      
      return $toShow; 
    } 
}

调用示例:

<? 
  include("email.php");    
  $msg='Hello {FNAME} {LNAME}, <br> 
      有关您的账号激活邮件。<br> 
      username:: {USERNAME}<br> 
      password:: {PASSWORD}<br>'; 
  $sub="激活账号"; 
   
  $from="admin@abc.com"; 
  $to="user@pqr.com"; 
   
  $email= new cls_email_module(); 
   
  $fname="UsersFirstName"; 
  $lname="UsersLastName"; 
  $username="usernameF"; 
  $password="usernameP"; 
   
  $msg=$email->mailSend($to,$from,$sub,$msg); 
  echo $msg;  
?>