澳门皇冠金沙网站-澳门皇冠844网站

热门关键词: 澳门皇冠金沙网站,澳门皇冠844网站

php下使用SMTP发邮件的代码,发送邮件代码

近来三个连串供给用到SMTP发送邮件,此前的库类一纸空文了,又不欣赏安装pear可能使用pear的net/smtp类,感觉太复杂了。就直接从discuz中抽出出中央稍微修改了下。
   从事商业业事务分析英特网,查找到SMTP协议的指令和应对,SMTP协议在出殡和埋葬SMTP和接受SMTP之间的对话是靠发送SMTP的SMTP命令和选取SMTP反馈的回答来达成的。常用的吩咐如下:
   HELLO<domain><CTiggoLF>识别发送方到收到SMTP的三个HELLO命令
   MAIL FROM:<reverse-path><C奥迪Q7LF><reverse-path>为发送者地址。此命令告诉接收方八个新邮件发送的早先,并对全数的景况和缓冲区进行初步化。此命令开端三个邮件传输管理,最终大功告成将邮件数量传送到叁个或七个信箱中。
   RCPT TO:<forward-path><C本田CR-VLF><forward-path>标记各种邮件接收者的地方  
   DATA<CRLF>
       接收SMTP将把未来的一举一动作为邮件数量去管理,以<C巴博斯 CL级LF>.<CSportageLF>标志数据的尾声。
   REST<C宝马X5LF>退出/重新恢复生机设置当前的邮件传输
   NOOP<C途胜LF>必要选择SMTP仅做OK应答。(用于测量试验)
   QUIT<C奥德赛LF>需求选拔SMTP重临五个OK应答并关闭传输。
   V福特ExplorerFY<string><CEnclaveLF>验证钦赐的邮箱是不是留存,由于安全因素,服务器多禁止此命令。
   EXPN<string><C景逸SUVLF>验证给定的信箱列表是或不是存在,扩大邮箱列表,也常禁止选用。
   HELP<C昂科威LF>查询服务器援救什么命令

本款php发送邮件代码他选用了php自由的邮件发送函数mail进行邮件发送的,他会利用两种试一是决断mail函数是或不是可用,假若行就动用它来发送不然fsockopen来操作。
*/

用的mail()函数,以为不佳用,何况以为那些邮件地址不能够太多,叁次copy了100个也许会出难题,上边提供一款能够界定发送邮件php类函数。
*/

   注:<COdysseyLF>为回车、换行,ASCII码分别为13、10(十进制)。

 代码如下

 代码如下

   其他,能够在command下,使用telnet来实行简易的手工业使用SMTP。
  比如:

class email
{
 function email()
 {
  $this->__construct();
 }

class sendmail{
    function __construct(){
        $this->set();
        $this->auth = 1;
    }

telnet smtp.263.net 25 
Trying 211.150.96.25... 
Connected to smtp.263.net. 
Escape character is '^]'. 
220 Welcome to coremail System(With Anti-Spam) 2.1 for 263(040326) 
HELO weiqiong@cctk.net 
250 smtp.263.net 
mail from:weiqiong@cctk.net 
250 Ok 
rcpt to:g2_t1@263.net 
250 Ok 
data 
354 End data with <CR><LF>.<CR><LF> 
haha 

250 Ok: queued as B9E452FF3E 
quit 
221 Bye 
Connection closed by foreign host. 

 function __construct()
    {
  @define('charset','gbk');
  $this->set(mail_server, mail_port, mail_user, mail_pwd,mail_type);
  $this->auth = 1;
 }

