欢迎光临
我们一直在努力

用SMTP传送邮件时的问题-PHP教程,邮件处理

建站超值云服务器,限时71元/月

下面是我找到的一个直接使用smtp发送邮件的php例子程序(经改编), 但是服务器方总是提示: 550 system busy! 并且邮件无法发送.
那位知道如何处理?

另: 对于smtp服务器需要密码的情况该如何处理验证呢?

sory , 刚刚看到 smtp 发送邮件的问题 (链接的这个帖子怎么不是精华?, 建议斑竹给一个啊).

问题已经解决:
1. 命令data应该是”data\r\n”
2. from和to命令应该有, 否则一些server拒发邮件
3.使用base64_encode 加密用户名和密码

<?php

require(“php\mail.php”);
?>

<?
$smtp = “smtp.163.net”;
$from =”hylpro@163.net”;

$to = “hylpro@163.net”;
$subject =”hello php mail”;

$message=”hello! this is a test use php”;

$mail = new mail($smtp,”welcom use this mail”,true);

$mail->send( $to,$from,$subject,$message);

?>



<?
//---------------
// 实现smtp
//---------------

class mail{
var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在helo后面,欢迎用户
var $debug; //是否显示调试信息
var $smtp; //smtp服务器
var $port; //smtp端口号
var $fp; //socket句柄


//construct
function mail($smtp, $welcome="", $debug=false)
{
if(empty($smtp)) die("smtp cannt be null!");

$this->smtp=$smtp;
if(empty($welcome))
{
$this->welcome=gethostbyaddr("localhost");
}
else
$this->welcome=$welcome;

$this->debug=$debug;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}

//
function show_debug($message, $inout)
{
if ($this->debug)
{
if($inout=="in") //响应信息
{
$m="<b> 收: </b>";
}
else
$m="<b> 发: </b>" ;

if(!ereg("n$", $message))
$message .= " ";

$message=nl2br($message);

echo "<font color=#339933> $m </font>$message ";
}
}//end show debug


function do_command($command, $code)
{

$this->lastact=$command."\r\n";
$this->show_debug($this->lastact, "out");

fputs ( $this->fp, $this->lastact );


$this->lastmessage = fgets ( $this->fp, 512 );

$this->show_debug($this->lastmessage, "in");

if(!ereg("^$code", $this->lastmessage)) return false;
else return true;


} //end do command


//发一个命令
function command($command, $code)
{
$this->lastact=$command."\r\n";
$this->show_debug($this->lastact, "out");

fputs ( $this->fp, $this->lastact );

} //end send cmd

//等待一个应答
function wait($code)
{

$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");

if(!ereg("^$code", $this->lastmessage))
{
return false;
}
else
{
return true;
}

}


//发送邮件
function send( $to,$from,$subject,$message)
{
//连接服务器
echo "<br>";

$this->lastact="connect";
$this->show_debug("connect to smtp server : ".$this->smtp, "out");
$this->fp = fsockopen ( $this->smtp, $this->port );

if ( $this->fp )
{
// set_socket_blocking( $this->fp, true );
// stream_set_blocking($this->fp,true);

$this->lastmessage=fgets($this->fp,512);
$this->show_debug($this->lastmessage, "in");
if (! ereg ( "^220", $this->lastmessage ) )
{
return false;
}
else
{
if(!$this->do_command("ehlo i want mail!", "250"))
{
fclose($this->fp);
return false;
}



//处理认证, 不知如何直接使用用户名和密码计算认证值
if(!$this->do_command("auth login","334"))
{
fclose($this->fp);
return false;
}

//user hash
if(!$this->do_command("xxxxx","334"))
{
fclose($this->fp);
return false;
}

//code hash
if(!$this->do_command("xxxx","235"))
{
fclose($this->fp);
return false;
}


if(!$this->do_command("mail from : <$from> " , "250"))
{
fclose($this->fp);
return false;
}

if(!$this->do_command("rcpt to: <$to>", "250"))
{
fclose($this->fp);
return false;
}

//发送正文
if(!$this->do_command("data\r\n.", "354"))
{
fclose($this->fp);
return false;
}

fputs($this->fp, "from: hylpro <$from>\r\n");
fputs($this->fp,"to: $to <$to>\r\n");
fputs($this->fp, "mime-version: 1.0\r\n");
fputs($this->fp, "subject: $subject\r\n");
fputs($this->fp, "context-type : text/plain;\r\n");
fputs($this->fp, " charset = \"gb2312\" \r\n ");
fputs($this->fp, "content-transfer-encoding : quoted--printable ;\r\n");

fputs($this->fp, "$message\r\n");
fputs($this->fp, " \r\n");
fputs($this->fp, " \r\n");
fputs($this->fp, ".\r\n"); //end


$this->show_debug($message, "out");
echo "<br>";
$this->show_debug(".\r\n", "out"); //以只含点的行结束数据传送


$this->wait("250"); 这里总是返回 system busy

if(!$this->do_command("quit", "250"))
{
fclose($this->fp);
return false;
}

fclose($this->fp);

}
return true;
} //end if($this->fp)
else
{
$this->show_debug("connect failed!", "in");
return false;
}
} //end send

} //end class


?>
赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用SMTP传送邮件时的问题-PHP教程,邮件处理
分享到: 更多 (0)