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="ûÓÐÁ¬½Óµ½ÈκηþÎñÆ÷£¬Çë¼ì²éÍøÂçÁ¬½Ó";
return false;
}
if ($this->debug)
$this->outdebug(">>> $command");
if (!fputs($this->connection,"$command \r\n"))
{
$this->err_str="ÎÞ·¨·¢ËÍÃüÁî".$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." ÃüÁî·þÎñÆ÷·µ»ØÎÞЧ:".$resp;
return false;
}
else
return true;
}
}
Function open()
{
if($this->hostname=="")
{$this->err_str="ÎÞЧµÄÖ÷»úÃû!!";
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="Á¬½Óµ½¡¡SMTP ·þÎñÆ÷ʧ°Ü,´íÎóÐÅÏ¢£º".$err_str."´íÎóºÅ£º".$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="·þÎñÆ÷·µ»ØÎÞЧµÄÐÅÏ¢£º".$resp."Çë¼ì²éSMTP·þÎñÆ÷ÊÇ·ñÕýÈ·";
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)."?=\r\n";
$this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=\r\n";
$subject=ereg_replace("\n","",$subject);
$this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)."?=\r\n";
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("\]+)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;
}
$this->temp_body_images[$image_count][name]=basename($image_tag);
$this->temp_body_images[$image_count][type]=$content_type;
$this->temp_body_images[$image_count][cid]=$cid;
$this->temp_body_images[$image_count][data]=$image_data;
$image_count++;
}
$this->temp_HTML_body=$passed.$HTML_body;
return true;
}
function build_content($bound_level=0,$text_body,$HTML_body,$hava_att=false)
{
if ($HTML_body)
{
if (eregi("\body.="Content-Type: multipart/related;
type=\"multipart/alternative\";
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
}
else
{
$this->body.="Content-Type: multipart/related;
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
} // ¶ÔÓÚÊÇ·ñ text ÕýÎÄ ¡¢ HTMLÕýÎÄ ÓÐûÓУ¬ÐëÓв»Í¬µÄ MIME Í·
if (!$hava_att) $this->body.="This is a multi-part message in MIME format.\r\n\r\n";
// ÕýÎıêʶ£¬Èç¹ûÊÇÒѾÓи½¼þµÄ±àÂ룬ÔòÔÚÕýÎÄ Öв»ÐèÒªÕâÒ»¾ä
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->parse_HTML_body($HTML_body);
if ($text_body)
{
$this->body.="Content-Type: multipart/alternative;
boundary=\"";
$bound_level++;
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/HTML;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_HTML_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/HTML;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_HTML_body)))."\r\n";
} //ÕýÎıàÂ룬ÓлòûÓС¡text ²¿·Ö£¬±à³É²»Í¬µÄ¸ñʽ¡£
for ($i=0;$itemp_body_images);$i++)
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type:".$this->temp_body_images[$i][type].";
name=\"";
$this->body.=$this->temp_body_images[$i][name]."\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="Content-ID: temp_body_images[$i][cid].">\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."\r\n";
}
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else // ÓлòûÓÐͼƬ£¬ÒÔÉÏÊÇÓÐͼƬµÄ´¦Àí£¬ÏÂÃæÊÇûÓÐͼƬµÄ´¦Àí
{
$this->temp_HTML_body=$HTML_body;
if ($text_body)
{
$bound_level++;
$this->body.="Content-Type: multipart/alternative;
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
if (!$hava_att) $this->body.="\r\nThis is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/HTML;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_HTML_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="Content-Type: text/HTML;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_HTML_body)))."\r\n";
} //ÕýÎıàÂ룬ÓлòûÓС¡text ²¿·Ö£¬±à³É²»Í¬µÄ¸ñʽ¡£
} // end else
}
else // Èç¹ûûÓС¡HTML ÕýÎÄ£¬Ö»ÓС¡text ÕýÎÄ¡¡
{
$this->body.="Content-Type: text/plain;
charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
}
} // end function default
Function Buildbody($text_body=null,$HTML_body=null,$att=null)
{
$this->body="MIME-Version: 1.0\r\n";
if (null==$att or (@count($att)==0)) //Èç¹ûûÓи½¼þ£¬²é¿´ÕýÎĵÄÀàÐÍ¡¡£»
{
$encode_level=0;
$this->build_content($encode_level,$text_body,$HTML_body);
} // Èç¹ûûÓи½¼þ£¬
// ********************************************************
else //Èç¹ûÓи½¼þ£¬
{
$bound_level=0;
$this->body.="Content-Type: multipart/mixed;
boundary=\"";
$bound_level++;
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="This is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->build_content($bound_level,$text_body,$HTML_body,true); // ±àÈëÕýÎIJ¿·Ö
$num=count($att);
for ($i=0;$ibody.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: $file_type;\r\n name=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\n";
$this->body.="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$this->body.=$file_data."\r\n";
}
} //end for
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
} // 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="ûÓÐÖ¸¶¨ÕýÈ·µÄÓʼþµØÖ·:·¢ËÍÈË£º".$from_mail."½ÓÊÕÈË£º".$to_mail;
return false;
}
if (gettype($to_mail)!="array")
$to_mail=split(",",$to_mail); //Èç¹û²»ÊÇÊý×飬ת»»³ÉÊý×飬ÄÄÅÂÖ»ÓÐÒ»¸ö·¢ËͶÔÏó;
if (gettype($to_name)!="array")
$to_name=split(",",$to_name); //Èç¹û²»ÊÇÊý×飬ת»»³ÉÊý×飬ÄÄÅÂÖ»ÓÐÒ»¸ö·¢ËͶÔÏó;
$this->Buildbody($text_body,$HTML_body,$att);
// ËùÓÐÐżþµÄÄÚÈÝÊÇÒ»ÑùµÄ£¬¿ÉÒÔÖ»±àÒ»´Î£¬¶ø¶ÔÓÚ²»Í¬µÄÊÕÐÅÈË£¬ÐèÒª²»Í¬µÄ¡¡Head
if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;
// Óë·þÎñÆ÷½¨Á¢Á´½Ó
if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;
for ($i=0;$iBuildhead($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;
// ×¼±¸·¢ËÍÓʼþ
if ($this->debug) $this->outdebug("sending subject;");
if (!fputs($this->connection,$this->subject)) { $this->err_str="·¢ËÍÓʼþͷʱ³ö´í£¡"; return false;}
if ($this->debug) $this->outdebug("sending body;");
if (!fputs($this->connection,$this->body)) { $this->err_str="·¢ËÍÕýÎÄʱ³ö´í£¡"; return false;}
if (!fputs($this->connection,".\r\n")) { $this->err_str="·¢ËÍÕýÎÄʱ³ö´í£¡"; return false;} //ÕýÎÄ·¢ËÍÍê±Ï£¬Í˳ö£»
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,1)!="2")
{
$this->err_str="·¢ËÍÍêºó£¬·þÎñÆ÷ûÓÐÏìÓ¦£¡£¡";
return false;
}
// ·¢ËÍÓʼþ
}
if (!$this->command("QUIT",3,"221")) return false;
$this->close();
return true;
}
}//end class define
}//end if(!isset($__smtp_class__))
?>

