不会,我的邮箱怎么会不知道?
isp提供的邮件服务器出错:501 dcode failed
还有一个提示:500 5.7.0 authentication failed
还有502错误等等。我是在搞不懂,上面的方式到底能不能够通过服务器验证?如果不能够,我们应该修改哪里?
isp提供的邮件服务器出错:501 dcode failed
还有一个提示:500 5.7.0 authentication failed
还有502错误等等。我是在搞不懂,上面的方式到底能不能够通过服务器验证?如果不能够,我们应该修改哪里?
501 是 参数格式错误 ,可能是是命令格式出了问题;
502 是 命令不可实现 ,可能是smtp server不支持认证$this->lastact="AUTH LOGIN"."\n";
if(!$this->do_command($this->lastact,"334"))
~~~~~~~~~~~~~~这句应该检查认证后服务返回的结果,"334"是正确的结果.
如果发送了AUTH LOGIN命令后返回的不是334就要检查是什么错了,要看错误代码才知道了
{
fclose($this->fp);
return false;
}
if(!$this->do_command($this->lastact,"334"))
~~~~你确定是这句do_command得到的结果是错的吗? 如果是这句出错,应该是跟smtp的服务器或其它原因的问题有关.
还有.你所提供的代码并没有给出具体的实现,而且也没有说明出错的具体代码,所以我是默认其它部分没有问题才有这个结论的.
<?
Class Send_mail
{
var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在HELO后面,欢迎用户
var $debug; //是否显示调试信息
var $smtp; //smtp服务器
var $port; //smtp端口号
var $fp; //socket句柄
var $auth=true;
var $user;
var $passwd;
function send_mail($smtp,$auth,$authuser,$authpasswd,$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='<< ';
}
else
$m='>> ';
if(!ereg("\n$", $message))
$message .= "<br>";
$message=nl2br($message);
echo "<font color=#999999>${m}${message}</font>";
}
}function do_command($command, $code)
{
$this->lastact=$command;
$this->show_debug($this->lastact, "out");
fputs ( $this->fp, $this->lastact );
/*$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");*/
while(true)
{
$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");
if(($this->lastmessage[3]==' ') or (empty($this->lastmessage)))
break;
}
if(!ereg("^$code", $this->lastmessage))
{
return false;
}
else
return true;
}
function send( $to,$from,$subject,$message)
{ //连接服务器
$this->lastact="connect"; $this->show_debug("Connect to ESMTP server : ".$this->smtp, "out");
$this->fp = fsockopen ( $this->smtp, $this->port );
if ( $this->fp )
{ set_socket_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->auth)
{
$this->lastact="EHLO ";
}
else
$this->lastact="HELO ";
$this->lastact=$this->lastact.$this->welcome . "\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
if($this->auth)
{
$this->lastact="AUTH PLAIN "."\n";
if(!$this->do_command($this->lastact,"334"))
{
echo "chucuozaizheli";
fclose($this->fp);
return false;
}
//回传用户名,用base64编码
$this->user="<NUL>".$this->user."<NUL>".$this->passwd;
$this->lastact=base64_encode($this->user)."\n";
//echo $this->lastact;
if(!$this->do_command($this->lastact, "334"))
{
fclose($this->fp);
return false;
} //回传口令,用base64编码
//$this->lastact=base64_encode($this->passwd)."\n";
//if(!$this->do_command($this->lastact, "235"))
//{
//fclose($this->fp);
//return false;
//}
} $this->lastact="MAIL FROM: $from" . "\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
} $this->lastact="RCPT TO: $to" . "\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
//发送正文
$this->lastact="DATA\n";
if(!$this->do_command($this->lastact, "354"))
{
fclose($this->fp);
return false;
} //处理Subject头
$head="Subject: $subject\n";
if(!empty($subject) && !ereg($head, $message))
{
$message = $head.$message;
}
//处理From头
$head="From: $from\n";
if(!empty($from) && !ereg($head, $message))
{
$message = $head.$message;
} //处理To头
$head="To: $to\n";
if(!empty($to) && !ereg($head, $message))
{
$message = $head.$message;
} //加上结束串
if(!ereg("\n\.\n", $message))
$message .= "\n.\n";
$this->show_debug($message, "out");
fputs($this->fp, $message); $this->lastact="QUIT\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
}
return true;
}
else
{
$this->show_debug("Connect failed!", "in");
return false;
}
}
} $email="你好,很高兴遇到你。!!!";
$sendmail=new send_mail("mail.hj888.net",true, "[email protected]", "passwd", "hello", true); //显示调示信息
if($sendmail->send("[email protected]", "[email protected]", "test", $email))
{
echo "发送成功!<br>";
}
else
{
echo "发送失败!<br>";
}
?>
不过现在这样看来,起码发现了两个问题,不知道有没有弄错:1. 构造函数send_mail()中.没有对this->user和this->passwd赋值,这样显示是无法通过认证的2. 一般来说认证的用户应该是[email protected]中的xxx是用户名,所以
new send_mail("mail.hj888.net",true, "[email protected]", "passwd", "hello", true);
中, [email protected]可能应该只写成user不过关键还是因为第一点吧.我回家了再仔细看看
>> Connect to ESMTP server : mail.hj888.net
<< 220 mail.bizcn.com ESMTP Sendmail 8.11.6/8.11.0; Mon, 28 Jul 2003 21:46:50 +0800
>> EHLO hello
<< 250-mail.bizcn.com Hello [218.12.187.113], pleased to meet you
<< 250-ENHANCEDSTATUSCODES
<< 250-EXPN
<< 250-VERB
<< 250-8BITMIME
<< 250-SIZE
<< 250-DSN
<< 250-ONEX
<< 250-ETRN
<< 250-XUSR
<< 250-AUTH GSSAPI LOGIN PLAIN
<< 250 HELP
>> AUTH PLAIN
<< 334
>> PE5VTD53ZWJtYXN0ZXJAaGo4ODgubmV0PE5VTD4xMjM0NTY=
<< 500 5.7.0 authentication failed
发送失败!
也老是出现连接不上服务器。
我在这里把做的改动说一下吧. 下一篇我再把修改后的代码贴一下.1. 在sendmail()函数最后加上两句,
$this->user = $authuser;
$this->passwd = $authpasswd; 因为原来的程序没有对这两个变量赋值.
2. 把send()函数中的
$this->lastact="AUTH PLAIN "."\n";
改为:
$this->lastact="AUTH LOGIN "."\n";
因为我们用的认证方式是LOGIN, 不是PLAIN,
这也是Outlook和我们目前大多mail server用的认证方式3. 把send()函数中的
$this->user="<NUL>".$this->user."<NUL>".$this->passwd;
一句去掉, 不知道是不是PLAIN要这样, 反正LOGIN方式是不用的.另外有几个提醒的地方:
$sendmail = new send_mail("smtp.163.com", true, "fishchen163", "xxxx", "127.0.0.1", true); //显示调示信息
这一句里,
fishchen163是我的用户名, (邮箱是fishchen163 AT 163.com, 不需要@及后边的)
xxxx就是我的密码啦..当然不能告诉你...xixi
127.0.0.1是我自报的来源地址, 因为我的主机做gethostbyaddr()时反解不出我的IP, :P,
而刚好163.com少了这个就会报错.... 有的主机,如21cn.com没问题
Class Send_mail
{
var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在HELO后面,欢迎用户
var $debug; //是否显示调试信息
var $smtp; //smtp服务器
var $port; //smtp端口号
var $fp; //socket句柄
var $auth=true;
var $user;
var $passwd; function send_mail($smtp, $auth, $authuser, $authpasswd, $welcome="", $debug=false)
{
if(empty($smtp)) die("SMTP cannt be NULL!");
if(empty($welcome)) {
@$this->welcome = gethostbyaddr("localhost");
} else {
$this->welcome = $welcome;
} //$this->welcome = "61.144.106.10";
$this->smtp = $smtp;
$this->auth = $auth;
$this->user = $authuser;
$this->passwd = $authpasswd;
$this->debug = $debug;
$this->lastmessage = "";
$this->lastact = "";
$this->port = "25";
}
function show_debug($message, $inout)
{
if ($this->debug) {
if($inout=="in") { //响应信息
$m='<< ';
} else {
$m='>> ';
}
if(!ereg("\n$", $message))
$message .= "<br>";
$message=nl2br($message);
echo "<font color=#999999>${m}${message}</font>";
}
} function do_command($command, $code)
{
$this->lastact = $command;
$this->show_debug($this->lastact, "out");
fputs ( $this->fp, $this->lastact );
/*
$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");
*/
while(true) {
$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");
if(($this->lastmessage[3]==' ') or (empty($this->lastmessage)))
break;
} //while if(!ereg("^$code", $this->lastmessage)) {
return false;
} else {
return true;
}
} function send( $to,$from,$subject,$message)
{ //连接服务器
$this->lastact="connect"; $this->show_debug("Connect to ESMTP server : ".$this->smtp, "out");
$this->fp = fsockopen ( $this->smtp, $this->port );
if ( $this->fp ) { set_socket_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->auth) {
$this->lastact="EHLO ";
} else {
$this->lastact="HELO ";
} $this->lastact=$this->lastact.$this->welcome . "\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
}
if($this->auth) {
//$this->lastact="AUTH PLAIN "."\n";
$this->lastact="AUTH LOGIN "."\n";
if(!$this->do_command($this->lastact, "334")) {
fclose($this->fp);
return false;
} //回传用户名,用base64编码
//$this->user="<NUL>".$this->user."<NUL>".$this->passwd;
$this->lastact=base64_encode($this->user)."\n"; //echo $this->lastact;
if(!$this->do_command($this->lastact, "334")) {
fclose($this->fp);
return false;
} //回传口令,用base64编码
$this->lastact=base64_encode($this->passwd)."\n";
if(!$this->do_command($this->lastact, "235")) {
fclose($this->fp);
return false;
}
} //if($this->auth) $this->lastact="MAIL FROM: $from" . "\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
} $this->lastact="RCPT TO: $to" . "\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
} //发送正文
$this->lastact="DATA\n";
if(!$this->do_command($this->lastact, "354")) {
fclose($this->fp);
return false;
} //处理Subject头
$head="Subject: $subject\n";
if(!empty($subject) && !ereg($head, $message)) {
$message = $head.$message;
} //处理From头
$head="From: $from\n";
if(!empty($from) && !ereg($head, $message)) {
$message = $head.$message;
} //处理To头
$head="To: $to\n";
if(!empty($to) && !ereg($head, $message)) {
$message = $head.$message;
} //加上结束串
if(!ereg("\n\.\n", $message))
$message .= "\n.\n";
$this->show_debug($message, "out");
fputs($this->fp, $message); $this->lastact="QUIT\n";
if(!$this->do_command($this->lastact, "250")) {
fclose($this->fp);
return false;
}
} //if (! ereg ( "^220", $this->lastmessage ) ).. else
return true;
} else { //if ( $this->fp )
$this->show_debug("Connect failed!", "in");
return false;
} //if ( $this->fp )..else
} //function send()
} //Class Send_mail
$sendmail = new send_mail("smtp.163.com", true, "fishchen163", "xxxxxxx", "", true); //显示调示信息if ($sendmail->send("[email protected]", "[email protected]", "title-test", "content-test")) {
echo "发送成功!<br>";
} else {
echo "发送失败!<br>";
}?>
>> Connect to ESMTP server : smtp.163.com
<< 220 Welcome to coremail System(With Anti-Spam) 2.1 for 163.com
>> EHLO 127.0.0.1
<< 250-192.168.1.205
<< 250-PIPELINING
<< 250-SIZE 10240000
<< 250-ETRN
<< 250-AUTH=LOGIN PLAIN
<< 250-AUTH LOGIN PLAIN
<< 250 8BITMIME
>> AUTH LOGIN
<< 334 VXNlcm5hbWU6
>> ZmlzaGNoZW4xNjM=
<< 334 (BASE64-CODE)
>> (BASE64-CODE)
<< 235 Authentication successful
>> MAIL FROM: [email protected]
<< 250 Ok
>> RCPT TO: [email protected]
<< 250 Ok
>> DATA
<< 354 End data with .
>> To: [email protected]
From: [email protected]
Subject: title-test
content-test
.
>> QUIT
<< 250 Ok: queued as 9B8091CAEBCC4
发送成功!
我自己编的程序也是打不开端口,可不可以告诉我一下,谢谢!
信息提示socket连接失败, 请检查你的主机是否可以连上smtp.163.com:25
try:
telnet smtp.163.com 25
我是在win2k下面调试的,不知道是不是win2k不支持这个函数?
怎么回事。我想做一个发附件了,以后,如果有问题,希望你多多帮忙啊,
如果你有QQ的话就好了,我的QQ是39983829