现在的位置: 首页 > 综合 > 正文

stmp mail

2012年08月05日 ⁄ 综合 ⁄ 共 4868字 ⁄ 字号 评论关闭
<?php
/*
@author sanshi
    QQ:35047205
    Email:sanshi0815@tom.com
    MSN:sanshi0815@tom.com
*/
class sendMail
{
 var $loginName= "";//用户登陆名称
 var $loginPwd = "";//用户登陆密码
 var $smtpHost = '';//smtp服务器名称
 var $smtpPort = '';//smtp的端口
 var $smtpFp   = '';//socket句柄
 var $msg      = array();//消息数组 
 var $welcome  = '欢迎使用叁石stmp发送邮件!';
 var $mailType = "html";
 var $mail     = array(
       'from'   =>'',//发送邮件的人
       'to'     =>'',//接受邮件的人
       'cc'     =>'',
       'replyTo'=>'',
       'subject'=>'',//标题头
       'data'   =>'',//内容
       );
 function sendMail( $loginName='',$loginPwd='',$smtpHost='smtp.tom.com',$smtpPort=25 )
 {
  $this->msg         = array();
  $this->msg[]       = $this->welcome;
  $this->loginName   = $this->checkMail( $loginName,"loginName is not mail" );
  $this->loginPwd    = $loginPwd;
  $this->smtpHost    = $smtpHost;
  $this->smtpPort    = $smtpPort;
  $this->mail['from']=$this->loginName;
  
  if($this->openStmp())
  {
   $this->msg[] = 'stmp server connect success!';
  }else if( empty($this->smtpFp)){
   $this->msg[] = 'smtpFp is null!<br>please check stmp host name!';
   print_r($this->msg);
  }else{
   $this->msg[] = 'stmp server connect failure';
   print_r( $this->msg );
  }
 }
 function checkCommon($com,$no='',$msg='')
 {
  $cod = empty($com)? $this->getServerMsg() : $this->putServerMsg( $com );
  if(!empty($no))
  {
   $this->msg[] = ereg( "^{$no} |250" ,$cod )? $msg : $com .' exec is fauiler!error:'.$cod ;
   if( ! ereg( "^{$no} |250",$cod ) )
   {
    echo $cod;
    print_r($this->msg);
    $this->closeStmp();
    exit(0);
   }
  }else{
   $this->msg[] = $msg;
  }
 }
 function checkMail($allMail,$msg)
 {
  $format='^[a-zA-Z0-9]{3,32}@[a-zA-Z0-9]{3,32}/.[a-zA-Z0-9]{2,5}';
  $mails = explode(',',$allMail);
  foreach ( $mails as $mail )
  {
   if(ereg($format,$mail))
   {
    //return $mail;
   }else{
    $this->msg[] = $msg.'<br>error mail address:'.$mail;
    print_r( $this->msg );
    exit(0);
   }
  }
  return $allMail;
 }
 function init($to,$subject='',$msg='',$mailType=0,$cc='',$replyTo='')
 {
  $this->mailType  = !$mailType?'text':'html';
  $this->mail['to'] = $this->checkMail( $to ,'send mail address error!');
  $this->mail['subject'] = empty($subject)? 'no subject' : $subject;
  $this->mail['data']    = empty($msg)? 'no mail body' : $msg;
  $this->mail['cc']    = empty($cc) ? $cc : $this->checkMail($cc,'cc mail check!');
  $this->mail['replyTo'] =$replyTo;
  $rn = "/r/n";
  //连接
  $this->checkCommon('EHLO '.$this->smtpHost.$rn,'220','longin success!');
  //登陆
  $this->checkCommon("AUTH LOGIN".$rn,'334',"user login start!");
  $this->checkCommon(base64_encode($this->loginName).$rn,'334','user name true!');
  $this->checkCommon(base64_encode($this->loginPwd).$rn,'235',"user password true!");
  //发送头
  $this->checkCommon("MAIL FROM: <".$this->mail['from'].">".$rn , "250","Mail from exec success!");
  $this->checkCommon("RCPT TO: <".$this->mail['to'].">".$rn,'250','rcpt to exec success!');
  $this->checkCommon("DATA ".$rn,'354','data exec success!');
  //处理头
  $this->checkCommon("TO: ".$this->mail['to'].$rn,'','to');
  $this->checkCommon("From: ".$this->mail['subject']."<".$this->mail['from'].">".$rn,'','from');
  $this->checkCommon("Subject: ".$this->mail['subject'].$rn,'','subject');
  
  $headers = "MIME-Version:1.0".$rn;
  $type= $this->mailType=='html'? "text/html":'text/plain';
  $headers.= "Content-Type: {$type}; charset=/"gb2312/"/r/nContent-Transfer-Encoding: base64";
  $headers.= "To: ".$this->mail['to'].$rn;
  $headers.= empty($this->mail['cc'])?'':"Cc: ".$this->mail['cc'].$rn;
  $headers.= empty($this->mail['replyTo'])?'':"Reply-To: ".$this->mail['replyTo'].$rn;
  $headers.= "From: ".$this->mail['subject']."<".$this->mail['from'].">".$rn;
  $headers.= "Subject: ".$this->mail['subject'].$rn;
  $this->checkCommon($headers,'','send header success!');
  
  $this->checkCommon($rn,'','header end');
  
  $this->checkCommon($this->mail['data'].$rn,'','data send');
  
  $this->checkCommon('.'.$rn,'','send mail end!');
  $this->checkCommon("QUIT".$rn,'','exit maill');
  $this->closeStmp();
  $this->msg[] = "<br>发送成功";
  print_r( $this->msg[0] );
  print_r(end($this->msg));
  
 }
 function putServerMsg($msg)
 {
  if( empty( $this->smtpFp ) )
  {
   $this->msg[] = 'smtpFp is null!';
   print_r($this->msg);
   return false;
  }
  fputs( $this->smtpFp , $msg );
  return $this->getServerMsg();
 }
 function getServerMsg()
 {
  if( empty( $this->smtpFp ) )
  {
   $this->msg[] = 'smtpFp is null!';
   print_r($this->msg);
   return false;
  }
  return fgets( $this->smtpFp,512);
 }
 function closeStmp()
 {
  empty($this->smtpFp) ? '':fclose($this->smtpFp);
  $this->msg[] = "close stmp";
 }
 function openStmp()
 {
  if ( empty( $this->smtpHost ) )
  {
   $this->msg[] = 'smtpHost is null' ;
   return false;
  }
  if ( empty( $this->smtpPort ) )
  {
   $this->msg[] = 'smtp port is null';
   return false;
  }
   if ( $fp = fsockopen( $this->smtpHost,$this->smtpPort ) )
   {
    $this->smtpFp = $fp;
    //set_socket_blocking( $this->smtpFp,true );
    return true;
   }
  return false;
 }
}
$m="sanshi0815@tom.com";
$stmpHost="smtp.tom.com";
$mail = new sendMail($m,'1234',$stmpHost,25);
$msg="<FONT COLOR='#FF0066'>内容</FONT><img src='http://www.phpsos.com/img/head.jpg'/>";
$mail->init($m,"标题",$msg,$mailType=1);
?>

抱歉!评论已关闭.