亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪(fǎng)問(wèn)板塊 發(fā)新帖
查看: 1864 | 回復(fù): 0
打印 上一主題 下一主題

SMTP郵件發(fā)送類(lèi) - Fmail.class.php [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-07-04 12:08 |只看該作者 |倒序?yàn)g覽
支持群發(fā)。
               
               
                ?php
/*
$svr = array(
  'smtp' => '發(fā)件smtp',
  'user' => '發(fā)件賬號(hào)',
  'pass' => '密碼',
  'host' => '發(fā)件主機(jī)',
); // 其他參數(shù)見(jiàn)Fmail::$svr
$debug = true;
$mail = array(
  'name' => '發(fā)件人名字',
  'from' => '發(fā)件人',
  'to' => '收件人',
  'cc' => '抄送',
  'subject' => "Hello, 這是主題!",
  'cont' => "信件內(nèi)容",
  'cont_type' => Fmail::CONT_TYPE_HTML, // html格式
  'apart' => true, // 隱藏To、Cc和Bcc
); // 其他參數(shù)見(jiàn)Fmail::send($mail)
$fmail = new Fmail($svr, $debug);
if($fmail->send($mail)) echo "發(fā)送成功!\n";
else echo "發(fā)送失。n";
$fmail->close();
*/
class Fmail {
  const CRLF = "\r\n";
  const BUF_LEN = 512;
  const RCPT_NUM = 99; // NOTE: 收件人上限,使用者須注意
  const CONT_TYPE_PLAIN = 1;
  const CONT_TYPE_HTML = 2;
  // TODO: log_
  protected $fp; //socket句柄
  protected $debug = false;  // 是否顯示調(diào)試信息
  protected $svr = array();
  /*'smtp'      // 發(fā)件服務(wù)器的地址
    'port'      // 發(fā)件服務(wù)器的端口一般默認(rèn)為25
    'time_out'            
    'host'      // 用在HELO后面,host / ip
    'user'      // 發(fā)件人帳號(hào)名稱(chēng)(現(xiàn)在基本都要驗(yàn)證的)
    'pass       // 發(fā)件人帳號(hào)密碼
    'x_mailer'  // (PHP/phpversion())
   */
  public function __construct($svr, $debug = false) {
    $this->opSvr($svr);
    $this->opDebug($debug);
    if(!$this->connect()) {
      $this->close();
      die('FAILED!');
    }
  }
  public function opSvr($svr = null) {
    $tmp = $this->svr;
    if($svr) {
      if(empty($svr['host'])) $svr['host'] = gethostbyaddr("localhost");
      if(empty($svr['host'])) $svr['host'] = gethostbyname("localhost");
      if(empty($svr['port'])) $svr['port'] = 25;
      if(empty($svr['time_out'])) $svr['time_out'] = 30;
      if(empty($svr['x_mailer'])) $svr['x_mailer'] = "PHP/".phpversion();
      $this->svr = $svr;
    }
    return $tmp;
  }
  public function opDebug($debug = null) {
    $tmp = $this->debug;
    if(!is_null($debug)) $this->debug = (bool)$debug;
    return $tmp;
  }
  public function connect() {
    $this->close();
    $svr = & $this->svr;
    $this->show_debug("Connect to SMTP server : ".$svr['smtp'], "out");
    $this->fp = fsockopen($svr['smtp'], $svr['port'],
      $errno, $errstr, $svr['time_out']);
    $fp = & $this->fp;
    if(false === $fp) {
      $this->show_debug("Connect failed! ERRNO: $errno ERRSTR: $errstr", "in");
      return false;
    }
    stream_set_blocking($fp, true);
    $msg = fgets($fp, self::BUF_LEN);
    $this->show_debug($msg, "in");
    if(220 !== (int)$msg) return false;
    if(!$this->do_cmd("HELO " . $svr['host'], 250)) return false;
    if(!empty($svr['user'])) {
      $user = base64_encode($this->svr['user']);
      if(!$this->do_cmd("AUTH LOGIN $user", 334)) return false;
      /*if(!$this->do_cmd("AUTH LOGIN ", 334)) return false;
      if(!$this->do_cmd(base64_encode($svr['user']), 334)) return false;*/
      if(!$this->do_cmd(base64_encode($svr['pass']), 235)) return false;
    }
    return true;
  }
  public function close() {
    if($this->fp) {
      $this->do_cmd("QUIT", 250);
      fclose($this->fp);
    }
    unset($this->fp);
  }
  /* $mail = array();
    'name'(sender), 'from', 'to'(str|array), 'cc'(str|array), 'bcc'(str|array),
    'subject', 'cont', 'charset'(utf-8), 'cont_type'(0), //'cont_encode'(''),
    'no_reply'(mail), 'reply'(msg-id), 'xheaders', 'apart'(false),
   */
  public function send($mail) {
    $fp = & $this->fp;
    $svr = & $this->svr;
    $cs = (empty($mail['charset']) ? 'utf-8' : $mail['charset']);
    $CRLF = self::CRLF;
    if(!$this->do_cmd("MAIL FROM: ", 250)) return false;
    list($msec, $sec) = explode(" ", microtime());
    $header = "Message-ID:  . date("YmdHis", $sec) . "." .
      ($msec*1000000) . ".{$mail['from']}>$CRLF";
    // "Message-Id: $CRLF";
    $header .= "Date: " . date("r", $sec) . $CRLF;
    $header .= "Mime-Version: 1.0$CRLF";
    $header .= "X-Mailer: {$svr['x_mailer']} $CRLF";
    $header .= "From: =?$cs?B?" . base64_encode($mail['name']) . "?=$CRLF";
    // "From: $from\r\n";
    if(!empty($mail['no_reply'])) {
      $header .= "Return-Path: $CRLF";
      $header .= "Return-To: $CRLF";
    } elseif(!empty($mail['reply'])) {
      $header .= "In-Reply-To: $CRLF";
      $header .= "References: $CRLF";
    }
    $subject = str_replace("\n", ' ', $mail['subject']);
    $header .= "Subject: =?$cs?B?" . base64_encode($mail['subject']) . "?=$CRLF";
    // 注意前后位置,某些不可由其更改的header最好置于其后
    if(!empty($mail['xheaders'])) $header .= $mail['xheaders'];
    if(isset($mail['cont_type'])) {
      if(self::CONT_TYPE_PLAIN == $mail['cont_type'])
        $header .= "Content-Type: text/plain; charset=$cs$CRLF";
      else
        $header .= "Content-Type: text/html; charset=$cs$CRLF";
    }
    if(is_string($mail['to'])) $mail['to'] = array($mail['to']);
    if(!isset($mail['cc'])) $mail['cc'] = array();
    elseif(is_string($mail['cc'])) $mail['cc'] = array($mail['cc']);
    if(!isset($mail['bcc'])) $mail['bcc'] = array();
    elseif(is_string($mail['bcc'])) $mail['bcc'] = array($mail['bcc']);
    $tos = array_merge($mail['to'], $mail['cc'], $mail['bcc']);
    foreach($tos as $to) {
      $this->do_cmd("RCPT TO: ", 250); // TODO: 記錄失敗的
    }
    if(!$this->do_cmd("DATA", 354)) return false;
    if(empty($mail['apart'])) {
      // $header .= "To: =?$cs?B?" . base64_encode($toname) . "?=$CRLF";
      $header .= "To: " . implode(',', $mail['to']) . "$CRLF";
      if($mail['cc'])
        $header .= "Cc: " . implode(',', $mail['cc']) . "$CRLF";
    }
    $header .= "Content-Transfer-Encoding: base64$CRLF"; // TODO: 7bit 8bit
    $msg = $header . $CRLF . base64_encode($mail['cont']) . "$CRLF.$CRLF";
    $this->show_debug($msg, "out");
    $ret = fwrite($fp, $msg);
    return $ret !== false;
  }
  protected function show_debug($msg, $inout) {
    if($this->debug) {
      $m = ($inout == "in" ? ' : '>> '); // >>響應(yīng)
      $msg = nl2br($msg . self::CRLF);
      echo "${m}${msg}";
    }
  }
  protected function do_cmd($cmd, $code) {
    $this->show_debug($cmd . self::CRLF, "out");
    fwrite($this->fp, $cmd . self::CRLF);
    // 處理多行響應(yīng)信息
    $msg = "";
    while($rt = fgets($this->fp, self::BUF_LEN)) {
      $msg .= $rt;
      if($rt{3} !== "-") break;
    }
    // $msg = fgets($this->fp, self::BUF_LEN);
    $this->show_debug($msg, "in");
    return $code === (int)$msg;
  }
}
?>

       
        文件:Fmail.class.zip
        大小:2KB
        下載:
下載
       


本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u1/57558/showart_1072707.html
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP