SMTP协议-邮件发送PHP程序代码

来源:asp之家 时间:2010-03-24 18:40:00 



classZSMailBox
{
var$fpSocket;
var$strLog;
var$strSMTPServer;
var$strSMTPPort;
var$strFrom;
var$strTo;
functionZSMailBox()
{
$this->strLog="";
$this->strSMTPPort="25";
$this->strFrom="";
$this->strTo="";
}
functionDoCommand($strCommand,$strOKReply)
{
fputs($this->fpSocket,$strCommand);
$strReply=fgets($this->fpSocket,512);
if(!ereg("^$strOKReply",$strReply))
{
returnfalse;
}
returntrue;
}
functionWaitReply($strOKReply)
{
$strReply=fgets($this->fpSocket,512);
$this->strLog.="Reply:$strReply"."
n";
if(!ereg("^$strOKReply",$strReply))
{
returnfalse;
}
returntrue;
}
functionSendData($strSubject,$strContent)
{
$str="To:".$this->strTo.chr(13).chr(10);
$str.="From:".$this->strFrom.chr(13).chr(10);
$str.="Subject:".$strSubject.chr(13).chr(10).chr(13).chr(10);
$str.=$strContent;
$str.=chr(13).chr(10).".".chr(13).chr(10);
fputs($this->fpSocket,$str);
returntrue;
}
functionSendMail($strSubject,$strContent)
{
if($this->VerifySMTPVariables())
{
$this->strLog="";
}else
{
$this->strLog="AnyofSMTPvariablesareinvaild
n";
returnfalse;
}
$this->fpSocket=fsockopen($this->strSMTPServer,$this->strSMTPPort);
if(!$this->fpSocket)
{
$this->strLog.="Cann'topensocket
n";
returnfalse;
}
$this->strLog.="Opensocketsucceed
n";
if(!$this->WaitReply("220"))
{
fclose($this->fpSocket);
returnfalse;
}
$strCommand="HELO".$this->strSMTPServer."n";
if(!$this->DoCommand($strCommand,"250"))
{
$this->strLog.="HELOerror
n";
fclose($this->fpSocket);
returnfalse;
}
$strCommand="MAILFROM:<".$this->strTo.">n";
if(!$this->DoCommand($strCommand,"250"))
{
$this->strLog.="MAILerror
n";
fclose($this->fpSocket);
returnfalse;
}
$strCommand="RCPTTO:<".$this->strTo.">n";
if(!$this->DoCommand($strCommand,"250"))
{
$this->strLog.="Refusetoestablishthechannel
n";
fclose($this->fpSocket);
returnfalse;
}
$strCommand="DATAn";
if(!$this->DoCommand($strCommand,"354"))
{
$this->strLog.="Refusetorecievethedata
n";
fclose($this->fpSocket);
returnfalse;
}
if(!$this->SendData($strSubject,$strContent))
{
$this->strLog.="Senddataerror
n";
fclose($this->fpSocket);
returnfalse;
}
if(!$this->WaitReply("250"))
{
$this->strLog.="Senddataunsuccessful
n";
fclose($this->fpSocket);
returnfalse;
}
$strCommand="QUITn";
if(!$this->DoCommand($strCommand,"221"))
{
$this->strLog.="QUITerror
n";
fclose($this->fpSocket);
returnfalse;
}
fclose($this->fpSocket);
returntrue;
}
functionVerifyMailAddr($strMailAddr)
{
return(eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$strMailAddr));
}
functionVerifyWebSiteAddr($strWebSiteAddr)
{
return(eregi("^([_0-9a-z-]+.)+[a-z]{2,3}$",$strWebSiteAddr));
}
functionVerifySMTPVariables()
{
if(!$this->VerifyWebSiteAddr($this->strSMTPServer))
returnfalse;
if(!isset($this->strSMTPPort))
returnfalse;
if(!$this->VerifyMailAddr($this->strFrom))
returnfalse;
if(!$this->VerifyMailAddr($this->strTo))
returnfalse;
returntrue;
}
functionSetSMTPVariables($strServer,$strPort,$strFrom,$strTo)
{
if(isset($strServer))
$this->strSMTPServer=$strServer;
if(isset($strPort))
$this->strSMTPPort=$strPort;
if(isset($strFrom))
$this->strFrom=$strFrom;
if(isset($strTo))
$this->strTo=$strTo;
}
}
?>
$strSMTPPort="25";
$strTo.="@sina.com";
break;
case2:$strSMTPServer="smtp.163.net";
$strSMTPPort="25";
$strTo.="@163.net";
break;
case3:$strSMTPServer="smtp.yeah.net";
$strSMTPPort="25";
$strTo.="@yeah.net";
break;
case4:$strSMTPServer="smtp.netease.com";
$strSMTPPort="25";
$strTo.="@netease.com";
break;
case5:$strSMTPServer="smtp.sohu.com";
$strSMTPPort="25";
$strTo.="@sohu.com";
break;
case6:$strSMTPServer="smtp.263.net";
$strSMTPPort="25";
$strTo.="@263.net";
break;
default:$strSMTPServer="smtp.sina.com.cn";
$strSMTPPort="25";
$strTo="guestxyz@sina.com";
break;
}
}
if(!isset($strFrom)||($strFrom==""))
{
$strFrom=$strTo;
}
if(!isset($strSubject)||($strSubject==""))
{
$strSubject="Nosubject";
}
if(!isset($strContent)||($strContent==""))
{
$strContent="Nocontent";
}
$zsmb=newZSMailBox;
$zsmb->SetSMTPVariables($strSMTPServer,$strSMTPPort,$strFrom,$strTo);
if($zsmb->SendMail($strSubject,$strContent))
{
printf("OK");
}else
{
printf($zsmb->strLog);
}
}


标签:smtp,邮件,程序,php
0
投稿

猜你喜欢

  • 对于IT类个人博客推广的八条建议

    2008-06-01 15:59:00
  • 什么是rss?rss由来 历史 应用

    2007-10-02 12:33:00
  • google广告竞价的小技巧

    2008-02-27 13:13:00
  • 网站百度收录问题经验分享

    2007-08-16 13:19:00
  • 做点击站必看的相关外语

    2008-07-24 12:26:00
  • 地方门户如何做好搜索引擎优化

    2008-12-24 13:17:00
  • 康盛创想Discuz! 7.1更新 后继版本功能预告发布

    2009-10-31 12:14:00
  • 同城分类网站信息发布管理的几个经验

    2009-01-05 23:34:00
  • QQ注册帐户总数达9.9亿 同时在线突破8000万

    2009-10-12 09:42:00
  • Windows Server 2008创新特性之高可用性

    2009-03-06 17:08:00
  • 草根站长对分类信息的一点想法

    2009-03-05 13:44:00
  • 提高收入Google AdSense优化心得

    2007-08-07 11:17:00
  • 用MDaemon搭建邮件服务器(上)

    2007-11-14 13:35:00
  • 我也谈高校论坛建设经验,同样适合各大类BBS

    2007-08-05 12:46:00
  • 友情链接首看友情 做链接应注重RP而非PR

    2008-12-16 10:07:00
  • google的403错误解密

    2008-01-02 19:25:00
  • 谷歌将向广告主发布免费Web评估工具:AdPlanner

    2008-06-24 11:33:00
  • TOMCAT服务器的内存和连接数配置详解

    2010-04-23 18:14:00
  • Windows 2000 Server安全设置完全手册

    2009-12-20 19:06:00
  • 浅谈cms系统的seo优化

    2008-04-03 15:54:00
  • asp之家 网站运营 m.aspxhome.com