文本版|topic 高级搜索
   名人堂 帮助 论坛制度 意见反馈 | 首页 博客 周新贴 专题 求职 读书
RSS 底部
 
社区导航: 专家门诊   网络技术   操作系统   数据库   程序设计   系统应用   考试认证   CIO及信息化   站长交流   综合交流   下载基地  51CTO产品服务 设为首页 | 收藏本站
51CTO技术论坛» Php » 用SMTP传送邮件时的问题       [ 打印]  [ 订阅]  [ 收藏]  [ 推荐给朋友]   [ 本帖文本页]

论坛跳转:
     
标题: [转载] 用SMTP传送邮件时的问题  ( 查看:233  回复:0 )   
 
sdfsdf107
技术员  点击可查看详细



帖子 65
精华 0
无忧币 345
积分 335
阅读权限 30
注册日期 2007-8-17
最后登录 2007-8-24 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2007-8-20 10:49   标题:用SMTP传送邮件时的问题
上一帖 |
下面是我找到的一个直接使用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


?>



论坛活动:测测你对IT技术大会的了解指数(赠微软礼品、无忧币)
2007-8-20 10:491楼
[ 顶部 ]
     
论坛跳转:  

| | |

标记已读 · 删除论坛Cookies · 文本版 · WAP
 
| 诚征版主 | 版主堂 | 意见建议 | 大史记 | 论坛地图
Copyright©2005-2008 51CTO.COM  Powered by Discuz!
本论坛言论纯属发布者个人意见,不代表51CTO网站立场!如有疑义,请与管理员联系。
京ICP备05051492号