时间:2021-07-01 10:21:17 帮助过:5人阅读
class aMail{
public static $smtp_prot = 25;
public static $debug = true;
public static $crlf = "\r\n";
var $smtp_host;
var $login_name;
var $password;
var $form;
var $to;
var $err;
var $message;
var $socket;
function __construct($options=""){
if($options == "" || !is_array($options)){
return false;
}
foreach($options as $key=>$val){
$this->$key = $val;
}
}
function sendMail(){
$this->socket = fsockopen($this->smtp_host,self::$smtp_prot);
$socket = $this->socket;
$crlf = self::$crlf;
stream_set_blocking($socket,true);
$this->message = fgets($socket,512);
echo $this->message;
if(!$this->print_messsage("/^220/","connect fail")){
return false;
}
fputs($socket,"EHLO 127.0.0.1 ".$crlf);
$this->message = fgets($socket, 512);
if(!$this->print_messsage("/^2/","EHLO fail")){
return false;
}
fputs($socket,"AUTH LOGIN".$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^2/","AUTH LOGIN fail");
fputs($socket,base64_encode($this->login_name).$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^2/","login_name fail");
fputs($socket,base64_encode($this->password).$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^2/","password fail");
fputs($socket,"MAIL FROM: <".$this->from.">".$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^2/","from fail");
fputs($socket,"RCPT TO: <".$this->to.">".$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^334|^2/","to fail");
fputs($socket,"DATA".$crlf);
$this->message = fgets($socket, 512);
$this->print_messsage("/^334|^2/","DATA fail");
$html = implode("",array(
"To: ".$this->to.$crlf,
"From: ".$this->from.$crlf,
"Subject: ".$this->subject.$crlf.$crlf,
$this->content.$crlf.".".$crlf,
));
fputs($socket,$html);
$this->message = fgets($socket, 512);
$this->print_messsage("/^2/","html fail");
fputs($socket, "QUIT".$crlf);
$this->message=fgets($socket,512);
$this->print_messsage("/^2/","QUIT fail");
}
function print_messsage($reg="",$err=""){
$message = $this->message;
if(preg_match($reg,$message)<=0){
$this->err = $err."___".$message."--------------";
echo $err."___".$message."--------------
";
return false;
}
if(self::$debug){
echo $this->message."
";
}
return true;
}
}
$mail_options = array(
"smtp_host"=>"smtp.sina.com.cn",
"login_name"=>"ycwtt0527",