/********************************** PHP MIMESMTP ver 1.0 Powered by Boss_ch, Unigenius soft ware co. Ltd All rights reserved, Copyright 2000; This class uses PHP to send MIME type emails through smtp sock operation, and can send HTML format The text and attachments are encoded in base64 This version is for individual sending. Different from the multi-person version, every time it is sent to a person, it is re-encoded. From the perspective of the user at the receiving end, it is just sent to him. A person's. For multi-person crowd sending, it is only sent once and sent to different people's mailboxes through multiple RCPTTO commands. Instructions: Please set $hostname as the default smtp server that you have permissions or Specify when new and change $charset to your default character set If there are pictures in the HTML text, please use absolute path references "httP://host/path/image.gif"; And connect to the Internet to ensure that the program can read the data information of the image If the Html text is submitted through a form, please use StripSlashes ($html_body) to preprocess the text content first Used in Html For style sheet files, please do not use references such as . Place the style sheet definition directly in the tag.
Please retain this copyright information when reprinting. , Bugs Report: [email]boss_ch@china.com[/email] ************************************ ******/ if(!isset($__smtp_class__)){ $__smtp_class__=1;
class smtp { var $hostname=""; var $port=25; var $connection=0; var $debug=1;
var $timeout=30; var $err_str; var $err_no;
var $autocode=true; var $charset="??????"; var $subject=""; var $body="" ; var $attach=""; var $temp_text_body; var $temp_html_body; var $temp_body_images;
var $bound_begin="=====powered_by_boss_chen_" ; var $bound_end="_046484063883_=====";
Function smtp($server="smtp.china.com",$port=25,$time_out=20) {$this->hostname=$server; $this->port=$port; $this->timeout=$time_out; return true; }
Function outdebug($message) { echo htmlspecialchars($message)." n"; }
function command($command, $return_lenth=1,$return_code='2') { if ($this->connection==0) { $this->err_str="Not connected to any Server, please check network connection"; return false; } if ($this->debug) $this->outdebug(">>> $command" ); if (!fputs($this->connection,"$command rn")) { $this->err_str="Unable to send command".$command; return false; } else { $resp=fgets($this->connection,256); if($this->debug) $this ->outdebug("$resp"); if (substr($resp,0,$return_lenth)!=$return_code) { $this->err_str=$command." command The server returned invalid: ".$resp; return false; } else return true; } }
Function open() { if($this->hostname=="") {$this->err_str="Invalid hostname!!"; return false; }
if ($this->debug) echo "$this->hostname,$this->port,&$err_no, &$err_str, $this->timeout "; if (!$this->connection=fsockopen($this->hostname,$this->port,&$err_no, &$err_str, $this->timeout)) { $this->err_str="Failed to connect to SMTP server, error message: ".$err_str."Error number: ".$err_no; return false; } else { $resp=fgets($this->connection,256); if($this->debug) $this->outdebug("$resp"); if (substr($resp,0,1)!="2") {$this->err_str="The server returned invalid information: ".$resp." Please check whether the SMTP server is correct"; return false; } return true; } }
Function Close() { if($this-> connection!=0) { fclose($this->connection); $this->connection=0; } }
Function Buildhead ($from_name,$to_name,$from_mail,$to_mail,$subject) { if (empty($from_name)) $from_name=$from_mail; if (empty($to_name) ) $to_name=$to_mail; $this->subject="From: =?$this->charset?B?".base64_encode($from_name)."?=<$from_mail>rn"; $this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=<$to_mail>rn"; $subject= ereg_replace("n","",$subject); $this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)."?=rn "; if ($this->debug) echo nl2br(htmlspecialchars($this->subject)); return true; }
Function parse_html_body($html_body=null) { $passed=""; $image_count=0; $this->temp_body_images=array(); while (eregi("<*img([^>]+)src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg)) {
$pos=@strpos($html_body,$reg[0]); $passed.=substr($html_body,0,$pos); $html_body=substr($html_body,$pos+strlen($reg[0])); $image_tag=$reg[2]; $image_att=$reg[1]; $tag_len=strlen($image_tag); if ($image_tag[0]=="'" or $image_tag[0]=='"') $image_tag=substr($image_tag,1); if (substr($image_tag,strlen($imgage_tag)-1,1)=="'" or substr($image_tag,strlen($imgage_tag)-1,1)=='"') $image_tag=substr($image_tag,0,strlen($imgage_tag)-1); //echo $image_tag." "; $cid=md5(uniqid(rand())); $cid=substr($cid,0,15)."@unigenius.com"; $passed.="$end_pos=@strpos($html_body,'>'); $passed.=substr($html_body,0,$end_pos); $html_body=substr($html_body,$end_pos); // 把图片数据读出来保存到一个数据;
$img_file_con=fopen($image_tag,"r"); unset($image_data); while ($tem_buffer=AddSlashes(fread($img_file_con,16777216))) $image_data.=$tem_buffer; fclose($img_file_con); $image_exe_name=substr($image_tag,strrpos($image_tag,'.')+1,3); switch (strtolower($image_exe_name)) { case "jpg": case "jpeg": $content_type="image/jpeg"; break; case "gif": $content_type="image/gif"; break; case "png": $content_type="image/x-png"; break; case "tif": $content_type="image/tif"; break; default: $content_type="image/"; break; }
if (!$hava_att) $this->body.="rnThis is a multi-part message in MIME format.rnrn"; $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."rn"; $this->body.="Content-Type: text/plain;rn"; $this->body.="tcharset="$this->charset"rn"; $this->body.="Content-Transfer-Encoding: base64rn"; $this->body.="rn".chunk_split(base64_encode(StripSlashes($text_body)))."rn"; $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."rn"; $this->body.="Content-Type: text/html;rn"; $this->body.="tcharset="$this->charset"rn"; $this->body.="Content-Transfer-Encoding: base64rn"; $this->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."rn"; $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--rnrn"; $bound_level--; } else { $this->body.="Content-Type: text/html;rn"; $this->body.="tcharset="$this->charset"rn"; $this->body.="Content-Transfer-Encoding: base64rn"; $this->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."rn"; }//正文编码,有或没有text 部分,编成不同的格式。
}// end else } else // If there is no html body, there is only text body { $this->body.="Content-Type: text /plain; tcharset="$this->charset"rn"; $this->body.="Content-Transfer-Encoding: base64rn"; $this->body. ="rn".chunk_split(base64_encode(StripSlashes($text_body)))."rn"; } }// end function default
Function Buildbody($text_body=null ,$html_body=null,$att=null) { $this->body="MIME-Version: 1.0rn"; if (null==$att or (@count($ att)==0)) //If there is no attachment, check the type of text; { $encode_level=0; $this->build_content($encode_level,$text_body,$html_body); }// If there is no attachment, // *********************************** ********************* else //If there is an attachment, { $bound_level=0; $this-> ;body.="Content-Type: multipart/mixed; tboundary=""; $bound_level++;
$this->body.=$this->bound_begin.$bound_level .$this->bound_end.""rnrn"; $this->body.="This is a multi-part message in MIME format.rnrn"; $this->body.= "--".$this->bound_begin.$bound_level.$this->bound_end."rn"; $this->build_content($bound_level,$text_body,$html_body,true);// Incorporate into the text part
$num=count($att); for ($i=0;$i<$num;$i++) { $file_name=$att [$i][name]; $file_source=$att[$i][source]; $file_type=$att[$i][type]; $file_size=$att[$ i][size];
if (file_exists($file_source)) { $file_data=addslashes(fread($fp=fopen($file_source,"r"), filesize($ file_source))); $file_data=chunk_split(base64_encode(StripSlashes($file_data)))); $this->body.="--".$this->bound_begin.$bound_level.$ this->bound_end."rn"; $this->body.="Content-Type: $file_type;rntname="$file_name"rnContent-Transfer-Encoding: base64rn"; $this- >body.="Content-Disposition: attachment;rntfilename="$file_name"rnrn"; $this->body.=$file_data."rn"; } }//end for
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--rnrn"; }// end else
if ($this->debug) echo nl2br(htmlspecialchars($this->body));
return true; }
function send($from_name,$to_name,$from_mail,$to_mail,$subject,$text_body=false,$html_body=false,$att=false) {
if (empty($ from_mail) or empty($to_mail)) { $this->err_str="The correct email address was not specified: Sender: ".$from_mail." Receiver: ".$to_mail; return false; }
if (gettype($to_mail)!="array") $to_mail=split(",",$to_mail);//If not an array, convert into an array, even if there is only one sending object; if (gettype($to_name)!="array") $to_name=split(",",$to_name);//If it is not an array, convert it to an array , even if there is only one sending object;
$this->Buildbody($text_body,$html_body,$att); // The content of all letters is the same and can be edited only once, but for Different recipients require different Head
if (!$this->open()) return false; if (!$this->command("HELO $this ->hostname",3,"250")) return false; //Establish a link with the server if (!$this->open()) return false; if (!$ this->command("HELO $this->hostname",3,"250")) return false;
for ($i=0;$i{ $this->Buildhead($from_name,$to_name[$i],$from_mail,$to_mail[$i],$subject); if (!$this-> command("RSET",3,"250")) return false; if (!$this->command("MAIL FROM:".$from_mail,3,"250")) return false; if (!$this->command("RCPT TO:".$to_mail[$i],3,"250")) return false; if (!$this->command("DATA" ,3,"354")) return false; // Prepare to send email if ($this->debug) $this->outdebug("sending subject;"); if ( !fputs($this->connection,$this->subject)) {$this->err_str="Error sending headers! ";return false;} if ($this->debug) $this->outdebug("sending body;"); if (!fputs($this->connection,$this- >body)) {$this->err_str="Error sending body! ";return false;} if (!fputs($this->connection,".rn")) {$this->err_str="Error sending text! ";return false;}//The text is sent, exit; $resp=fgets($this->connection,256); if($this->debug) $this- >outdebug("$resp"); if (substr($resp,0,1)!="2") { $this->err_str="After sending, the server No response! !"; return false; } // 发送邮件 } if (!$this->command("QUIT",3,"221")) return false; $this->close(); return true; }
}//end class define }//end if(!isset($__smtp_class__)) ?>
PHP SMTP类使用方法
include("smtp.php");
$mail = new smtp("localhost");
$mail->debug = 0;
$sender_name = "root"; $sender_mail = "root@localhost"; $to_name = "mm"; $to_mail = "[email]mm@your.com[/email]"; $subject = "i miss u"; $content = "i miss u much."; $att[0]["name"] = "miss.jpg"; $att[0]["source"] = "/path/to/your/miss.jpg"; $att[0]["type"] = "image/jpeg"; $att[0]["size"] = "10kb"; $extra_subject = "To:".$to_mail." 主题:".$subject;
if (!$mail->send($sender_name,$to_name,$sender_mail,$to_mail,$extra_subject,$content,false,$att)) $err = 1; ?>
返回文件的MIME类型的函数guessMIMEType() function guessMIMEType($filename) { //GUESS MIME TYPE $filename = basename($filename); if(strrchr($filename,".") == false) { return("application/octet-stream"); }
$ext = strrchr($filename,"."); switch($ext) { case ".gif": return "image/gif"; break; case ".gz": return "application/x-gzip"; case ".htm": case ".html": return "text/html"; break; case ".jpg": return "image/jpeg"; break; case ".tar": return "application/x-tar"; break; case ".txt": return "text/plain"; break; case ".zip": return "application/zip"; break; default: return "application/octet-stream"; break; } }
/* * void add_attachment(string message, [string name], [string ctype]) * Add an attachment to the mail object */ function add_attachment($message, $name = "", $ctype = "application/octet-stream") { $this->parts[] = array ( "ctype" => $ctype, "message" => $message, "encode" => $encode, "name" => $name ); }
/* * void build_message(array part= * Build message parts of an multipart mail */ function build_message($part) { $message = $part["message"]; $message = chunk_split(base64_encode($message)); $encoding = "base64"; return "Content-Type: ".$part["ctype"]. ($part["name"]?";ntname="".$part["name"].""" : ""). "nContent-Transfer-Encoding: $encoding". "nContent-Disposition: attachment". ($part["name"]?";ntfilename="".$part["name"].""" : ""). "nn$messagen"; }
/* * void build_multipart() * Build a multipart mail */ function build_multipart() { $boundary = "b".md5(uniqid(time())); $multipart = "Content-Type: multipart/mixed;ntboundary="$boundary"nnThis is a MIME encoded message.nn--$boundary";
/* * void send() * Send the mail (last class-function to be called) */ function send() { $addtionheaders = ""; if (!empty($this->from)) $addtionheaders .= "From: ".$this->from."n"; if (!empty($this->headers)) $addtionheaders .= $this->headers."n";
if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain");
$mail = new mime_mail(); $mail->from = "[email]foo@bar.com[/email]"; $mail->headers = "Errors-To: [email]foo@bar.com[/email]"; $mail->to = "[email]bar@foo.com[/email]"; $mail->subject = "Testing..."; $mail->body = "This is just a test."; $mail->add_attachment("$attachment", "test.jpg", "image/jpeg"); $mail->send();
*/ ?>
myimap类,读取邮件 ////////////////////////////////////////////// //Origin by Alpha.Z (5/21/2000) //Modified By Belltree <[email]belltree@163.com[/email]> (11/01/2000) /////////////////////////////////////////////// class myimap { var $username=""; var $userpwd=""; var $hostname=""; var $port=0; var $connection=0; //是否连接 var $state="DISCONNECTED"; //连接状态 var $greeting=""; var $must_update=0; var $inStream=0; var $num_msg_parts = 0; var $attach; var $num_of_attach = 0;
function open() { if ($this->port==110) $this->inStream=imap_open("{".$this->hostname."/pop3:110}inbox",$this->username,$this->userpwd); else $this->inStream=imap_open("{".$this->hostname.":143}INBOX",$this->username,$this->userpwd);
function decode_mime_string ($string) { $pos = strpos($string, '=?'); if (!is_int($pos)) { return $string; }
$preceding = substr($string, 0, $pos); // save any preceding text
$search = substr($string, $pos+2, 75); // the mime header spec says this is the longest a single encoded word can be $d1 = strpos($search, '?'); if (!is_int($d1)) { return $string; }
Function get_barefrom($user, $server) { $barefrom = "$user@$real_server";
return $barefrom; }
Function get_structure($msg_num) { $structure=imap_fetchstructure($this->inStream,$msg_num); //echo gettype($structure); return $structure; }
Function proc_structure($msg_part, $part_no, $msg_num) { if ($msg_part->ifdisposition) { // See if it has a disposition, The only thing I know of that this, would be used for would be an attachment // Lets check anyway if ($msg_part->disposition == "attachment") { $att_name = "unknown"; for ($lcv = 0; $lcv < count($msg_part->parameters); $lcv++) { $param = $msg_part->parameters[$lcv];
n"; //} } // END IF ATTACHMENT else //NOT ifdisposition { // I guess it is used for something besides attachments???? } } else { // Not an attachment, lets see what this part is... switch ($msg_part->type) { case 0: $mime_type = "text"; break; case 1: $mime_type = "multipart"; // Hey, why not use this function to deal with all the parts // of this multipart part $this->num_msg_parts = count($msg_part->parts); for ($i = 0; $i < $this->num_msg_parts; $i++) { if ($part_no != "") { $part_no = $part_no."."; } for ($i = 0; $i < count($msg_part->parts); $i++) { $this->proc_structure($msg_part->parts[$i], $part_no.($i + 1), $msg_num); } } break; case 2: $mime_type = "message"; break; case 3: $mime_type = "application"; break; case 4: $mime_type = "audio"; break; case 5: $mime_type = "image"; break; case 6: $mime_type = "video"; break; case 7: $mime_type = "model"; break; default: $mime_type = "unknown"; // hmmm.... }
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn