class smtp_client {
var $connection; // socket connection
var $server; // smtp server name or ip address
var $auth; // Whether to use basic thentication
var $username; // Username for authentication
var $password; // Password for authentication
var $getMsg; // return socket message
function smtp_client($server="localhost",$user="",$pwd="") {
$this->server = $server;
$this->username = $user;
$this->password = $pwd
;
$this->auth = true; //default is false
$this->connection = @fsockopen($this->server, 25); //smtp port 25
if(!is_resource($this->connection)){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^220",$getMsg)){ //judge return code
return 0;
}
if( !$this->auth ) {//jugde whether authenticatication
$rtn_Msg = fputs($this->connection,"HELO \r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^250",$getMsg)){
return 0;
}
}else{ // need authentication
if($this->username == "" || $this->password == "" )
{
return 0;
}
$rtn_Msg = fputs($this->connection,"EHLO \r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^250",$getMsg)){
return 0;
}
$rtn_Msg = fputs($this->connection, "AUTH LOGIN \r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^334",$getMsg)){
return 0;
}
$put_user = base64_encode($this->username)."\r\n";
$rtn_Msg = fputs($this->connection, $put_user);
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^334",$getMsg)){
return 0;
}
$put_passwd = base64_encode($this->password)."\r\n";
$rtn_Msg = fputs($this->connection, $put_passwd);
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^235",$getMsg)){
return 0;
}
}
}
function email($from_mail, $to_mail, $to_name, $mheader, $subject, $body) {
if ($this->connection <= 0) return 0;
$rtn_Msg = fputs($this->connection,"MAIL FROM:$from_mail\r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^250",$getMsg)){
return 0;
}
$rtn_Msg = fputs($this->connection, "RCPT TO:$to_mail\r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^250",$getMsg)){
return 0;
}
$rtn_Msg = fputs($this->connection, "DATA\r\n");
if($rtn_Msg<0){
return 0;
}
$getMsg = fgets($this->connection, 1024);
if(!ereg("^354",$getMsg)){
return 0;
}
$rtn_Msg = fputs($this->connection,"Subject: $subject\r\n");
if($rtn_Msg<0){
return 0;
}
$rtn_Msg = fputs($this->connection,"To: $to_name\r\n");
if($rtn_Msg<0){
return 0;
}
if ($mheader){
$rtn_Msg = fputs($this->connection, "$header\r\n");
if($rtn_Msg<0){
return 0;
}
}
$rtn_Msg = fputs($this->connection,"\r\n");
$rtn_Msg = fputs($this->connection,"$body \r\n");
if($rtn_Msg<0){
return 0;
}
$rtn_Msg = fputs($this->connection,".\r\n");
fgets($this->connection, 1024);
return 1;
}
function close() {
if ($this->connection) {
$rtn_Msg = fputs($this->connection, "QUIT\r\n");
@fclose($this->connection);
$this->connection=0;
}
}
}//End Class
?>
上面的代码总是执行不成功,请高手指教,急急急急急
我的邮箱 bai_shy@gd-soft.com
class Mail_SMTP
{
var $host = 127.0.0.1; // SMTP主机名
var $port = 25; // 主机的smtp端口,一般是25号端口
var $timeout = 5; // 连接主机的最大超时时间
var $debug = 0; // 做为标识,是否在调试状态,是的话,输出调试信息
var $showerr = true;
var $handle;
function Mail_SMTP($server = 127.0.0.1, $port = 25, $time_out = 5){
$this ->host = $server;
$this ->port = $port;
$this ->timeout = $time_out;
return true;
}
function errmsg($err,$show=false){
if ($this->showerr || $show){
echo $err."<br>";
exit();
}
}
function _command($cmd, $response){
if (!(fputs($this->handle,$cmd . "\r\n"))){
$this->errmsg("Couldnt send command to server");
return false;
}
while ( substr($server_response,3,1) != ){
if( !( $server_response = fgets($this->handle, 256))){
$this->errmsg("Couldnt get mail server response codes");
}
}
if( !( substr($server_response, 0, 3) == $response )){
$this->errmsg( "Ran into problems sending Mail. Response: $server_response");
}
}
function connect($user = ,$pass = ){
if( !$this->handle = fsockopen($this->host, 25, $errno, $errstr, 20)){
$this->errmsg( "Could not connect to smtp host : $errno : $errstr");
}
$r = fgets($this->handle, 256);
if ( substr($r, 0, 3) != "220"){
$this->errmsg("couldnt connection to smtp server");
return false;
}
if( $user != && $pass != ){
// Send the RFC2554 specified EHLO.
// This improved as provided by SirSir to accomodate
// both SMTP AND ESMTP capable servers
$this->_command("EHLO \"" . $this->host . "\"", "250");
$this->_command("AUTH LOGIN","334");
$this->_command(base64_encode($user), "334");
$this->_command(base64_encode($pass), "235");
}
else{
// Send the RFC821 specified HELO.
$this->_command("HELO \"" . $this->host . "\"", "250");
}
}
function sendmail($header, $body = ){
//header
if (isset($header)){
if(is_array($header)){
if(sizeof($header) > 1){
$header = join("\r\n", $header);
}
else{
$header = $header[0];
}
}
$header = chop($header);
$header = preg_replace("/(?<!\r)\n/si", "\r\n", $header);
$header_array = explode("\r\n", $header);
@reset($header_array);
$headers = "";
while( list(, $header) = each($header_array)){
if ( preg_match("/^from:/si", $header)){
$from = preg_replace("/^from:(.*)/si", "\\1", $header);
}
elseif ( preg_match("/^to:/si", $header)){
$to = preg_replace("/^to:(.*)/si", "\\1", $header);
}
elseif ( preg_match("/^subject:/si", $header)){
$subject = preg_replace("/^subject:(.*)/si", "\\1", $header);
}
elseif ( preg_match("/^cc:/si", $header)){
$cc = preg_replace("/^cc:(.*)/si", "\\1", $header);
}
elseif( preg_match("/^bcc:/si", $header )){
$bcc = preg_replace("/^bcc:(.*)/si", "\\1", $header);
$header = "";
continue;
}
$headers .= $header . "\r\n";
}
$headers = chop($headers);
}
//body
$body = preg_replace("/(?<!\r)\n/si", "\r\n", $body);
if(trim($to) == ""){
$this->errmsg("No email address specified");
}
if(trim($subject) == ""){
$this->errmsg( "No email Subject specified");
}
$to = explode(",", $to);
$cc = explode(",", $cc);
$bcc = explode(",", $bcc);
$this->_command("MAIL FROM: " . $from, "250");
@reset( $to );
while( list( , $to_address ) = each( $to )){
$to_address = trim($to_address);
if ( preg_match(/[^ ]+\@[^ ]+/, $to_address)){
$this->_command("RCPT TO: <$to_address>", "250");
}
}
// Ok now do the CC and BCC fields...
@reset( $bcc );
while( list( , $bcc_address ) = each( $bcc )){
$bcc_address = trim( $bcc_address );
if ( preg_match(/[^ ]+\@[^ ]+/, $bcc_address)){
$this->_command("RCPT TO: <$bcc_address>", "250");
}
}
@reset( $cc );
while( list( , $cc_address ) = each( $cc )){
$cc_address = trim( $cc_address );
if ( preg_match(/[^ ]+\@[^ ]+/, $cc_address)){
$this->_command("RCPT TO: <$cc_address>", "250");
}
}
// Ok now we tell the server we are ready to start sending data
$this->_command("DATA", "354");
$bodys = $headers . "\r\n\r\n" . $body . "\r\n";
fputs($this->handle, $bodys);
$this->_command("\r\n.", "250");
return TRUE;
}
function quit(){
// Now tell the server we are done and close the socket...
fputs($this->handle, "QUIT\r\n");
fclose($this->handle);
}
}//end class