程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php使用smtp發送支持附件的郵件示例

php使用smtp發送支持附件的郵件示例

編輯:關於PHP編程

輕量級PHP郵件發送,需要有smtp服務器,代碼經過多次實戰使用,現在把代碼分享給大家

復制代碼 代碼如下:
<?php
/*
郵件發送smtp服務
聯結smtp服務器,進行郵件發送,版權所有,不能復制
@author:jackbrown;
@qq: 610269963
@time:2011-8-20;
@version:1.0.3;
*/
class smtp{

 /*郵件用戶名*/
 public $mailUser = MAIL_USER;

 /*郵件密碼*/
 public $mailPwd = MAIL_PWD;

 /*郵件服務器地址*/
 public $server = MAIL_SMTP_HOST;

 /*郵件端口*/
 public $port = MAIL_SMTP_PORT;

 public $timeout = MAIL_TIMEOUT;

 /*郵件編碼*/
 public $charset = MAIL_CHARSET;

 /*郵件發送者email,用於顯示給接收者*/
 public $senderMail = MAIL_SENDER;

 /*發用者名稱*/
 public $senderName = MAIL_SENDER_NAME;

 /*是否使用ssl安全操作*/
 public $useSSL = IN_SSL;

 /*是否顯示錯誤信息*/
 public $showError = MAIL_SHOW_ERR;

 public $needLogin = MAIL_NEED_LOGIN;

 /*附件數組*/
 public $attachMent = array();

 public $failed = false;

 private static $smtpCon;
 private $stop ="\r\n";
 private $status = 0;

 

 public function __construct(){

  if(self::$smtpCon){
   return;
  }

  if($this->mailUser==''){
   $this->error('請配置好郵件登錄用戶名!');
   return false;
  }

  if($this->mailPwd==''){ 
   $this->error('請配置好郵件登錄密碼!');
   return false;
  }

  if($this->server==''){  
   $this->error('請配置好郵服務器地址!');
   return false;
  }

  if(!is_numeric($this->port)){  
   $this->error('請配置好郵服務器端口!');
   return false;
  }

  /*ssl使用**/
  $server = $this->server;
  if($this->useSSL == true){
   $server = "ssl://".$this->server;
  }

  self::$smtpCon = @fsockopen($server, $this->port, $errno, $errstr,10);;

 
  if(!self::$smtpCon){
   $this->error($errno.$errstr);
   return false;
  }

 
  socket_set_timeout(self::$smtpCon,0,250000);

  /*開始郵件指令*/
  $this->getStatus();
  $resp = true;
  $resp = $resp && $this->helo();
  if($this->needLogin == '1'){
   $resp = $resp && $this->login();
  }

  if(!$resp){
   $this->failed = true;
  }

 }

 /*
 發送郵件
 @param string $to 接收郵件地址
 @param string $msg 郵件主要內容
 @title string $title 郵件標題
 */
 public function sendMail($to,$msg,$title=''){

  if($msg=='' ){

   return false;
  }
  if(is_array($to)){

   if($to!=null){
    foreach($to as $k=>$e){

     if(!preg_match('/^[a-z0-9A-Z_-]+@+([a-z0-9A-Z_-]+\.)+[a-z0-9A-Z]{2,3}$/',$e)){

      unset($to[$k]);
     }
    }
   }else{
    return false;
   }

   if($to == null){
    return false;
   }

  }else{

   if(!preg_match('/^[a-z0-9A-Z_-]+@+([a-z0-9A-Z_-]+\.)+[a-z0-9A-Z]{2,3}$/',$to)){

    return false;
   }

  }

  
  if(!self::$smtpCon){
   return false;
  }

  $this->sendSmtpMsg('MAIL FROM:<'.$this->senderMail.'>');

  if(!is_array($to)){     
   $this->sendSmtpMsg('RCPT TO:<'.$to.'>');
  }else{

   foreach($to as $k=>$email){   
    $this->sendSmtpMsg('RCPT TO:<'.$email.'>');
   }
  }

  $this->sendSmtpMsg("DATA");

 
  if($this->status !='354'){
   $this->error('請求發送郵件失敗!');
   $this->failed = true;
   return false;
  }

  $msg  = base64_encode($msg);
  $msg = str_replace($this->stop . '.', $this->stop . '..', $msg);
  $msg    = substr($msg, 0, 1) == '.' ? '.' . $msg : $msg;

  if($this->attachMent!=null){

   $headers = $this->mimeHeader($msg,$to,$title);
   $this->sendSmtpMsg($headers,false);

  }else{

   $headers = $this->mailHeader($to,$title);
   $this->sendSmtpMsg($headers,false);
   $this->sendSmtpMsg('',false);
   $this->sendSmtpMsg($msg,false);
  }
  $this->sendSmtpMsg('.');//發送結束標識符

  if($this->status != '250'){
   $this->failed = true;
   $this->error($this->readSmtpMsg());
   return false;
  }

  return true;
 }

 /*
 關閉郵件連接
 */
 public function close(){

  $this->sendSmtpMsg('Quite');
  @socket_close(self::$smtpCon);
 }

 /*
 添加普通郵件頭信息
 */
 protected function mailHeader($to,$title){
  $headers = array();
  $headers[] = 'Date: '.$this->gmtime('D j M Y H:i:s').' '.date('O');

  if(!is_array($to)){
   $headers[] = 'To: "'.'=?'.$this->charset.'?B?'.base64_encode($this->getMailUser($to)).'?="<'.$to.'>';
  }else{
   foreach($to as $k=>$e){
    $headers[] = 'To: "'.'=?'.$this->charset.'?B?'.base64_encode($this->getMailUser($e)).'?="<'.$e.'>';
   }
  }

  $headers[] = 'From: "=?'.$this->charset.'?B?'.base64_encode($this->senderName).'?="<'.$this->senderMail.'>';
  $headers[] = 'Subject: =?'.$this->charset.'?B?'.base64_encode($title).'?=';
  $headers[] = 'Content-type: text/html; charset='.$this->charset.'; format=flowed';
  $headers[] = 'Content-Transfer-Encoding: base64';

     $headers = str_replace($this->stop . '.', $this->stop . '..', trim(implode($this->stop, $headers)));
  return $headers;
 }

 /*
 帶付件的頭部信息
 */
 protected function mimeHeader($msg,$to,$title){

  if($this->attachMent!=null){

   $headers = array();
   $boundary = '----='.uniqid();
   $headers[] = 'Date: '.$this->gmtime('D j M Y H:i:s').' '.date('O'); 
   if(!is_array($to)){
    $headers[] = 'To: "'.'=?'.$this->charset.'?B?'.base64_encode($this->getMailUser($to)).'?="<'.$to.'>';
   }else{
    foreach($to as $k=>$e){
     $headers[] = 'To: "'.'=?'.$this->charset.'?B?'.base64_encode($this->getMailUser($e)).'?="<'.$e.'>';
    }
   }

   $headers[] = 'From: "=?'.$this->charset.'?B?'.base64_encode($this->senderName).'?="<'.$this->senderMail.'>';
   $headers[] = 'Subject: =?'.$this->charset.'?B?'.base64_encode($title).'?=';
   $headers[] =  'Mime-Version: 1.0';
   $headers[] = 'Content-Type: multipart/mixed;boundary="'.$boundary.'"'.$this->stop;
   $headers[]='--'.$boundary;

   $headers[]='Content-Type: text/html;charset="'.$this->charset.'"';
   $headers[]='Content-Transfer-Encoding: base64'.$this->stop;
   $headers[] = '';
   $headers[]= $msg.$this->stop;  

   foreach($this->attachMent as $k=>$filename){

    $f = @fopen($filename, 'r');
    $mimetype = $this->getMimeType(realpath($filename));
    $mimetype = $mimetype == '' ? 'application/octet-stream' : $mimetype;

    $attachment = @fread($f, filesize($filename));
    $attachment = base64_encode($attachment);
    $attachment = chunk_split($attachment);

    $headers[] = "--" . $boundary;
    $headers[] = "Content-type: ".$mimetype.";name=\"=?".$this->charset."?B?". base64_encode(basename($filename)).'?="' ;
    $headers[] = "Content-disposition: attachment; name=\"=?".$this->charset."?B?". base64_encode(basename($filename)).'?="';
    $headers[] = 'Content-Transfer-Encoding: base64'.$this->stop;
    $headers[] = $attachment.$this->stop;

   

   }
   $headers[] = "--" . $boundary . "--";
   $headers = str_replace($this->stop . '.', $this->stop . '..', trim(implode($this->stop, $headers)));
   return $headers;

  } 
 }

