/**
*邮件发送类
*/
classMail
{
//SMTP服务器名称
private$SmtpHost;
/*SMTP服务端口
+标准服务端口,默认为25
*/
private$SmtpPort=25;
//SMTP用户名
private$SmtpUser='';
//SMTP用户密码
private$SmtpPassword='';
/*超时时间
+用于fsockopen()函数,超过该时间未连上则中断
*/
private$TimeOut=30;
/*用户身份
+用于HELO指令
*/
private$HostName='localhost';
/*开启调试模式*/
private$Debug=false;
/*是否进行身份验证*/
private$Authentication=false;
/*PrivateVariables*/
private$Socket=false;
/**
*构造方法
*/
publicfunction__construct($smtpHost='',$smtpUser='',$smtpPassword='',$authentication=false,$smtpPort=25)
{
$this->SmtpHost=$smtpHost;
$this->SmtpPort=$smtpPort;
$this->SmtpUser=$smtpUser;
$this->SmtpPassword=$smtpPassword;
$this->Authentication=$authentication;
}
/**发送邮件
*@paramstring$maiTo收件人
*@paramstring$mailFrom发件人(支持名称:Email)
*@paramstring$subject主题
*@paramstring$body内容
*@paramstring$mailType邮件类型
*@paramstring$cc抄送邮件地址
*@paramstring$bcc隐藏抄送邮件地址
*@paramstring$additionalHeaders附加内容
*@returnboolean
*/
publicfunctionSendMail($maiTo,$mailFrom,$subject='',$body='',$mailType='HTML',$cc='',$bcc='',$additionalHeaders='')
{
$header='';
$header.="MIME-Version:1.0\r\n";
if($mailType=='HTML'){
$header.="Content-Type:text/html;";
}
$header.="charset='utf-8'\r\n";
$header.="Content-Language='zh-cn'\r\n";
$header.="To:".$maiTo."\r\n";
if($cc!=''){
$header.="Cc:".$cc."\r\n";
}
$header.="From:".$mailFrom."\r\n";
$header.="Subject:=?UTF-8?B?".base64_encode($subject)."?=\r\n";
$header.=$additionalHeaders;
$header.="Date:".date("r")."\r\n";
$header.="X-Mailer:ByRedhat(PHP/".phpversion().")\r\n";
list($msec,$sec)=explode('',microtime());
$header.="Message-ID:\r\n";
//收件人地址解析
$maiTo=explode(",",$maiTo);
if($cc!=""){
$maiTo=array_merge($maiTo,explode(",",$cc));
}
if($bcc!=""){
$maiTo=array_merge($maiTo,explode(",",$bcc));
}
//邮件是否发送成功标志
$mailSent=true;
foreach($maiToas$value){
if(!$this->SmtpSockopen($value)){
$this->SmtpDebug("[错误]:无法发送邮件至\"".$value."\"\n");
$mailSent=false;
continue;
}
if($this->SmtpSend($this->HostName,$mailFrom,$value,$header,$body)){
$this->SmtpDebug("[成功]:E-mail已经成功发送至\n");
}else{
$this->SmtpDebug("[失败]:E-mail无法发送至\n");
$mailSent=false;
}
fclose($this->Socket);
//$this->SmtpDebug("[失败]:连接服务器失败\n");
}
$this->SmtpDebug($header);
return$mailSent;
}
/**
*发送邮件
*@param$helo
*@param$maiFrom
*@param$maiTo
*@param$header
*@paramstring$body
*@returnbool
*/
functionSmtpSend($helo,$maiFrom,$maiTo,$header,$body="")
{
//发送连接命令
if(!$this->SmtpPutcmd("HELO",$helo)){
return$this->SmtpError("发送HELO命令");
}
//身份验证
if($this->Authentication){
if(!$this->SmtpPutcmd("AUTHLOGIN",base64_encode($this->SmtpUser))){
return$this->SmtpError("发送HELO命令");
}
if(!$this->SmtpPutcmd("",base64_encode($this->SmtpPassword))){
return$this->SmtpError("发送HELO命令");
}
}
//发件人信息
if(!$this->SmtpPutcmd("MAIL","FROM:")){
return$this->SmtpError("发送MAILFROM命令");
}
//收件人信息
if(!$this->SmtpPutcmd("RCPT","TO:")){
return$this->SmtpError("发送RCPTTO命令");
}
//发送数据
if(!$this->SmtpPutcmd("DATA")){
return$this->SmtpError("发送DATA命令");
}
//发送消息
if(!$this->SmtpMessage($header,$body)){
return$this->SmtpError("发送信息");
}
//发送EOM
if(!$this->SmtpEom()){
return$this->SmtpError("发送.[EOM]");
}
//发送退出命令
if(!$this->SmtpPutcmd("QUIT")){
return$this->SmtpError("发送QUIT命令");
}
returntrue;
}
/**发送SMTP命令
*@param$cmd
*@param$arg
*@returnbool
*/
functionSmtpPutcmd($cmd,$arg="")
{
if($arg!=''){
if($cmd==''){
$cmd=$arg;
}else{
$cmd=$cmd.''.$arg;
}
}
fputs($this->Socket,$cmd."\r\n");
$this->SmtpDebug(">".$cmd."\n");
return$this->SmtpOk();
}
/**SMTP错误信息
*@paramstring$string错误信息
*@returnbool
*/
functionSmtpError($string)
{
$this->SmtpDebug("[错误]:在".$string."时发生了错误\n");
returnfalse;
}
/**SMTP信息
*@paramstring$header头部消息
*@paramstring$body内容
*@returnbool
*/
functionSmtpMessage($header,$body)
{
fputs($this->Socket,$header."\r\n".$body);
$this->SmtpDebug(">".str_replace("\r\n","\n".">",$header."\n>".$body."\n>"));
returntrue;
}
/*EOM*/
functionSmtpEom()
{
fputs($this->Socket,"\r\n.\r\n");
$this->SmtpDebug(".[EOM]\n");
return$this->SmtpOk();
}
/*SMTPOK*/
functionSmtpOk()
{
$response=str_replace("\r\n","",fgets($this->Socket,512));
$this->SmtpDebug($response."\n");
if(preg_match("/^[23]/",$response)==0){
fputs($this->Socket,"QUIT\r\n");
fgets($this->Socket,512);
$this->SmtpDebug("[错误]:服务器返回\"".$response."\"\n");
returnfalse;
}
returntrue;
}
/*debug
*@paramstring$message错误消息
*/
privatefunctionSmtpDebug($message)
{
if($this->Debug){
echo$message."
";
}
}
/**网络Socket链接准备
*@paramstring$address邮件地址
*@returnboolean
*/
privatefunctionSmtpSockopen($address)
{
if($this->SmtpHost==''){
return$this->SmtpSockopenMx($address);
}else{
return$this->SmtpSockopenRelay($this->SmtpHost);
}
}
/**获取MX记录
*@paramstring$address邮件地址
*@returnboolean
*/
privatefunctionSmtpSockopenMx($address)
{
$domain=ereg_replace("^.+@([^@]+)$","\\1",$address);
if(!$this->MyCheckdnsrr($domain,'mx')){
$this->SmtpDebug("[错误]:无法找到MX记录\"".$domain."\"\n");
returnfalse;
}
$this->SmtpSockopenRelay($domain);
$this->SmtpDebug('[错误]:无法连接MX主机('.$domain.")\n");
returnfalse;
}
/**打开网络Socket链接
*@paramstring$smtpHost服务器名称
*@returnboolean
*/
privatefunctionSmtpSockopenRelay($smtpHost)
{
$this->SmtpDebug('[操作]:尝试连接"'.$smtpHost.':'.$this->SmtpPort."\"\n");
$this->Socket=@stream_socket_client('tcp://'.$smtpHost.':'.$this->SmtpPort,$errno,$errstr,$this->TimeOut);
if(!($this->Socket&&$this->SmtpOk())){
$this->SmtpDebug('[错误]:无法连接服务器"'.$smtpHost."\n");
$this->SmtpDebug('[错误]:'.$errstr."(".$errno.")\n");
returnfalse;
}
$this->SmtpDebug('[成功]:成功连接'.$smtpHost.':'.$this->SmtpPort."\"\n");
returntrue;;
}
/**自定义邮箱有效性验证
*+解决window下checkdnsrr函数无效情况
*@paramstring$hostName主机名
*@paramstring$recType类型(可以是MX、NS、SOA、PTR、CNAME或ANY)
*@returnboolean
*/
functionMyCheckdnsrr($hostName,$recType='MX')
{
if($hostName!=''){
$recType=$recType==''?'MX':$recType;
exec("nslookup-type=$recType$hostName",$result);
foreach($resultas$line){
if(preg_match("/^$hostName/",$line)>0){
returntrue;
}
}
returnfalse;
}
returnfalse;
}
}
//邮件配置
$title='xxx';
$sm=newMail('xxx.xxx.xxx','xxx','xxx',true);
$sendTo="xxx@xxx.xxx";
$end=$sm->SendMail($sendTo,'xxx@xxx.xxx',$title,$content);
if($end)echo$end;
elseecho"发送成功!";