search
Homephp教程PHP源码分享个发邮件API 便于不支持smtp的虚拟空间使用

分享个发邮件API 便于不支持smtp的虚拟空间使用 

<?php
class email
{
     
//通过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 &#39;1.没有设置好smtp服务&#39;;}
		   		   
		   stream_set_blocking($fp, true);//设置为阻塞模式,此模式读不到数据则会停止在那
		   
		   $mail_return=fgets($fp, 512);//读取512字节内容
		   if(substr($mail_return, 0, 3) != &#39;220&#39;)
		   {return $host.&#39;-2.返回应答码为&#39;.substr($mail_return, 0, 3);}//返回应答码所代表意思请参考&#39;smtp协议.txt&#39;
		   		   
		   
		   fputs($fp, ($auth ? &#39;EHLO&#39; : &#39;HELO&#39;)." ".$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.&#39;-3.返回应答码为&#39;.substr($mail_return, 0, 3);}
		   
		   while(true)
		   {
            $mail_return = fgets($fp, 512);
				if(substr($mail_return, 3, 1) != &#39;-&#39; || 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.&#39;-5.返回应答码为&#39;.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.&#39;-6.返回应答码为&#39;.substr($mail_return, 0, 3).&#39;user=&#39;.$user;}
				
			  fputs($fp, base64_encode($pass)."\r\n");
			  $mail_return=fgets($fp, 512);
			    if(substr($mail_return, 0, 3) != 235)
		        {return $host.&#39;-7.用户验证失败,应答码为&#39;.substr($mail_return, 0, 3);}
		   }
		   
//向服务器添加FROM and TO
//------------------------------------------------------------------------------------------------------------------------
		        fputs($fp, "MAIL FROM: ".$from."\r\n");//有两种格式,MAIL FROM:xxx@xx.com和MAIL FROM: <xxx@xx.com>
		       	$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.&#39;-8.返回应答码为&#39;.substr($mail_return, 0, 3);}
		       	}
								
				foreach(explode(&#39;,&#39;, $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.&#39;-9.返回应答码为&#39;.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.&#39;-10.返回应答码为&#39;.substr($mail_return, 0, 3);}
	
			//邮件内容
			//-----------------------------------------------------------
			     $mail_message           = "From:".$from_name.&#39;<&#39;.$from.">\r\n"; 
			     $mail_message          .= "To:".$to."\r\n"; 
			     $mail_message          .= "Subject:".str_replace("\n",&#39; &#39;,$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;
	}
}

?>

 以上就是分享个发邮件API 便于不支持smtp的虚拟空间使用的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.