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

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

May 25, 2016 pm 05:04 PM

分享个发邮件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;
	}
}

?>
Copy after login

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

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to crawl and process data by calling API interface in PHP project? How to crawl and process data by calling API interface in PHP project? Sep 05, 2023 am 08:41 AM

How to crawl and process data by calling API interface in PHP project? 1. Introduction In PHP projects, we often need to crawl data from other websites and process these data. Many websites provide API interfaces, and we can obtain data by calling these interfaces. This article will introduce how to use PHP to call the API interface to crawl and process data. 2. Obtain the URL and parameters of the API interface. Before starting, we need to obtain the URL of the target API interface and the required parameters.

How to deal with Laravel API error problems How to deal with Laravel API error problems Mar 06, 2024 pm 05:18 PM

Title: How to deal with Laravel API error problems, specific code examples are needed. When developing Laravel, API errors are often encountered. These errors may come from various reasons such as program code logic errors, database query problems, or external API request failures. How to handle these error reports is a key issue. This article will use specific code examples to demonstrate how to effectively handle Laravel API error reports. 1. Error handling in Laravel

Save API data to CSV format using Python Save API data to CSV format using Python Aug 31, 2023 pm 09:09 PM

In the world of data-driven applications and analytics, APIs (Application Programming Interfaces) play a vital role in retrieving data from various sources. When working with API data, you often need to store the data in a format that is easy to access and manipulate. One such format is CSV (Comma Separated Values), which allows tabular data to be organized and stored efficiently. This article will explore the process of saving API data to CSV format using the powerful programming language Python. By following the steps outlined in this guide, we will learn how to retrieve data from the API, extract relevant information, and store it in a CSV file for further analysis and processing. Let’s dive into the world of API data processing with Python and unlock the potential of the CSV format

React API Call Guide: How to interact and transfer data with the backend API React API Call Guide: How to interact and transfer data with the backend API Sep 26, 2023 am 10:19 AM

ReactAPI Call Guide: How to interact with and transfer data to the backend API Overview: In modern web development, interacting with and transferring data to the backend API is a common need. React, as a popular front-end framework, provides some powerful tools and features to simplify this process. This article will introduce how to use React to call the backend API, including basic GET and POST requests, and provide specific code examples. Install the required dependencies: First, make sure Axi is installed in the project

Oracle API Usage Guide: Exploring Data Interface Technology Oracle API Usage Guide: Exploring Data Interface Technology Mar 07, 2024 am 11:12 AM

Oracle is a world-renowned database management system provider, and its API (Application Programming Interface) is a powerful tool that helps developers easily interact and integrate with Oracle databases. In this article, we will delve into the Oracle API usage guide, show readers how to utilize data interface technology during the development process, and provide specific code examples. 1.Oracle

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

How to develop a simple CRUD API using MongoDB How to develop a simple CRUD API using MongoDB Sep 19, 2023 pm 12:32 PM

How to use MongoDB to develop a simple CRUD API In modern web application development, CRUD (Create, Delete, Modify, Check) operations are one of the most common and important functions. In this article, we will introduce how to develop a simple CRUD API using MongoDB database and provide specific code examples. MongoDB is an open source NoSQL database that stores data in the form of documents. Unlike traditional relational databases, MongoDB does not have a predefined schema

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles