我也知道很乱,这是CSDN造成的,他将tab符号搞的太宽了我的代码与下面的差不多,并与一本"PHP高级开发技术与应用"中的代码差不多。 http://pythonrecord.51.net/bin/content.php3?src=1&url=doc2000082201 http://pythonrecord.51.net/bin/content.php3?src=1&url=doc2001030101我再贴一次代码: sendmail.php <?php if (!defined('SENDMAIL_CLASS_DEFINED')) { define('SENDMAIL_CLASS_DEFINED', 1 ); class sendmail { var $lastmessage; //记录最后返回的响应信息 var $lastact; //最后的动作,字符串形式 var $welcome; //用在HELO后面,欢迎用户
http://pythonrecord.51.net/bin/content.php3?src=1&url=doc2000082201
http://pythonrecord.51.net/bin/content.php3?src=1&url=doc2001030101我再贴一次代码:
sendmail.php
<?php
if (!defined('SENDMAIL_CLASS_DEFINED'))
{
define('SENDMAIL_CLASS_DEFINED', 1 );
class sendmail
{
var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在HELO后面,欢迎用户
var $smtp; //smtp服务器
var $smtp_username;//用户名
var $smtp_password;//用户密码
var $port; //smtp端口号
var $fp; //socket句柄
var $from; //发送者 var $debug; //是否显示调试信息
var $isSendHtml;//是否发送HTML邮件
// 构造类
function sendmail()
{
//使用smtp.21cn.com发送邮件
$this->welcome="Free";
$this->smtp="smtp.21cn.com";
$this->smtp_username="xxxxx";
$this->smtp_password="xxxxx";
$this->from="[email protected]"; $this->debug=false;
$this->isSendHtml=true;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}
function setShowDebug($debug=false)
{
$this->debug=$debug;
}
function setSendHtml($isSendHtml=false)
{
$this->isSendHtml=$isSendHtml;
}
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 );
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,$subject,$message)
{
$intPos=0;
$addressArray=split(",",$to);
for($i=0;$i<count($addressArray);$i++)
{
if($this->checkEmail($addressArray[$i])==true)
{
$addressTo[$intPos]=$addressArray[$i];
$intPos++;
}
}
if($intPos<1)
{
$this->show_debug("收件人邮件地址无效!", "in");
return false;
}
$to="";
//$toEmail="";
for($i=0;$i<count($addressTo);$i++)
{
$to.=$addressTo[$i].",";
}
$to=substr($to,0,Strlen($to)-1);
$from=$this->from;
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$from))
{
$this->show_debug("发件人地址无效!", "in");
return false;
}
$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 );
$this->lastmessage=fgets($this->fp,512);
$this->show_debug($this->lastmessage, "in");
if (! ereg ( "^220", $this->lastmessage ) )
{
return false;
}
else
{
//是否需要身份验证
if((trim($this->smtp_username)=="") || (trim($this->smtp_password)==""))
{
$this->lastact="HELO " . $this->welcome . "\n"; if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
}
else
{
$this->lastact="EHLO " . $this->welcome . "\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
$this->lastact="AUTH LOGIN" . "\n";
if(!$this->do_command($this->lastact, "334"))
{
fclose($this->fp);
return false;
}
//回传用户名,用base64编码
$this->lastact=base64_encode($this->smtp_username) . "\n";
if(!$this->do_command($this->lastact, "334"))
{
fclose($this->fp);
return false;
}
//回传口令,用base64编码
$this->lastact=base64_encode($this->smtp_password) . "\n";
if(!$this->do_command($this->lastact, "235"))
{
fclose($this->fp);
return false;
}
$this->show_debug("认证成功可以发送邮件了\n", "out");
}
$this->lastact="MAIL FROM: ".$from . "\n";
if(!$this->do_command($this->lastact, "250"))
{
fclose($this->fp);
return false;
}
for($i=0;$i<count($addressTo);$i++)
{
$toEmail="<".$addressTo[$i].">";
$this->lastact="RCPT TO: $toEmail\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;
}
//处理邮件格式(文本或HTML)
if($this->isSendHtml)
$head="Content-Type: text/html; charset=gb2312\n";
else
$head="Content-type: text/plan; charset=gb2312\n";
$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;
}
}
function checkEmail($inAddress)
{
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$inAddress))
{
return false;
}
else
return true;
}
}
}
?>test.php
<?
include "sendmail.php";
$message="Hello! This is a test! GO GO";
$sendmail=new sendmail();
$sendmail->setShowDebug(true);//是否调试
//$to,$subject,$message
if($sendmail->send("[email protected]", "test chinese", $message))
echo "发送成功!<br>";
else
echo "发送失败!<br>";
?>