用php实现表单提交发送邮件的功能!

网站技术 qyuef 10年前 (2011-07-21) 1244次浏览 16个评论

这是一个简易的利用第三方邮件系统实现的smtp邮件发送功能。

使用方法:

< ?php
include('smtp_email_class.php');
//定义参数------------------------------------------------------
$host    = 'smtp.qq.com';//smtp服务器地址
$from    = '**@qq.com';//自己的邮件地址
$port    = 25;//端口
$errno   = 0;//错误返回号
$errstr  = '';//错误返回内容
$timeout = 10;//系统运行超时
$auth    = 1;//是否需要 AUTH LOGIN 验证, 1=是, 0=否
$user    = '*****';//smtp服务器用户名
$pass    = '*****';//smtp服务器密码
$from_name='枫叶网络';//联系人名称
//---------------------------------------------------------------
$send_mode=2;
if($_POST["title"]){
$em=new email();//使用类
$em->email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from);
if($em->send_mail_sock($_POST["title"],$_POST["content"],'***@qq.com','枫叶网络','0')){
echo "";
}
}
?>

下面是相关的类文件:复制保存为:smtp_email_class.php

< ?php
//枫叶博客:2011-7-21
//http://www.qyuef.com
class email
{
     function send_mail($to,$subject,$message,$from,$from_name,$mailformat=1)
	 {
	   if(function_exists('mail'))
	   {

       $headers  = 'From: '.$from_name.'<'.$from.'>'."\r\n";
       $headers .= 'TO: '.$to."\r\n";
	   if($mailformat)
	   {$headers .="Content-Type: text/html;\r\n";}
	   else
	   {$headers .="Content-Type: text/plain;\r\n";}
	    $headers .="charset=utf-8\r\n\r\n";


	    $message = str_replace("\r", '', $message);
	    
		$mail_return=@mail($to, str_replace("\n",' ',$subject), $message,$headers);
		
				if(!$mail_return)
				{
				   return $to.'发送不成功';
				}
		
		return 1;
		}
	 }
	 
