文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Mail 邮件发送类

Mail 邮件发送类

时间:2005-12-21  来源:xrsf

从PHPSo MVC 中挖出来的邮件发送类
setCharset("gb2312");
$mail->setTo("收件人1 ,收件人2 "); //收件人
//$mail->setCC("
[email protected]
"); //抄送
//$mail-> setBCC("
[[email protected],[email protected]][email protected],[email protected][/email]
"); //秘密抄送
$mail->setFrom("Monkey ][email protected]>");//[/email]
发件人
$mail->setSubject("email 标题") ; //主题
$mail->setText("文本格式") ;//发送文本格式也可以是变量
//$mail->setHTML("主题HTML格式") ;//发送html格式也可以是变量
$mail->setAttachments("c:/www/phpsomvc/_sample/5.htm|附件1,c:/www/phpsomvc/readme.txt|附件2") ;
//添加附件,需表明路径,多附件用逗号分割,文件名“|”后为自定义文件名
$mail->send(); //发送邮件
*/
class Mail
{
/**
* Standard email parameters
*
* This is where we keep the most important stuff like who to send the mail to etc.
*
* @var fromEmail
* @var toEmail
* @var subject
* @var headers
* @var message
* @var success
*/
var $fromEmail = '';
var $toEmail = '';
var $subject = '';
var $headers = '';
var $message = '';
var $success = false;
/**
* Constructor
*
* @param string Destination Email Address
* @param string Email Subject
* @param string Email Message Body
* @param string Extra Email Headers
* @param string Webmaster (From) Email Address
* @param boolean Require '-f' parameter to sendmail?
*/
function SendMail($toemail, $subject, $message, $headers, $fromemail, $minusf = false)
{
  if ($minusf)
  {
   $this->success = @mail($toemail, $subject, $message, trim($headers), "-f $fromemail");
  }
  else
  {
   $this->success = @mail($toemail, $subject, $message, trim($headers));
  }
  return $this->success;
}
}
/**
* SMTP Mail Sending Object
*
* This class sends email from vBulletin using an SMTP wrapper
*
* @package   vBulletin
* @version  $Revision: 1.5 $
* @date   $Date: 2004/07/04 20:52:01 $
* @copyright  
http://www.vbulletin.com/license.html
*
*/
class Smtp extends Mail
{
/**
* SMTP email parameters
*
* Variables used to work with the SMTP protocol.
*
* @var    smtpHost
* @var    smtpPort
* @var    smtpUser
* @var    smtpPass
* @var    smtpSocket
*/
var $smtpHost = "";
var $smtpPort = 25;
var $smtpUser = "";
var $smtpPass = "";
var $smtpSocket = null;
/**
* Constructor
*
* @param string Destination Email Address
* @param string Email Subject
* @param string Email Message Body
* @param string Extra Email Headers
* @param string Webmaster (From) Email Address
* @param boolean Require '-f' parameter to sendmail?
*/
function SendMail($toemail, $subject, $message, $headers, $fromemail, $minusf = false)
{
  // this class doesn't handle BCC or CC at the moment
  $this->toEmail = $toemail;
  $this->subject = $subject;
  $this->message = $message;
  $this->headers = trim($headers);
  $matches = array();
  preg_match('#From: ".*" #siU', $this->headers, $matches);
  if (!empty($matches))
  {
   $this->fromEmail = "";
  }
  else
  {
   $this->fromEmail = "";
  }
  $this->success = $this->send();
  return $this->success;
}
/**
* Sends instruction to SMTP server
*
* @param string Message to be sent to server
* @param mixed Message code expected to be returned or false if non expected
*
* @return boolean Returns false on error
*/
function sendMessage($msg, $expectedResult = false)
{
  if ($msg !== false && !empty($msg))
  {
   fputs($this->smtpSocket, $msg . "
");
  }
  if ($expectedResult !== false)
  {
   $result = '';
   while ($line = fgets($this->smtpSocket, 1024))
   {
    $result .= $line;
    if (substr($result, 3, 1) == ' ')
    {
     break;
    }
   }
   return (intval(substr($result, 0, 3)) == $expectedResult);
  }
  return true;
}
/**
* Triggers PHP warning on error
*
* @param string Error message to be shown
*
* @return boolean Always returns false (error)
*/
function errorMessage($msg)
{
  echo $msg;exit;
  return false;
}
/**
* Attempts to send email based on parameters passed into the constructor
*
* @return boolean Returns false on error
*/
function send()
{
  $this->smtpSocket = fsockopen($this->smtpHost, intval($this->smtpPort), $errno, $errstr, 30);
  if ($this->smtpSocket)
  {
   if (!$this->sendMessage(false, 220))
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   if (!$this->sendMessage("HELO " . $this->smtpHost, 250))
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   if ($this->smtpUser AND $this->smtpPass)
   {
    if ($this->sendMessage("AUTH LOGIN", 334))
    {
     if (!$this->sendMessage(base64_encode($this->smtpUser), 334) OR !$this->sendMessage(base64_encode($this->smtpPass), 235))
     {
      return $this->errorMessage("Authorization to the SMTP server failed");
     }
    }
   }
   if (!$this->sendMessage("MAIL FROM:" . $this->fromEmail, 250))
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   // we could have multiple addresses since a few people might expect this to be the same as PHP
   $addresses = explode(',', $this->toEmail);
   foreach ($addresses AS $address)
   {
    if (!$this->sendMessage("RCPT TO:", 250))
    {
     return $this->errorMessage("Unexpected response from SMTP server");
    }
   }
   if ($this->sendMessage("DATA", 354))
   {
    $this->sendMessage("To: " . $this->toEmail, false);
    $this->sendMessage("Subject: " . $this->subject, false);
    $this->sendMessage($this->headers, false);
    $this->sendMessage($this->message, false);
   }
   else
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   if (!$this->sendMessage(".", 250))
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   if (!$this->sendMessage("QUIT", 221))
   {
    return $this->errorMessage("Unexpected response from SMTP server");
   }
   fclose($this->smtpSocket);
   return true;
  }
  else
  {
   return $this->errorMessage("Unable to connect to SMTP server");
  }
}
}
class email {
//---设置全局变量
var $mailTo = ""; // 收件人
var $mailCC = ""; // 抄送
var $mailBCC = ""; // 秘密抄送
var $mailFrom = ""; // 发件人
var $mailSubject = ""; // 主题
var $mailText = ""; // 文本格式的信件主体
var $mailHTML = ""; // html格式的信件主体
var $main_charset = "US-ASCII";  // 信件主体的编码
var $mailAttachments = ""; // 附件
var $send;
var $error = false; // 错误
function Email($method,$host='',$user='',$pass='',$post=25,$socket=null) {
if ($method=='mail') $this->send = new Mail();
if ($method=='smtp') {
  $this->send = new Smtp();
  $this->send->smtpHost = $host;
  $this->send->smtpPort = $post;
  $this->send->smtpUser = $user;
  $this->send->smtpPass = $pass;
  $this->send->smtpSocket = $socket;
}
}
/**
* 函数 setCharset($charset) 设置邮件的默认字符集
* @parm string $charset 邮件的字符集,默认为US-ASCII
*/
function setCharset($charset) {
$this->main_charset=$charset;
}
/**
* 函数 setTo($inAddress) 设置邮件的地址
* @parm string $inAddress 为包涵一个或多个字串,email地址变量,使用逗号来分割多个邮件地址
* @return 默认返回值为true
*/
function setTo($inAddress){
//--用explode()函数根据”,”对邮件地址进行分割
$addressArray = explode( ",",$inAddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckEmail($addressArray[$i])==false) {$this->error=true;return false;}
}
//--所有合法的email地址存入数组中
$this->mailTo = implode($addressArray, ",");
return true;
}
/**
* 函数 setCC($inAddress) 设置抄送人邮件地址
* @parm string $inAddress 为包涵一个或多个邮件地址的字串,email地址变量,使用逗号来分割多个邮件地址
* @return 默认返回值为true
*/
function setCC($inAddress){
//--用explode()函数根据”,”对邮件地址进行分割
$addressArray = explode( ",",$inAddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckEmail($addressArray[$i])==false) {$this->error=true;return false;}
}
//--所有合法的email地址存入数组中
$this->mailCC = implode($addressArray, ",");
return true;
}
/**
* 函数 setBCC($inAddress) 设置秘密抄送地址
* @parm string $inAddress 为包涵一个或多个邮件地址的字串,email地址变量,使用逗号来分割多个邮件地址
* @return 默认返回值为 true
*/
function setBCC($inAddress){
//--用explode()函数根据”,”对邮件地址进行分割
$addressArray = explode( ",",$inAddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckEmail($addressArray[$i])==false) {$this->error=true;return false;}
}
//--所有合法的email地址存入数组中
$this->mailBCC = implode($addressArray, ",");
return true;
}
/**
* 函数 setFrom($inAddress) 设置发件人地址
* @parm string $inAddress 为包涵邮件地址的字串
* @return 默认返回值为true
*/
function setFrom($inAddress){
if($this->checkEmail($inAddress)){
  $this->mailFrom = $inAddress;
  return true;
} $this->error=true;return false;
}
/**
* 函数 setSubject($inSubject) 设置邮件主题
* @parm string $inSubject 为主题字串
* @return 默认返回的是true
*/
function setSubject($inSubject){
if(strlen(trim($inSubject)) > 0){
  $this->mailSubject = ereg_replace( "
", "",$inSubject);
  $this->mailSubject = "=?".$this->main_charset."?B?".base64_encode($this->mailSubject)."?=";
  return true;
} $this->error=true;return false;
}
/**
* 函数 setText($inText) 设置文本格式的邮件主体
* @parm string $inText 为文本内容
* @return 默认返回值为true
*/
function setText($inText){
if(strlen(trim($inText)) > 0){
  $this->mailText = $inText;
  return true;
} $this->error=true;return false;
}
/**
* 函数 setHTML($inHTML) 设置html格式的邮件主体
* @parm string $inHTML 为html格式内容
* @return 默认返回值为true
*/
function setHTML($inHTML){
if(strlen(trim($inHTML)) > 0){
  $this->mailHTML = $inHTML;
  return true;
} $this->error=true;return false;
}
/**
* 函数 setAttachments($inAttachments) 设置邮件的附件
* @parm string $inAttachments 为一个包涵目录的字串,也可以包涵多个文件用逗号进行分割
               文件名"|"后的字串可以为附件自定义文件名,用于显示在邮件中
* @return 默认返回值为true
*/
function setAttachments($inAttachments){
$inAttachmentsArray = explode( ",",$inAttachments);
for($inAttachments='',$i=0;$imailAttachments = $inAttachmentsArray_new;
  return true;
} $this->error=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) 读取临时文件并且替换无用的信息
* @parm string $inFileLocation 用于定位文件的目录
* @parm string $inHash 由于存储临时的值
* @parm string $inFormat 由于放置邮件主体 TEXT or HTML
*/
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; $isetText($templateLineOut));
else if( strtoupper($inFormat)== "HTML" )
  return($this->setHTML($templateLineOut));
} $this->error=true;return false;
}
/**
* 函数 getRandomBoundary($offset) 返回一个随机的边界值
* @parm integer $offset 为整数 – 用于多管道的调用
* @return 返回一个md5()编码的字串
*/
function getRandomBoundary($offset = 0){
//--随机数生成
srand(time()+$offset);
//--返回 md5 编码的32位 字符长度的字串
return ( "--PHPSoMVC_".(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=" .$this->main_charset. "
";
$outTextHeader .= "Content-Transfer-Encoding: 7BIT
";
$outTextHeader .= $this->mailText."
";
return $outTextHeader;
}
/**
函数formatHTMLHeader()把邮件主体内容加上html的文件头
*/
function formatHTMLHeader(){
$outHTMLHeader = "";
$outHTMLHeader .= "Content-Type: text/html;charset=" .$this->main_charset. "
";
$outHTMLHeader .= "Content-Transfer-Encoding: base64
";
$outHTMLHeader .= chunk_split(base64_encode($this->mailHTML));
return $outHTMLHeader;
}
/**
函数 formatAttachmentHeader($inFileLocation) 把邮件中的附件标识出来
*/
function formatAttachmentHeader($inFileLocation){
$Attach=explode("|",$inFileLocation);
if ($Attach[1]=='') $Attach[1]=basename($Attach[0]);
$outAttachmentHeader = "";
//--用上面的函数getContentType($inFileLocation)得出附件类型
$contentType = $this->getContentType($Attach[0]);
$outAttachmentHeader .= "Content-Type: ".$contentType. ";
";
$outAttachmentHeader .= ' name="'.$Attach[1]. '"'. "
";
$outAttachmentHeader .= "Content-Transfer-Encoding: base64
";
$outAttachmentHeader .= "Content-Disposition: attachment;
";
$outAttachmentHeader .= ' filename="'.$Attach[1]. '"'. "
";
$otherFile = fopen($Attach[0], "r");$FileContent='';
while(!feof($otherFile)){
  $FileContent .= fgets($otherFile,1000);
}
$outAttachmentHeader .= chunk_split(base64_encode($FileContent));
  return $outAttachmentHeader;
}
/**
函数 send()用于发送邮件,发送成功返回值为true
如果前面邮件资料准备阶段出现错误,则直接返回false
*/
function send(){
if ($this->error) return false;
//--设置邮件头为空
$mailHeader = "";
//--添加抄送人
if($this->mailCC != "") $mailHeader .= "CC: ".$this->mailCC. "
";
//--添加秘密抄送人
if($this->mailBCC != "") $mailHeader .= "BCC: ".$this->mailBCC. "
";
//--添加发件人
if($this->mailFrom != "") $mailHeader .= "FROM: ".$this->mailFrom. "
";
//---------------------------邮件格式------------------------------
if($this->mailText != "" || $this->mailHTML != "" || $this->mailAttachments != ""){
  $bodyBoundary = $this->getRandomBoundary();
  //--设置 MIME-版本
  $mailHeader .= "MIME-Version: 1.0
";
  $mailHeader .= "Content-Type: multipart/mixed;
";
  $mailHeader .= ' boundary="'.$bodyBoundary. '"';
  $mailHeader .= "
";
  if ($this->mailText!='') {
   $mailHeader .= "This is a multi-part message in MIME format.
";
   $textHeader = $this->formatTextHeader();
   $mailHeader .= "--".$bodyBoundary. "
";
   $mailHeader .= $textHeader;
  }else if ($this->mailHTML!='') {
   $htmlHeader = $this->formatHTMLHeader();
   $mailHeader .= "--".$bodyBoundary. "
";
   $mailHeader .= $htmlHeader;
  }
  if ($this->mailAttachments!='') {
   $attachmentArray = $this->mailAttachments;
   //--根据附件的个数进行循环
   for($i=0;$iformatAttachmentHeader($attachmentArray[$i]);
   }
  }//$mailHeader .= "--".$bodyBoundary. "
";
  return $this->send->SendMail($this->mailTo,$this->mailSubject, "",$mailHeader,$this->mailFrom);
} return false;
}
}
?>


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载