<?php
/******************************************
* CSMTP class v 1.0 *
* by CayLeung *
* copyright (c) 2008 CayLeung *
*******************************************
* [email protected] *
*******************************************
* this script is free to use only if this *
* copyright statement is not removed *
*******************************************/
class CSMTP{
protected $_emailaddr;
protected $_mailhost;
protected $_smtpport = 25;
protected $_smtptimeout = 30;
protected $_smtpgetanswertimeout = 10;
protected $_smtpheaders = array();
protected $_smtpbody = "";
protected $_smtpboundary = "";
protected $_smtpattachfile;
protected $_debug = 1; function __construct($email,$password){ //发邮件的邮箱地址和密码
$this ->_emailaddr = $email;
$temp = explode("@",$email);
$this ->_user = trim($temp[0]);
$this ->_host = trim($temp[1]);
$this ->_password = $password;
$this ->_smtphost = "smtp.".$this ->_host;
}
public function linkSMTP(){ //连接smtp服务器
$this ->_smtpfs = @fsockopen($this ->_smtphost,$this ->_smtpport, $errorn, $this ->_errorstr, $this ->_smtptimeout);
if(!$this ->checkOK($this ->getAnswer($this ->_smtpfs),"220"))$this -> outbug("Can't link ".$this ->_smtphost);
$this ->sendCommand($this ->_smtpfs, "HELO ".$this ->_host."\r\n","250");
$this ->sendCommand($this ->_smtpfs, "EHLO ".$this ->_host."\r\n","250");
$this ->sendCommand($this ->_smtpfs, "AUTH LOGIN\r\n","334");
$this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_user)."\r\n","334");
$this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_password)."\r\n","235");
}
public function quitSMTP(){ //离开服务器
$this ->sendCommand($this ->_smtpfs, "QUIT\r\n","221");
fclose($this ->_smtpfs);
}
public function attachFile(){ //加入附件,支持数组或单文件
foreach(func_get_args() as $filename){
if(!file_exists($filename))
if(!file_exists(dirname($_SERVER['SCRIPT_FILENAME'])."/".$filename))break;
$handle = fopen($filename,"rb");
$str = fread($handle ,filesize($filename));
fclose($handle);
$temp_mime = include("mime.php"); //所包含文件找不到
$temp_arr = explode("." ,basename($filename));
$temp_key = array_slice($temp_arr, -1, 1);
$filemime = isset($temp_mime[$temp_key[0]])?$temp_mime[$temp_key[0]]:"application/octet-stream";
$temp = "Content-Type: ".$filemime.";\r\n";
$temp .= " name=\"".$this ->encode(basename($filename),1)."\"\r\n";
$temp .= "Content-Transfer-Encoding: base64\r\n";
$temp .= "Content-Disposition: attachment;\r\n";
$temp .= " filename=\"".$this ->encode(basename($filename),1)."\"\r\n";
$temp .= "\r\n";
$temp .= $this ->cutFile($this ->encode($str));
$this ->_smtpattachfile[] = $temp;
}
} public function sendMail(){ //发送邮件
foreach($this ->_smtpmialto as $mailadd_val){
$filestate = count($this ->_smtpattachfile)>0 ? 1 : 0;
$this ->sendCommand($this ->_smtpfs, "MAIL FROM:<".$this ->_emailaddr.">\r\n","250");
$this ->sendCommand($this ->_smtpfs, "RCPT TO:<".$mailadd_val.">\r\n","250");
foreach($this ->_smtpheaders as $val)
$temp .= $val;
$temp .= "--".$this ->_smtpboundary."\r\n";
if($filestate==1){
$new_smtpboundary = "--=".md5(microtime());
$temp .= "Content-Type: multipart/alternative;\r\n";
$temp .= " boundary=\"".$new_smtpboundary."\"\r\n";
$temp .= "\r\n";
$temp .= "--".$new_smtpboundary."\r\n";
}
$temp .= "Content-Type: ".$this ->_smtpmialmime."\r\n";
$temp .= "Content-Transfer-Encoding: base64\r\n";
$temp .= "\r\n";
$temp .= $this ->encode($this ->_smtpcontent)."\r\n";
$temp .= "\r\n";
if($filestate==1){
$temp .= "--".$new_smtpboundary."--\r\n";
$temp .= "\r\n";
foreach($this ->_smtpattachfile as $val){
$temp .= "--".$this ->_smtpboundary."\r\n";
$temp .= $val;
}
}
$temp .= "--".$this ->_smtpboundary."--\r\n";
echo $temp .= ".\r\n";
$this ->sendCommand($this ->_smtpfs, "DATA\r\n","354");
$this ->sendCommand($this ->_smtpfs, $temp,"250");
unset($temp);
}
}
public function buildMail($to ,$subject,$content ,$mime ,$username=""){ //建立邮件,建立邮件文件头
if(is_array($to)){
$this ->_smtpmialto = $to;
}else{
$this ->_smtpmialto[] = $to;
}
$this ->_smtpmialfrom = $this ->_emailaddr;
$this ->_smtpboundary = "--=".md5(microtime());
$this ->_smtpmialmime = $mime;
$headers["date"] = "Date: ".date("r")."\r\n";
$headers["subject"] = "Subject: ".$this ->encode($subject ,1)."\r\n";
$headers["message_id"] = "Message-Id: <".md5(uniqid(microtime())) ."@".$this ->_host.">\r\n";
$username = $username==""?$this ->_user:$username;
$headers["from"] = "From: ".$this ->encode($username ,1)." <".$this ->_emailaddr.">\r\n";
$temp = explode("@",$this ->_smtpmialto);
$user = trim($temp[0]);
$headers["to"] = "To: ".$this ->encode($user,1)." <".$this ->_smtpmialto.">\r\n";
$headers["content_type"] = "Content-Type: multipart/mixed ";
$headers["boundary"] = "; boundary=\"".$this ->_smtpboundary ."\"; charset=\"utf-8\"\r\n";
$headers["content_transfer_encoding"] = "Content-Transfer-Encoding: base64\r\n";
$headers["other"] = "X-Power-by: CayLeung\r\n";
$headers["version"] = 'MIME-Version: 1.0' . "\r\n";
$headers["end"] = "\r\n";
$this ->_smtpheaders = $headers;
$this ->_smtpcontent = $content;
}
protected function getAnswer($socket){ //获取服务器返回信息
$starttime = time();
$timeout = $this ->_smtpgetanswertimeout ;
$this ->checkLink($this ->_smtpfs);
$results = $line = "";
while(true){
$line = fgets($this ->_smtpfs,4096);
$results .= $line;
if(strpos($line,"\r\n")!==false){
break;
}
if((time() - $starttime)>$timeout){
$this -> _state = 0;
break;
}
}
$this -> _wait = 0;
return $results;
}
protected function sendCommand($socket,$command,$okstr){ //发送命令
echo "C: ".$command."<br/>";
$this ->checkLink($this ->_smtpfs);
fwrite($socket,$command);
$r = $this ->getAnswer($socket)."<br/>";
echo "S: ".$r;
return $r;
//echo $r= $this ->checkOK($r,$okstr)?"OK":"KO";
}
protected function isMailAddr($addr){ //判断是否为邮箱地址
$r = preg_match("'\w[\w]+@[\w]+\.(com|cn|org)'iU",$addr)==0?false:true;
return $r;
} protected function encode($str,$mode=0){ //进行base64编码
switch($mode){
case 0:
return base64_encode($str);
case 1:
return "=?UTF8?B?".base64_encode($str)."?=";
default:
return $str;
}
}
protected function cutFile($str ,$len = 80){ //文件切割传输
$strlen = strlen($str);
$i = 0;
$temp = "";
while($strlen>0){
$temp .= substr($str,$i*$len,$len);
$temp .= "\r\n";
$strlen -= $len;
$i++;
}
return $temp;
}
protected function checkOK($str,$okstr){ //判断服务器返回值是否成功
return strtoupper(substr($str,0,strlen($okstr)))==$okstr;
}
protected function checkLink($socket){ //检查连接是否断开
if (!$socket) $this ->outbug($this ->_errorstr);
}
protected function outbug($str){ //显示错误信息
if(!$this ->_debug)return false;
echo "Catch a bug : ".$str;
exit;
}
} /*
发送邮箱和密码$e = new CSMTP("[email protected]","*****");连接服务器
$e -> linkSMTP(); 建立邮件,
第一参数为邮箱地址Array or String
第二参数为邮件标题String
第三参数为邮件内容String
第四参数为邮件格式String
$e -> buildMail(array("[email protected]","[email protected]") ,"Hi Cay" ,"Just say hi!!哇哈哈哈","text/html"); 添加附件
绝对路径或相对路径,支持多文件
$e -> attachFile("temp.txt","tempmail.txt","x.gif");
$e -> attachFile("tempmail.txt"); 发送文件
$e -> sendMail(); 离开服务器
$e -> quitSMTP();
*/
?>错误如下:
C: HELO 163.com
S: 250 OK
C: EHLO 163.com
S: 250 OK
C: AUTH LOGIN
S: 334 dXNlcm5hbWU6
C: aW5hbW9tb28=
S: 334 UGFzc3dvcmQ6
C: MjY1OTQzMA==
S: 550 用户被锁定Warning: CSMTP::include(mime.php) [csmtp.include]: failed to open stream: No such file or directory in D:\APMServ5.2.6\www\htdocs\mail\CSMTP.php on line 55Warning: CSMTP::include() [function.include]: Failed opening 'mime.php' for inclusion (include_path='.;C:\php5\pear') in D:\APMServ5.2.6\www\htdocs\mail\CSMTP.php on line 55
C: MAIL FROM:
S: 550 用户被锁定
C: RCPT TO:
S: 503 bad sequence of commands
Date: Tue, 22 Dec 2009 08:13:19 +0000 Subject: =?UTF8?B?dGl0bGU=?= Message-Id: 望高手指教!
/******************************************
* CSMTP class v 1.0 *
* by CayLeung *
* copyright (c) 2008 CayLeung *
*******************************************
* [email protected] *
*******************************************
* this script is free to use only if this *
* copyright statement is not removed *
*******************************************/
class CSMTP{
protected $_emailaddr;
protected $_mailhost;
protected $_smtpport = 25;
protected $_smtptimeout = 30;
protected $_smtpgetanswertimeout = 10;
protected $_smtpheaders = array();
protected $_smtpbody = "";
protected $_smtpboundary = "";
protected $_smtpattachfile;
protected $_debug = 1; function __construct($email,$password){ //发邮件的邮箱地址和密码
$this ->_emailaddr = $email;
$temp = explode("@",$email);
$this ->_user = trim($temp[0]);
$this ->_host = trim($temp[1]);
$this ->_password = $password;
$this ->_smtphost = "smtp.".$this ->_host;
}
public function linkSMTP(){ //连接smtp服务器
$this ->_smtpfs = @fsockopen($this ->_smtphost,$this ->_smtpport, $errorn, $this ->_errorstr, $this ->_smtptimeout);
if(!$this ->checkOK($this ->getAnswer($this ->_smtpfs),"220"))$this -> outbug("Can't link ".$this ->_smtphost);
$this ->sendCommand($this ->_smtpfs, "HELO ".$this ->_host."\r\n","250");
$this ->sendCommand($this ->_smtpfs, "EHLO ".$this ->_host."\r\n","250");
$this ->sendCommand($this ->_smtpfs, "AUTH LOGIN\r\n","334");
$this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_user)."\r\n","334");
$this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_password)."\r\n","235");
}
public function quitSMTP(){ //离开服务器
$this ->sendCommand($this ->_smtpfs, "QUIT\r\n","221");
fclose($this ->_smtpfs);
}
public function attachFile(){ //加入附件,支持数组或单文件
foreach(func_get_args() as $filename){
if(!file_exists($filename))
if(!file_exists(dirname($_SERVER['SCRIPT_FILENAME'])."/".$filename))break;
$handle = fopen($filename,"rb");
$str = fread($handle ,filesize($filename));
fclose($handle);
$temp_mime = include("mime.php"); //所包含文件找不到
$temp_arr = explode("." ,basename($filename));
$temp_key = array_slice($temp_arr, -1, 1);
$filemime = isset($temp_mime[$temp_key[0]])?$temp_mime[$temp_key[0]]:"application/octet-stream";
$temp = "Content-Type: ".$filemime.";\r\n";
$temp .= " name=\"".$this ->encode(basename($filename),1)."\"\r\n";
$temp .= "Content-Transfer-Encoding: base64\r\n";
$temp .= "Content-Disposition: attachment;\r\n";
$temp .= " filename=\"".$this ->encode(basename($filename),1)."\"\r\n";
$temp .= "\r\n";
$temp .= $this ->cutFile($this ->encode($str));
$this ->_smtpattachfile[] = $temp;
}
} public function sendMail(){ //发送邮件
foreach($this ->_smtpmialto as $mailadd_val){
$filestate = count($this ->_smtpattachfile)>0 ? 1 : 0;
$this ->sendCommand($this ->_smtpfs, "MAIL FROM:<".$this ->_emailaddr.">\r\n","250");
$this ->sendCommand($this ->_smtpfs, "RCPT TO:<".$mailadd_val.">\r\n","250");
foreach($this ->_smtpheaders as $val)
$temp .= $val;
$temp .= "--".$this ->_smtpboundary."\r\n";
if($filestate==1){
$new_smtpboundary = "--=".md5(microtime());
$temp .= "Content-Type: multipart/alternative;\r\n";
$temp .= " boundary=\"".$new_smtpboundary."\"\r\n";
$temp .= "\r\n";
$temp .= "--".$new_smtpboundary."\r\n";
}
$temp .= "Content-Type: ".$this ->_smtpmialmime."\r\n";
$temp .= "Content-Transfer-Encoding: base64\r\n";
$temp .= "\r\n";
$temp .= $this ->encode($this ->_smtpcontent)."\r\n";
$temp .= "\r\n";
if($filestate==1){
$temp .= "--".$new_smtpboundary."--\r\n";
$temp .= "\r\n";
foreach($this ->_smtpattachfile as $val){
$temp .= "--".$this ->_smtpboundary."\r\n";
$temp .= $val;
}
}
$temp .= "--".$this ->_smtpboundary."--\r\n";
echo $temp .= ".\r\n";
$this ->sendCommand($this ->_smtpfs, "DATA\r\n","354");
$this ->sendCommand($this ->_smtpfs, $temp,"250");
unset($temp);
}
}
public function buildMail($to ,$subject,$content ,$mime ,$username=""){ //建立邮件,建立邮件文件头
if(is_array($to)){
$this ->_smtpmialto = $to;
}else{
$this ->_smtpmialto[] = $to;
}
$this ->_smtpmialfrom = $this ->_emailaddr;
$this ->_smtpboundary = "--=".md5(microtime());
$this ->_smtpmialmime = $mime;
$headers["date"] = "Date: ".date("r")."\r\n";
$headers["subject"] = "Subject: ".$this ->encode($subject ,1)."\r\n";
$headers["message_id"] = "Message-Id: <".md5(uniqid(microtime())) ."@".$this ->_host.">\r\n";
$username = $username==""?$this ->_user:$username;
$headers["from"] = "From: ".$this ->encode($username ,1)." <".$this ->_emailaddr.">\r\n";
$temp = explode("@",$this ->_smtpmialto);
$user = trim($temp[0]);
$headers["to"] = "To: ".$this ->encode($user,1)." <".$this ->_smtpmialto.">\r\n";
$headers["content_type"] = "Content-Type: multipart/mixed ";
$headers["boundary"] = "; boundary=\"".$this ->_smtpboundary ."\"; charset=\"utf-8\"\r\n";
$headers["content_transfer_encoding"] = "Content-Transfer-Encoding: base64\r\n";
$headers["other"] = "X-Power-by: CayLeung\r\n";
$headers["version"] = 'MIME-Version: 1.0' . "\r\n";
$headers["end"] = "\r\n";
$this ->_smtpheaders = $headers;
$this ->_smtpcontent = $content;
}
protected function getAnswer($socket){ //获取服务器返回信息
$starttime = time();
$timeout = $this ->_smtpgetanswertimeout ;
$this ->checkLink($this ->_smtpfs);
$results = $line = "";
while(true){
$line = fgets($this ->_smtpfs,4096);
$results .= $line;
if(strpos($line,"\r\n")!==false){
break;
}
if((time() - $starttime)>$timeout){
$this -> _state = 0;
break;
}
}
$this -> _wait = 0;
return $results;
}
protected function sendCommand($socket,$command,$okstr){ //发送命令
echo "C: ".$command."<br/>";
$this ->checkLink($this ->_smtpfs);
fwrite($socket,$command);
$r = $this ->getAnswer($socket)."<br/>";
echo "S: ".$r;
return $r;
//echo $r= $this ->checkOK($r,$okstr)?"OK":"KO";
}
protected function isMailAddr($addr){ //判断是否为邮箱地址
$r = preg_match("'\w[\w]+@[\w]+\.(com|cn|org)'iU",$addr)==0?false:true;
return $r;
} protected function encode($str,$mode=0){ //进行base64编码
switch($mode){
case 0:
return base64_encode($str);
case 1:
return "=?UTF8?B?".base64_encode($str)."?=";
default:
return $str;
}
}
protected function cutFile($str ,$len = 80){ //文件切割传输
$strlen = strlen($str);
$i = 0;
$temp = "";
while($strlen>0){
$temp .= substr($str,$i*$len,$len);
$temp .= "\r\n";
$strlen -= $len;
$i++;
}
return $temp;
}
protected function checkOK($str,$okstr){ //判断服务器返回值是否成功
return strtoupper(substr($str,0,strlen($okstr)))==$okstr;
}
protected function checkLink($socket){ //检查连接是否断开
if (!$socket) $this ->outbug($this ->_errorstr);
}
protected function outbug($str){ //显示错误信息
if(!$this ->_debug)return false;
echo "Catch a bug : ".$str;
exit;
}
} /*
发送邮箱和密码$e = new CSMTP("[email protected]","*****");连接服务器
$e -> linkSMTP(); 建立邮件,
第一参数为邮箱地址Array or String
第二参数为邮件标题String
第三参数为邮件内容String
第四参数为邮件格式String
$e -> buildMail(array("[email protected]","[email protected]") ,"Hi Cay" ,"Just say hi!!哇哈哈哈","text/html"); 添加附件
绝对路径或相对路径,支持多文件
$e -> attachFile("temp.txt","tempmail.txt","x.gif");
$e -> attachFile("tempmail.txt"); 发送文件
$e -> sendMail(); 离开服务器
$e -> quitSMTP();
*/
?>错误如下:
C: HELO 163.com
S: 250 OK
C: EHLO 163.com
S: 250 OK
C: AUTH LOGIN
S: 334 dXNlcm5hbWU6
C: aW5hbW9tb28=
S: 334 UGFzc3dvcmQ6
C: MjY1OTQzMA==
S: 550 用户被锁定Warning: CSMTP::include(mime.php) [csmtp.include]: failed to open stream: No such file or directory in D:\APMServ5.2.6\www\htdocs\mail\CSMTP.php on line 55Warning: CSMTP::include() [function.include]: Failed opening 'mime.php' for inclusion (include_path='.;C:\php5\pear') in D:\APMServ5.2.6\www\htdocs\mail\CSMTP.php on line 55
C: MAIL FROM:
S: 550 用户被锁定
C: RCPT TO:
S: 503 bad sequence of commands
Date: Tue, 22 Dec 2009 08:13:19 +0000 Subject: =?UTF8?B?dGl0bGU=?= Message-Id: 望高手指教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货