 /*
 獲取返回狀態
 */
 protected function getStatus(){

  $this->status = substr($this->readSmtpMsg(),0,3);
 }

 
 /*
 獲取郵件服務器返回的信息
 @return string 信息字符串
 */
 protected function readSmtpMsg(){

  if(!is_resource(self::$smtpCon)){
   return false;
  }

  $return = '';
  $line   = '';
  while (strpos($return, $this->stop)=== false OR $line{3}!== ' ')
  {
   $line    = fgets(self::$smtpCon, 512);
   $return .= $line;
  }

  return trim($return); 

 }

 /*
 給郵件服務器發給指定命令消息
 */
 protected function sendSmtpMsg($cmd,$chStatus=true){
        if (is_resource(self::$smtpCon))
        {
             fwrite(self::$smtpCon, $cmd . $this->stop, strlen($cmd) + 2);
        }
  if($chStatus == true){
   $this->getStatus();
  }

  return true;
 }

 /*
 郵件時間格式
 */
 protected function gmtime(){

  return (time() - date('Z'));

 }

 /*
 獲取付件的mime類型
 */
 protected function getMimeType($file){

  $mimes = array(
   'chm'=>'application/octet-stream', 'ppt'=>'application/vnd.ms-powerpoint',
   'xls'=>'application/vnd.ms-excel', 'doc'=>'application/msword', 'exe'=>'application/octet-stream',
   'rar'=>'application/octet-stream', 'js'=>"javascrīpt/js", 'css'=>"text/css",
   'hqx'=>"application/mac-binhex40", 'bin'=>"application/octet-stream", 'oda'=>"application/oda", 'pdf'=>"application/pdf",
   'ai'=>"application/postsrcipt", 'eps'=>"application/postsrcipt", 'es'=>"application/postsrcipt", 'rtf'=>"application/rtf",
   'mif'=>"application/x-mif", 'csh'=>"application/x-csh", 'dvi'=>"application/x-dvi", 'hdf'=>"application/x-hdf",
   'nc'=>"application/x-netcdf", 'cdf'=>"application/x-netcdf", 'latex'=>"application/x-latex", 'ts'=>"application/x-troll-ts",
   'src'=>"application/x-wais-source", 'zip'=>"application/zip", 'bcpio'=>"application/x-bcpio", 'cpio'=>"application/x-cpio",
   'gtar'=>"application/x-gtar", 'shar'=>"application/x-shar", 'sv4cpio'=>"application/x-sv4cpio", 'sv4crc'=>"application/x-sv4crc",
   'tar'=>"application/x-tar",'ustar'=>"application/x-ustar",'man'=>"application/x-troff-man", 'sh'=>"application/x-sh",
   'tcl'=>"application/x-tcl", 'tex'=>"application/x-tex", 'texi'=>"application/x-texinfo",'texinfo'=>"application/x-texinfo",
   't'=>"application/x-troff", 'tr'=>"application/x-troff", 'roff'=>"application/x-troff",
   'shar'=>"application/x-shar", 'me'=>"application/x-troll-me", 'ts'=>"application/x-troll-ts",
   'gif'=>"image/gif", 'jpeg'=>"image/pjpeg", 'jpg'=>"image/pjpeg", 'jpe'=>"image/pjpeg", 'ras'=>"image/x-cmu-raster",
   'pbm'=>"image/x-portable-bitmap", 'ppm'=>"image/x-portable-pixmap", 'xbm'=>"image/x-xbitmap", 'xwd'=>"image/x-xwindowdump",
   'ief'=>"image/ief", 'tif'=>"image/tiff", 'tiff'=>"image/tiff", 'pnm'=>"image/x-portable-anymap", 'pgm'=>"image/x-portable-graymap",
   'rgb'=>"image/x-rgb", 'xpm'=>"image/x-xpixmap", 'txt'=>"text/plain", 'c'=>"text/plain", 'cc'=>"text/plain",
   'h'=>"text/plain", 'html'=>"text/html", 'htm'=>"text/html", 'htl'=>"text/html", 'rtx'=>"text/richtext", 'etx'=>"text/x-setext",
   'tsv'=>"text/tab-separated-values", 'mpeg'=>"video/mpeg", 'mpg'=>"video/mpeg", 'mpe'=>"video/mpeg", 'avi'=>"video/x-msvideo",
   'qt'=>"video/quicktime", 'mov'=>"video/quicktime", 'moov'=>"video/quicktime", 'movie'=>"video/x-sgi-movie", 'au'=>"audio/basic",
   'snd'=>"audio/basic", 'wav'=>"audio/x-wav", 'aif'=>"audio/x-aiff", 'aiff'=>"audio/x-aiff", 'aifc'=>"audio/x-aiff",
   'swf'=>"application/x-shockwave-flash", 'myz'=>"application/myz"
  );

  $ext = substr(strrchr($file,'.'),1);
  $type = $mimes[$ext];

 
  unset($mimes);
  return $type;
 }

 /*
 郵件helo命令
 */
 private function helo(){

  if($this->status != '220'){

   $this->error('連接服務器失敗!');
   return false;
  }

  return $this->sendSmtpMsg('HELO '.$this->server);

 }

 
 /*
 登錄
 */
 private function login(){

  if($this->status!='250'){

   $this->error('helo郵件指令失敗!');
   return false;
  }

  $this->sendSmtpMsg('AUTH LOGIN'); 
  if($this->status!='334'){
   $this->error('AUTH LOGIN 郵件指令失敗!');
   return false;
  }

  $this->sendSmtpMsg(base64_encode($this->mailUser)); 
  if($this->status!='334'){
   $this->error('郵件登錄用戶名可能不正確!'.$this->readSmtpMsg());
   return false;
  }

  $this->sendSmtpMsg(base64_encode($this->mailPwd));
  if($this->status !='235'){
   $this->error('郵件登錄密碼可能不正確!');
   return false;
  }

  return true;

 }

 private function getMailUser($to){

  $temp = explode('@',$to);
  return $temp[0];
 }
 /*
 異常報告
 */
 private function error($exception){

  if($this->showError == false){
   file_put_contents('mail_log.txt',$exception,FILE_APPEND);
   return;
  }

  if(class_exists('error') && is_object($GLOBALS['error'])){  
   $GLOBALS['error']->showErrorStr($exception,'javascript:',false);
  }else{
   throw new Exception($exception);
  }
 }

}

// 使用示例
ini_set('memory_limit','128M');
set_time_limit(120);
define('MAIL_SENDER_NAME','楚賢');
define('MAIL_SMTP_HOST','smtp.ym.163.com');
define('MAIL_USER','[email protected]');
define('MAIL_SENDER','[email protected]');
define('MAIL_PWD','xxxx');
define('MAIL_SMTP_PORT',25);
define('IN_SSL',false);
define('MAIL_TIMEOUT',10);
define('MAIL_CHARSET','utf-8');
date_default_timezone_set('PRC');
$m = new smtp();
$msg = "有用戶登錄服務器@".date('Y-m-d H:i:s');
付件
//$m->attachMent = array('hehe.php','common.php');
if($m->sendMail(array('[email protected]'),$msg,'88服務器登錄提示')){
 echo '發送成功!';
}
$m->close();
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved