php 邮件类

没试过不知道行不行。。。

 

PHP代码
  1. <?php   
  2. /**  
  3.  *名称:Email.class.php  
  4.  *作用:邮件类  
  5.  *说明:  
  6.  *作者:ningjinfeng@126.com  
  7.  *时间:2005-11-18  
  8.  *更新:2008-10-15  
  9.  **/  
  10. class Email   
  11. {   
  12.  /******************************************  
  13.    *函数:sendMail  
  14.    *作用:发送邮件函数  
  15.    *输入:$to:收件人(一个收件人 a@123.com,多个 a@1.com,b@2.com)  
  16.    ,$subject:主题,$message:内容,$from:发件人地址,$fromname:发件人名称,$ishtml:是否为html邮件(1/0),$cc:抄送的邮件,$bcc:暗送的邮件  
  17.    *输出:true/false  
  18.    *用法:$to:可为多个邮件,每个邮件用半角逗号隔开;$from为:somebody@example.com;$to和$subject中不能有换行的字符;  
  19.    **  
  20.    ******************************************  
  21.    *--制作--日期--  
  22.    *KuaiYigang@xingmo.com  2004-07-13 15:00  
  23.    ******************************************  
  24.    *--修改--日期--目的--  
  25.    *  
  26.    */  
  27.  function sendMail($to$subject$message$from$fromname$ishtml=1, $FILE=''$cc=''$bcc=''$charset = 'gbk'$type=4)   
  28.  {   
  29.   //mail方式发送   
  30.   if($type == 1)   
  31.   {      
  32.    $tousers = array();   
  33.    foreach(explode(','$toas $touser)    
  34.    {   
  35.     $tousers[] = preg_match('/^(.+?) \<(.+?)\>$/',$touser$temp) ? ($fromname ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : $temp[2]) : $touser;   
  36.    }   
  37.    $to = implode(','$tousers);   
  38.    $subject = '=?'.$charset.'?B?'.base64_encode(str_replace("\r"''str_replace("\n"''$subject))).'?=';   
  39.    $message = chunk_split(base64_encode(str_replace("\r\n."" \r\n.."str_replace("\n""\r\n"str_replace("\r""\n"str_replace("\r\n""\n"str_replace("\n\r""\r"$message)))))));   
  40.    //$from = $from == '' ? '=?'.$charset.'?B?'.base64_encode($fromname)."?= <$from>" : (preg_match('/^(.+?) \<(.+?)\>$/',$from, $temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : $from);   
  41.    $from = preg_match('/^(.+?) \<(.+?)\>$/',$from$temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : '=?'.$charset.'?B?'.base64_encode($fromname)."?= <$from>";   
  42.   
  43.    if ($ishtml == 1)   
  44.    {   
  45.     if($FILE['name'] <> '')   
  46.     {   
  47.      //附件    
  48.      $file = $FILE['tmp_name'];    
  49.      // 定义分界线    
  50.      $boundary = uniqid( "");    
  51.      $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";    
  52.      $headers .= "From:$from\r\n";    
  53.      //确定上传文件的MIME类型    
  54.      if($FILE['type'])    
  55.      $mimeType = $FILE['type'];    
  56.      else    
  57.      $mimeType ="application/unknown";    
  58.      //文件名    
  59.      $fileName = $FILE['name'];    
  60.   
  61.      // 打开文件    
  62.      $fp = fopen($file"r");    
  63.      // 把整个文件读入一个变量    
  64.      $read = fread($fpfilesize($file));    
  65.      //我们用base64方法把它编码    
  66.      $read = base64_encode($read);    
  67.      //把这个长字符串切成由每行76个字符组成的小块    
  68.      $read = chunk_split($read);    
  69.      //现在我们可以建立邮件的主体    
  70.      $body = "--$boundary   
  71.      Content-type: text/plain; charset=iso-8859-1   
  72.      Content-transfer-encoding: 8bit   
  73.      $message   
  74.      --$boundary   
  75.      Content-type: $mimeType; name=$fileName   
  76.      Content-disposition: attachment; filename=$fileName   
  77.      Content-transfer-encoding: base64   
  78.      $read   
  79.      --$boundary--";    
  80.      //发送邮件    
  81.      $mail = mail($to$subject,$body,$headers);   
  82.      return $mail;   
  83.     }   
  84.     else  
  85.     {   
  86.      /* To send HTML mail, you can set the Content-type header. */  
  87.      $headers  = "MIME-Version: 1.0\r\n";   
  88.      $headers .= "Content-type: text/html; charset=".$charset."\r\n";   
  89.      /* additional headers */  
  90.      $headers .= "From: ".$from."\r\n";   
  91.      $headers .= "Cc: ".$cc."\r\n";   
  92.      $headers .= "Bcc: ".$bcc."\r\n";   
  93.   
  94.      $headers .= "Content-Transfer-Encoding: base64\r\n";   
  95.   
  96.      /* and now mail it */  
  97.      $mail = @mail($to$subject$message$headers);   
  98.      return $mail;   
  99.     }   
  100.    }   
  101.    else  
  102.    {   
  103.     /*  
  104.     $mail = mail($to, $subject, $message,  
  105.         "From: ".$from."\r\n"  
  106.        ."Reply-To: ".$from."\r\n"  
  107.        ."X-Mailer: PHP/" . phpversion());  
  108.  
  109.     return $mail;  
  110.     */  
  111.     /* To send HTML mail, you can set the Content-type header. */  
  112.     $headers  = "MIME-Version: 1.0\r\n";   
  113.     $headers .= "Content-type: text/plain; charset=".$charset."\r\n";   
  114.     /* additional headers */  
  115.     $headers .= "From: ".$from."\r\n";   
  116.     $headers .= "Cc: ".$cc."\r\n";   
  117.     $headers .= "Bcc: ".$bcc."\r\n";   
  118.   
  119.     $headers .= "Content-Transfer-Encoding: base64\r\n";   
  120.   
  121.     /* and now mail it */  
  122.     $mail = @mail($to$subject$message$headers);   
  123.     return $mail;   
  124.    }   
  125.   }   
  126.   
  127.   //sendmail   
  128.   if($type == 2)   
  129.   {   
  130.    if ( $from == '')   
  131.    {   
  132.     $from = '163邮箱帐号';   
  133.    }   
  134.    if ( $ishtml == 1 )   
  135.    {   
  136.     $header  = "MIME-Version: 1.0\r\n";   
  137.     $header .= "Content-type: text/html; charset=gb2312\r\n";   
  138.     $header .= "From: ".$from."\r\n";   
  139.     $header .= "Subject: ".$subject."\r\n";   
  140.     $header .= "Cc: \r\n";   
  141.     $header .= "Bcc: \r\n";   
  142.     $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";   
  143.    }   
  144.    else  
  145.    {   
  146.     $header  = "Return-Path: ".$from."\r\n";   
  147.     $header .= "From: ".$from."\r\n";   
  148.     $header .= "Cc: \r\n";   
  149.     $header .= "Bcc: \r\n";   
  150.     $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";   
  151.    }   
  152.    if ( !@$mailok = popen("/usr/sbin/sendmail ".$to"w") )   
  153.    {   
  154.     return false;   
  155.    }   
  156.    fputs($mailok$header);   
  157.    fputs($mailok$message);   
  158.   
  159.    $result = pclose($mailok) >> 8 & 0xFF;   
  160.    if($result != 0)   
  161.    {   
  162.     return false;   
  163.    }   
  164.   
  165.    return true;   
  166.   }   
  167.   
  168.   //SMTP   
  169.   if($type == 3)   
  170.   {   
  171.    $host = 'smtp.163.com';   
  172.    $username = '163邮箱帐号';   
  173.    $password = '163邮箱密码';   
  174.   
  175.    $res = $this -> sendSmtpMail($to,$subject,$message,$from$fromname$host$username$password$ishtml);   
  176.    return $res ? true :false;   
  177.   }   
  178.   
  179.   //phpmailer   
  180.   if($type == 4)   
  181.   {   
  182.    $res = $this->sendPhpMail($to,$subject,$message,$from$fromname$ishtml$FILE$cc,$bcc$charset);   
  183.    return $res ? true :false;   
  184.   }   
  185.  }   
  186.   
  187.   
  188.  function sendSmtpMail($to,$subject,$message,$from$fromname$host$username$password$ishtml=0)   
  189.  {   
  190.   require '../inc/class.phpmailer.php';   
  191.   $mail = new PHPMailer();   
  192.   
  193.   $mail->IsSMTP();          // send via SMTP   
  194.   $mail->Host      = $host;       // SMTP servers   
  195.   $mail->SMTPAuth  = true;                       // turn on SMTP authentication   
  196.   $mail->Username  = $username;      // SMTP username   
  197.   $mail->Password  = $password;             // SMTP password   
  198.   
  199.   $mail->From      =  $from;   
  200.   $mail->FromName  = $fromname;   
  201.   $mail->AddAddress($to);   
  202.   $mail->CharSet = 'gbk';   
  203.   if ($ishtml == 1)   
  204.   {   
  205.    $mail->IsHTML(true);                           // send as HTML   
  206.   }   
  207.   else  
  208.   {   
  209.    $mail->IsHTML(false);                           // send as HTML   
  210.   }   
  211.   $mail->Subject   = $subject;   
  212.   $mail->Body      =  $message;   
  213.   
  214.   return ($mail->Send()) ? true : false; //Success   
  215.  }   
  216.   
  217.  //PHPMailer,备用   
  218.  function sendPhpMail($to$subject$message$from$fromname$ishtml=0, $FILE=''$cc='',$bcc=''$charset='gbk'$method='MAIL')   
  219.  {   
  220.   require "../inc/class.phpmailer.php";   
  221.   $mail = new PHPMailer();   
  222.   
  223.   //发送邮件的方式   
  224.   switch($method)   
  225.   {   
  226.    case "SMTP":   
  227.     $mail->isSMTP();   
  228.     $mail->Host     = 'smtp.163.com';   
  229.     $mail->SMTPAuth = true;   
  230.     $mail->Username = '163邮箱帐号';   
  231.     $mail->Password = '163邮箱密码';   
  232.     break;   
  233.    case "MAIL":   
  234.     $mail->isMail();   
  235.     break;   
  236.    case "SENDMAIL":   
  237.     $mail->isSendmail();   
  238.     break;   
  239.    case "QMAIL":   
  240.     $mail->isQmail();   
  241.     break;   
  242.   }   
  243.   $mail->AddReplyTo($from$fromname);//回复地址   
  244.   $mail->SetFrom($from$fromname);//发送人E-mail地址,姓名   
  245.   //接收E-mail地址   
  246.   foreach(explode(','$toas $touser)    
  247.   {   
  248.    $mail->AddAddress(trim($touser));   
  249.   }   
  250.   //抄送E-mail地址   
  251.   if($cc <> '')   
  252.   {   
  253.    foreach(explode(','$ccas $touser)    
  254.    {   
  255.     $mail->AddCC(trim($touser));   
  256.    }   
  257.   }   
  258.   //暗送E-mail地址   
  259.   if($bcc <> '')   
  260.   {   
  261.    foreach(explode(','$bccas $touser)    
  262.    {   
  263.     $mail->AddBCC(trim($touser));   
  264.    }   
  265.   }   
  266.   //标题   
  267.   //$mail->Subject  =  stripslashes($subject);   
  268.   $mail->Subject  =  str_replace("\r"''str_replace("\n"''$subject));//把换行和回车去掉   
  269.   
  270.   //正文   
  271.   $message = eregi_replace("[\]",'',$message);   
  272.   if($ishtml == 1)   
  273.   {   
  274.    $mail->MsgHTML($message);   
  275.   }   
  276.   else  
  277.   {   
  278.    $mail->IsHTML(false);   
  279.   }   
  280.   
  281.   $mail->CharSet = $charset;   
  282.   //$mail->Body    = stripslashes($message);       
  283.   $mail->AltBody    = "Html内容不能正常显示。请使用能够显示html邮件内容的软件查看此邮件。"// optional, comment out and test   
  284.   //todo: 发送附件   
  285.   //$mail->AddAttachment("images/phpmailer.gif");   
  286.   return ($mail->Send()) ? true : false;   
  287.  }   
  288. }   
  289. ?>   

 


文章如转载,请注明:转载自:第九街-PHP[http://www.9streets.cn/]
本文链接地址:
发布者:9streets于 2009-11-25 发表 | 我要评论(0) | 阅读次数(2690)

转播到腾讯微博


首页第九街博客由来关于博主联系博主免责条款隐私保护九街项目流程 | 网站地图 | 申请友情链接