    function set($server=yj_sysmail_smtp,$user="",$password=yj_sysmail_pass,$port=yj_sysmailport,$type=1,$mailusername=0){
        $user=($user=="")?substr(yj_sysmail,0,stripos(yj_sysmail,"@")):$user;

   在此基础上就足以写出二个简便的SMTP类了。

 function set($server, $port, $user, $password, $type = 1, $delimiter = 1, $mailusername = 0)
 {
  $this->type = $type;
  $this->server = $server;
  $this->port = $port;
  $this->user = $user;
  $this->password = $password;
        $this->delimiter = $delimiter == 1 ? "rn" : ($delimiter == 2 ? "r" : "n");
  $this->mailusername = $mailusername;
 }
 
 function send($email_to, $email_subject, $email_message, $email_from = '')
 {
  global $dircms;
  $email_to=iconv("utf-8", "gbk", $email_to);
  $email_subject=iconv("utf-8", "gbk", $email_subject);
  $email_message=iconv("utf-8", "gbk", $email_message);
  $email_subject = '=?'.charset.'?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
  $email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
  $adminemail = $this->type == 1 ? $dircms['mail_user'] : $dircms['mail_user'];
  $email_from = $email_from == '' ? '=?'.charset.'?b?'.base64_encode($dircms['site_name'])."?= <$adminemail>" : (preg_match('/^(. ?) <(. ?)>$/',$email_from, $from) ? '=?'.charset.'?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
  $emails = explode(',', $email_to);
  foreach($emails as $touser)
  {
   $tousers[] = preg_match('/^(. ?) <(. ?)>$/',$touser, $to) ? ($this->mailusername ? '=?'.charset.'?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
  }
  $email_to = implode(',', $tousers);
  $headers = "from: $email_from{$this->delimiter}x-priority: 3{$this->delimiter}x-mailer: dircms {$this->delimiter}mime-version: 1.0{$this->delimiter}content-type: text/html; charset=".charset."{$this->delimiter}";
  if($this->type == 1)
  {
   return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
  }
  elseif($this->type == 2)
  {
   return @mail($email_to, $email_subject, $email_message, $headers);
  }
  else
  {
   ini_set('smtp', $this->server);
   ini_set('smtp_port', $this->port);
   ini_set('sendmail_from', $email_from);
   return @mail($email_to, $email_subject, $email_message, $headers);
  }
 }

        $this->type = $type;
        $this->server = $server;
        $this->port = $port;
        $this->user = $user;
        $this->password = $password;
        $this->mailusername = $mailusername;
    }

<? 
class stmp{ 

 function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = '')
 {
  global $dircms;
  if(!$fp = fsockopen($this->server, $this->port, $errno, $errstr, 10))
  {
   $this->errorlog('smtp', "($this->server:$this->port) connect

    function send($email_to, $email_subject, $email_message, $email_from = ''){
        $email_subject = '=?utf-8?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
        $email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
        $email_from = $email_from == '' ? '=?utf-8?b?'.base64_encode(yj_sysname)."?= <".yj_sysmail.">" : (preg_match('/^(. ?) <(. ?)>$/',$email_from, $from) ? '=?utf-8?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
        $emails = explode(',', $email_to);
        foreach($emails as $touser){
            $tousers[] = preg_match('/^(. ?) <(. ?)>$/',$touser, $to) ? ($this->mailusername ? '=?utf-8?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
        }
        $email_to = implode(',', $tousers);
        $headers = "mime-version: 1.0rnto: {$email_to}rnfrom: {$email_from}<{$email_from}>rnx-priority: 3rnx-mailer: eglive rndate: ".date("r")."rncontent-type: text/html; charset=utf-8rn";

    private $mailcfg=array(); 
    private $error_msg=''; 

  • unable to connect to the smtp server", 0);
       return false;
      }
      stream_set_blocking($fp, true);
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != '220')
      {
       $this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
       return false;
      }
      fputs($fp, "ehlo dircmsrn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250)
      {
       $this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
       return false;
      }
      while(1)
      {
       if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage))
       {
        break;
       }
       $lastmessage = fgets($fp, 512);
      }
      fputs($fp, "auth loginrn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 334)
      {
       $this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
       return false;
      }
      fputs($fp, base64_encode($this->user)."rn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 334)
      {
       $this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
       return false;
      }
      fputs($fp, base64_encode($this->password)."rn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 235)
      {
       $this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
       return false;
      }
      fputs($fp, "mail from: <".preg_replace("/.*<(. ?)>.*/", "", $email_from).">rn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 250)
      {
       fputs($fp, "mail from: <".preg_replace("/.*<(. ?)>.*/", "", $email_from).">rn");
       $lastmessage = fgets($fp, 512);
       if(substr($lastmessage, 0, 3) != 250)
       {
        $this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
        return false;
       }
      }
      $email_tos = array();
      $emails = explode(',', $email_to);
      foreach($emails as $touser)
      {
       $touser = trim($touser);
       if($touser)
       {
        fputs($fp, "rcpt to: <".preg_replace("/.*<(. ?)>.*/", "", $touser).">rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250)
        {
         fputs($fp, "rcpt to: <".preg_replace("/.*<(. ?)>.*/", "", $touser).">rn");
         $lastmessage = fgets($fp, 512);
         $this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
         return false;
        }
       }
      }
      fputs($fp, "datarn");
      $lastmessage = fgets($fp, 512);
      if(substr($lastmessage, 0, 3) != 354)
      {
       $this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
      }
      $headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">{$this->delimiter}";
      fputs($fp, "date: ".gmdate('r')."rn");
      fputs($fp, "to: ".$email_to."rn");
      fputs($fp, "subject: ".$email_subject."rn");
      fputs($fp, $headers."rn");
      fputs($fp, "rnrn");
      fputs($fp, "$email_messagern.rn");
      fputs($fp, "quitrn");
      return true;
     }

        if($this->type == 1){
            return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
        }elseif($this->type == 2){
            return @mail($email_to, $email_subject, $email_message, $headers);
        }else{
            ini_set('smtp', $this->server);
            ini_set('smtp_port', $this->port);
            ini_set('sendmail_from', $email_from);
            return @mail($email_to, $email_subject, $email_message, $headers);
        }
    }

    function __construct($mailcfg){ 

 function errorlog($type, $message, $is)
 {
  $this->error[] = array($type, $message, $is);
 }
}

    function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = ''){
        if(!$fp = @fsockopen($this->server, $this->port, $errno, $errstr, 10)){
            $this->errorlog('smtp', "($this->server:$this->port) connect - unable to connect to the smtp server", 0);
            return false;
        }
        stream_set_blocking($fp, true);
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != '220'){
            $this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
            return false;
        }
        fputs($fp, ($this->auth ? 'ehlo' : 'helo')." phpcmsrn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250){
            $this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
            return false;
        }
        while(1){
            if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)){
                break;
            }
            $lastmessage = fgets($fp, 512);
        }
        fputs($fp, "auth loginrn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334){
            $this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
            return false;
        }
        fputs($fp, base64_encode($this->user)."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334){
            $this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
            return false;
        }
        fputs($fp, base64_encode($this->password)."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 235){
            $this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
            return false;
        }
        fputs($fp, "mail from: <".preg_replace("/.*<(. ?)>.*/", "", $email_from).">rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250){
            fputs($fp, "mail from: <".preg_replace("/.*<(. ?)>.*/", "", $email_from).">rn");
            $lastmessage = fgets($fp, 512);
            if(substr($lastmessage, 0, 3) != 250){
                $this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
                return false;
            }
        }
        $email_tos = array();
        $emails = explode(',', $email_to);
        foreach($emails as $touser){
            $touser = trim($touser);
            if($touser){
                fputs($fp, "rcpt to: <".preg_replace("/.*<(. ?)>.*/", "", $touser).">rn");
                $lastmessage = fgets($fp, 512);
                if(substr($lastmessage, 0, 3) != 250){
                    fputs($fp, "rcpt to: <".preg_replace("/.*<(. ?)>.*/", "", $touser).">rn");
                    $lastmessage = fgets($fp, 512);
                    $this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
                    return false;
                }
            }
        }
        fputs($fp, "datarn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 354){
            $this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
        }
        $headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">rn";
        fputs($fp, "date: ".gmdate('r')."rn");
        fputs($fp, "to: ".$email_to."rn");
        fputs($fp, "subject: ".$email_subject."rn");
        fputs($fp, $headers."rn");
        fputs($fp, "rnrn");
        fputs($fp, "$email_messagern.rn");
        $lastmessage = fgets($fp, 512);
        fputs($fp, "quitrn");
        return true;
    }

        $this->mailcfg=$mailcfg; 

    function errorlog($type, $message, $is){
        $this->error[] = array($type, $message, $is);
    }

本文由澳门皇冠金沙网站发布于编辑程序,转载请注明出处:php下使用SMTP发邮件的代码,发送邮件代码