	function send_win32_mail($to,$subject,$message,$from,$from_name,$host,$port,$mailformat=1)
	{
	  ini_set('SMTP', $host);
	  ini_set('smtp_port', $port);
	  ini_set('sendmail_from', $from);

       $headers  = 'From: '.$from_name.'< '.$from.'>'."\r\n";
       $headers .= 'TO: '.$to."\r\n";
	   if($mailformat)
	   {$headers .="Content-Type: text/html;\r\n";}
	   else
	   {$headers .="Content-Type: text/plain;\r\n";}
	    $headers .="charset=gb2312\r\n\r\n";

	   foreach(explode(',', $to) as $touser)
	    {
		  $touser = trim($touser);
		  if($touser)
		   {
			  $mail_return=@mail($touser, $subject, $message, $headers);
			    if(!$mail_return)
				{
				   return $touser.'发送不成功';
				}
		   }
	    }
		return 1;
	}

//通过sock发送e_mail,不支持附件,
//-------------------------------------------------------------------------------------------------------
	function email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from)//构造函数
	{
		$this->host    = $host;
		$this->port    = $port;
		$this->errno   = $errno;
		$this->errstr  = $errstr;
		$this->timeout = $timeout;
		$this->auth    = $auth;
		$this->user    = $user;
		$this->pass    = $pass;
		$this->from    = $from;
	}
	
	function send_mail_sock($subject,$message,$to,$from_name,$mailformat=0)//邮件标题,邮件内容,收件地址,邮件格式1=text|0=html,默认为0
	{
	   $host    = $this->host;
	   $port    = $this->port;
	   $errno   = $this->errno;
	   $errstr  = $this->errstr;
	   $timeout = $this->timeout;
	   $auth    = $this->auth;
	   $user    = $this->user;
	   $pass    = $this->pass;
	   $from    = $this->from;
	   	   
	   /*
	   1.创建sock,并打开连接
	   2.设置为阻塞模式
	   3.测试smtp应答码是否为220,220代表邮件服务就绪
	   4.发送用户身份验证,由用户设置
	       1=EHLO Host Domain \r\n
		   0=HELO Host Domain \r\n
	   ?.读取服务器端发送给客户端的返回数据
	     smtp.163.com 发送的数据为:
		    250-PIPELINING//流水命令,告诉客户端可以一次发送多个命令来提高速度,在这里PHP
			                并没有使用,因为PHP单个文件的运行还是单线程的
		    250-AUTH LOGIN PLAIN
		    250-AUTH=LOGIN PLAIN
			250 8BITMIME//得到这一行也就是smtp服务器发送结束了,等待客户端发送命令
	   5.发送AUTH LOGIN命令
	   6.发送用户名
	   7.发送密码
	   ?.身份验证过成功后后,
	   8.向服务器添加from
	   9.向服务器添加to
	   10.发送DATA命令,开始输入email数据,以"."号结束
	   11.书写邮件内容
	   12.将邮件内容发送到smtp服务器
	   13.发送QUIT命令,结束会话
	   */  
	   	   $fp = fsockopen($host,$port,$errno,$errstr,$timeout);//打开sock的网络连接
		   if(!$fp){return '1.没有设置好smtp服务';}
		   		   
		   stream_set_blocking($fp, true);//设置为阻塞模式,此模式读不到数据则会停止在那
		   
		   $mail_return=fgets($fp, 512);//读取512字节内容
		   if(substr($mail_return, 0, 3) != '220')
		   {return $host.'-2.返回应答码为'.substr($mail_return, 0, 3);}//返回应答码所代表意思请参考'smtp协议.txt'
		   		   
		   
		   fputs($fp, ($auth ? 'EHLO' : 'HELO')." ".$host."\r\n");//服务器标识用户身份 1=身份验证的标识,0=不需要身份验证的标识
	       $mail_return = fgets($fp, 512);
		   if(substr($mail_return, 0, 3) != 220 && substr($mail_return, 0, 3) != 250)
		   {return $host.'-3.返回应答码为'.substr($mail_return, 0, 3);}
		   
		   while(true)
		   {
            $mail_return = fgets($fp, 512);
				if(substr($mail_return, 3, 1) != '-' || empty($mail_return))
		        {break;}
	       }	   		
		   
		   
		   if($auth)
		   {
		      fputs($fp, "AUTH LOGIN\r\n");
			  $mail_return = fgets($fp, 512);
			    if(substr($mail_return, 0, 3) != 334) 
				{return $host.'-5.返回应答码为'.substr($mail_return, 0, 3);}
				
			  fputs($fp, base64_encode($user)."\r\n");
			  $mail_return = fgets($fp, 512);
			    if(substr($mail_return, 0, 3) != 334) 
				{return $host.'-6.返回应答码为'.substr($mail_return, 0, 3).'user='.$user;}
				
			  fputs($fp, base64_encode($pass)."\r\n");
			  $mail_return=fgets($fp, 512);
			    if(substr($mail_return, 0, 3) != 235)
		        {return $host.'-7.用户验证失败,应答码为'.substr($mail_return, 0, 3);}
		   }
		   
//向服务器添加FROM and TO
//------------------------------------------------------------------------------------------------------------------------
		        fputs($fp, "MAIL FROM: ".$from."\r\n");//有两种格式,MAIL FROM:xxx@xx.com和MAIL FROM: 
		       	$mail_return = fgets($fp, 512);
			   	if(substr($mail_return, 0, 3) != 250)
				{
		       	   	fputs($fp, "MAIL FROM: < ".$from.">\r\n");
		          	$mail_return = fgets($fp, 512);
		   	      	if(substr($mail_return, 0, 3) != 250)
					{return $host.'-8.返回应答码为'.substr($mail_return, 0, 3);}
		       	}
								
				foreach(explode(',', $to) as $mailto)
				{
					$mailto = trim($mailto);
					if($mailto)
					{
						fputs($fp, "RCPT TO: ".$mailto."\r\n");
						$mail_return = fgets($fp, 512);
						if(substr($mail_return, 0, 3) != 250)
						{
							fputs($fp, "RCPT TO: < ".$mailto.">\r\n");
							$mail_return = fgets($fp, 512);
							  if(substr($mail_return, 0, 3) != 250)
					          {return $host.'-9.返回应答码为'.substr($mail_return, 0, 3);}
						}
					}
					
				}
//------------------------------------------------------------------------------------------------------------------------
        	fputs($fp, "DATA\r\n");//开始输入email数据,以"."号结束
        	$mail_return = fgets($fp, 512);
			if(substr($mail_return, 0, 3) != 354)
			{return $host.'-10.返回应答码为'.substr($mail_return, 0, 3);}
	
			//邮件内容
			//-----------------------------------------------------------
			     $mail_message           = "From:".$from_name.'< '.$from.">\r\n"; 
			     $mail_message          .= "To:".$to."\r\n"; 
			     $mail_message          .= "Subject:".str_replace("\n",' ',$subject)."\r\n"; 
			     if($mailformat==1)
				{$mail_message          .= "Content-Type: text/html;\r\n"; }
			     else
				{$mail_message          .= "Content-Type: text/plain;\r\n";} 
			     $mail_message          .= "charset=gb2312\r\n\r\n"; 
			     $mail_message          .= $message; 
				 $mail_message          .= "\r\n.\r\n"; 
		    //-----------------------------------------------------------
			
			fputs($fp,$mail_message);
			fputs($fp,"QUIT\r\n");
			
			return 1;
	}
}
?>

 

喜欢 (1)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(16)个小伙伴在吐槽
  1. 来看看啦 支持下!!!
    狐臭怎么治疗2011-07-23 09:45 回复
  2. 都是代码,晕啊
  3. 这个功能我还没用过。期待回访,来走走心情好一点!
    广州SEO2011-08-07 14:05 回复
  4. 多谢分享
    薇姿隔离霜2011-08-08 10:58 回复
  5. 好东西,作为PHP爱好者,收藏了。
    温哥2011-08-13 00:09 回复
  6. 咱都是直接用插件解决了!
    ixwebhosting2011-08-18 06:56 回复
  7. 楼主,你这段代码写的好全面好详细哟~ 收了,谢谢
    成都网页设计2011-09-08 15:17 回复
  8. 很长的代码啊!
    妙行鞋业2011-09-14 14:45 回复
  9. 代码神马的好麻烦啊
  10. php太给力了 比起asp强多了啊
    滨州房产网2011-10-20 09:44 回复
  11. 很有用但我不特别懂
    国为名茶2011-10-20 11:25 回复
  12. 以前我也做过ASP的,PHP的用的少。
    hostgator2011-10-24 22:29 回复
  13. 支持~部懂
    belstaff2011-10-28 15:00 回复
  14. 支持~
  15. 博主很牛x
    Treadmill Fitness2011-12-03 11:09 回复
  16. 过来看看
    横坐标2012-01-07 23:42 回复