用PHP5写的smtp类,支持身份验证、附件、抄送、暗送

发一个用PHP5写的smtp邮件发送类,支持smtp身份验证,可同时给多人发信,同时支持抄送、暗送、附件等,喜欢的朋友帮我顶起来。这个类需要Fileinfo扩展的支持,有关Fileinfo扩展的更多信息请查看http://www.phpx.com/viewarticle.php?id=115888。

  类文件如下:

<?
/*+--------------------------------------------------+
|文件名:Smtp.php|
|创建人:Simon.Ye|
|创建时间:2006-10-08|
|说  明:smtp邮件发送类|
+--------------------------------------------------+*/

classsmtp
{
//相关属性定义
private$_host;//smtp服务器地址
private$_auth;//是否需要身份验证
private$_user;//smtp帐号
private$_pass;//smtp密码
private$_debug=false;//是否显示调试信息
private$_magicFile;//mime文件所在路径
private$_Subject;//邮件主题
private$_From;//发送人邮箱地址
private$_To=array();//收件人
private$_Cc=array();//抄送
private$_Bcc=array();//暗送
private$_attachment=array();//附件
private$_mailtype='text';//邮件类型('text':纯文本,'html':HTML邮件)
private$_charset='gb2312';//邮件编码
private$_mimemail;//邮件内容
private$_socket;//smtp连接
private$_port=25;//smtp端口
private$_timeout=30;//超时时间

//构造函数
publicfunction__construct($host,$auth=false,$user='',$pass='')
{
$this->_host=$host;
$this->_auth=$auth;
$this->_user=$user;
$this->_pass=$pass;
}

//设置是否显示调试信息
publicfunctionsetDebug($boolDebug)
{
$this->_debug=$boolDebug;
}

//设置mime文件所在路径
publicfunctionsetMagicFile($filename)
{
$this->_magicFile=$filename;
}

//设置邮件主题
publicfunctionsetSubject($str)
{
$this->_Subject=$str;
}

//设置发件人
publicfunctionsetFrom($email)
{
$email=$this->stripComment($email);
$this->_From=$email;
}

//添加收件人
publicfunctionaddTo($email)
{
$email=$this->stripComment($email);
$this->_To[]=$email;
}

//添加抄送人
publicfunctionaddCc($email)
{
$email=$this->stripComment($email);
$this->_Cc[]=$email;
}

//添加暗送人
publicfunctionaddBcc($email)
{
$email=$this->stripComment($email);
$this->_Bcc[]=$email;
}

//添加附件
publicfunctionaddAttachment($filename)
{
if(is_file($filename))$this->_attachment[]=$filename;
}

//设置邮件类型
publicfunctionsetMailType($type)
{
$this->_mailtype=$type;
}

//设置邮件编码
publicfunctionsetCharset($strCharset)
{
$this->_charset=$strCharset;
}

//设置邮件内容
publicfunctionsetMimeMail($str)
{
$boundary=uniqid('');

$this->_mimemail="From:".$this->_From."\r\n";
$this->_mimemail.="Reply-To:".$this->_From."\r\n";
$this->_mimemail.="To:".implode(",",$this->_To)."\r\n";

if(count($this->_Cc))$this->_mimemail.="Cc:".implode(",",$this->_Cc)."\r\n";
if(count($this->_Bcc))$this->_mimemail.="Bcc:".implode(",",$this->_Bcc)."\r\n";

$this->_mimemail.="Subject:".$this->_Subject."\r\n";
$this->_mimemail.="Message-ID:<".time().".".$this->_From.">\r\n";
$this->_mimemail.="Date:".date("r")."\r\n";
$this->_mimemail.="MIME-Version:1.0\r\n";
$this->_mimemail.="Content-type:multipart/mixed;boundary=\"".$boundary."\"\r\n\r\n";
$this->_mimemail.="--".$boundary."\r\n";

if($this->_mailtype=='text')
{
$this->_mimemail.="Content-type:text/plain;charset=\"".$this->_charset."\"\r\n\r\n";
}
elseif($this->_mailtype=='html')
{
$this->_mimemail.="Content-type:text/html;charset=\"".$this->_charset."\"\r\n\r\n";
}

$this->_mimemail.=$str."\r\n\r\n";

if(count($this->_attachment))
{
$finfo=newfinfo(FILEINFO_MIME,$this->_magicFile);
foreach($this->_attachmentas$k=>$filename)
{
$f=@fopen($filename,'r');
if(!$f)continue;

$mimetype=$finfo->file(realpath($filename));
$attachment=@fread($f,filesize($filename));
$attachment=base64_encode($attachment);
$attachment=chunk_split($attachment);

$this->_mimemail.="--".$boundary."\r\n";
$this->_mimemail.="Content-type:".$mimetype.";name=".basename($filename)."\r\n";
$this->_mimemail.="Content-disposition:attachment;filename=".basename($filename)."\r\n";
$this->_mimemail.="Content-transfer-encoding:base64\r\n\r\n";
$this->_mimemail.=$attachment."\r\n\r\n";

unset($f);
unset($mimetype);
unset($attachment);
}
}

$this->_mimemail.="--".$boundary."--";
}

publicfunctionsend()
{
$arrToEmail=$this->_To;
if(count($this->_Cc))$arrToEmail=array_merge($arrToEmail,$this->_Cc);
if(count($this->_Bcc))$arrToEmail=array_merge($arrToEmail,$this->_Bcc);

$this->connect();
$this->sendCMD('HELOlocalhost');
$this->smtpOK();

if($this->_auth)
{
$this->sendCMD('AUTHLOGIN'.base64_encode($this->_user));
$this->smtpOK();
$this->sendCMD(base64_encode($this->_pass));
$this->smtpOK();
}
$this->sendCMD('MAILFROM:<'.$this->_From.'>');
$this->smtpOK();

foreach($arrToEmailas$k=>$toEmail)
{
$this->sendCMD('RCPTTO:<'.$toEmail.'>');
$this->smtpOK();
}

$this->sendCMD('DATA');
$this->smtpOK();
$this->sendCMD($this->_mimemail);
$this->smtpOK();
$this->sendCMD('.');
$this->smtpOK();

$this->disconnect();
}

//连接smtp服务器
privatefunctionconnect()
{
$fp=@fsockopen($this->_host,$this->_port,$errno,$errstr,$this->_timeout);

if(!$fp)
{
if($this->_debug)$this->showMessage('错误:无法与smtp服务器建立连接!');
die();
}
else
{
$this->_socket=$fp;
if($this->_debug)$this->showMessage('正在与smtp服务器建立连接...成功!');
}
}

//关闭smtp服务器连接
privatefunctiondisconnect()
{
$this->sendCMD('QUIT');
@fclose($this->_socket);
$this->_socket=null;
if($this->_debug)$this->showMessage('断开与smtp服务器的连接');
}

//显示信息
privatefunctionshowMessage($msg)
{
echo"[".date("H:i")."]".$msg."<br/>";
}

//发送指令
privatefunctionsendCMD($cmd)
{
@fputs($this->_socket,$cmd."\r\n");
if($this->_debug)$this->showMessage($cmd);
}

//判断指令是否成功
privatefunctionsmtpOK()
{
$res=str_replace("\r\n","",@fgets($this->_socket,512));

if(ereg("^[23]",$res))
{
if($this->_debug)$this->showMessage('请求成功!');
}
else
{
if($this->_debug)$this->showMessage('错误:服务器返回信息<'.$res.'>');
$this->disconnect();
}
}

//过滤邮箱地址中的非法字符
privatefunctionstripComment($email)
{
$comment="\([^()]*\)";

while(ereg($comment,$email))
{
$email=ereg_replace($comment,"",$email);
}

$email=ereg_replace("([\t\r\n])+","",$email);
$email=ereg_replace("^.*<(.+)>.*$","\1",$email);

return$email;
}
}
?>

http://www.corange.cn/archives/2008/02/229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值