Home php教程 php手册 PHP邮件发送类【精简版】

PHP邮件发送类【精简版】

Jun 07, 2016 am 11:42 AM

一般发系统通知之类的邮件,这个版本就足够.
<?php <br /> /**<br> * 邮件发送类<br> * 仅支持发送纯文本和HTML内容邮件<br> * 需要的php扩展,sockets<br> * @example<br> * $mail = new MySendMail();<br> * $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); 设置smtp服务器<br> * $mail->setFrom("XXXXX"); 设置发件人<br> * $mail->setReceiver("XXXXX"); 设置收件人<br> * $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容<br> * $mail->sendMail(); 发送<br> */<br> class MySendMail {<br>     /**<br>     * @var string 邮件传输代理用户名<br>     * @access private<br>     */<br>     private $_userName;<br> <br>     /**<br>     * @var string 邮件传输代理密码<br>     * @access private<br>     */<br>     private $_password;<br> <br>     /**<br>     * @var string 邮件传输代理服务器地址<br>     * @access private<br>     */<br>     private $_sendServer;<br> <br>     /**<br>     * @var int 邮件传输代理服务器端口<br>     * @access protected<br>     */<br>     protected $_port=25;<br> <br>     /**<br>     * @var string 发件人<br>     * @access protected<br>     */<br>     protected $_from;<br> <br>     /**<br>     * @var string 收件人<br>     * @access protected<br>     */<br>     protected $_to;<br> <br>     /**<br>     * @var string 主题<br>     * @access protected<br>     */<br>     protected $_subject;<br> <br>     /**<br>     * @var string 邮件正文<br>     * @access protected<br>     */<br>     protected $_body;<br> <br>     /**<br>     * @var reource socket资源<br>     * @access protected<br>     */<br>     protected $_socket;<br> <br>     /**<br>     * @var string 错误信息<br>     * @access protected<br>     */<br>     protected $_errorMessage;<br> <br>     /**<br>     * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行<br>     * @access public<br>     * @param string $server 代理服务器的ip或者域名<br>     * @param string $username 认证账号<br>     * @param string $password 认证密码<br>     * @param int $port 代理服务器的端口,smtp默认25号端口<br>     * @return boolean<br>     */<br>     public function setServer($server, $username="", $password="", $port=25) {<br>         $this->_sendServer = $server;<br>         $this->_port = $port;<br>         if(!empty($username)) {<br>             $this->_userName = base64_encode($username);<br>         }<br>         if(!empty($password)) {<br>             $this->_password = base64_encode($password);<br>         }<br>         return true;<br>     }<br> <br>     /**<br>     * 设置发件人<br>     * @access public<br>     * @param string $from 发件人地址<br>     * @return boolean<br>     */<br>     public function setFrom($from) {<br>         $this->_from = $from;<br>         return true;<br>     }<br> <br>     /**<br>     * 设置收件人<br>     * @access public<br>     * @param string $to 收件人地址<br>     * @return boolean<br>     */<br>     public function setReceiver($to) {<br>         $this->_to = $to;<br>         return true;<br>     }<br> <br>     /**<br>     * 设置邮件信息<br>     * @access public<br>     * @param string $body 邮件主题<br>     * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本<br>     * @return boolean<br>     */<br>     public function setMailInfo($subject, $body) {<br>         $this->_subject = $subject;<br>         $this->_body = base64_encode($body);<br>         if(!empty($attachment)) {<br>             $this->_attachment = $attachment;<br>         }<br>         return true;<br>     }<br> <br>     /**<br>     * 发送邮件<br>     * @access public<br>     * @return boolean<br>     */<br>     public function sendMail() {<br>         $command = $this->getCommand();<br>         $this->socket();<br> <br>         foreach ($command as $value) {<br>             if($this->sendCommand($value[0], $value[1])) {<br>                 continue;<br>             }<br>             else{<br>                 return false;<br>             }<br>         }<br> <br>         //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放<br>         $this->close(); <br>         echo 'Mail OK!';<br>         return true;<br>     }<br> <br>     /**<br>     * 返回错误信息<br>     * @return string<br>     */<br>     public function error(){<br>         if(!isset($this->_errorMessage)) {<br>             $this->_errorMessage = "";<br>         }<br>         return $this->_errorMessage;<br>     }<br> <br>     /**<br>     * 返回mail命令<br>     * @access protected<br>     * @return array<br>     */<br>     protected function getCommand() {<br>         $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符<br> <br>         $command = array(<br>                 array("HELO sendmail\r\n", 250)<br>             );<br>         if(!empty($this->_userName)){<br>             $command[] = array("AUTH LOGIN\r\n", 334);<br>             $command[] = array($this->_userName . "\r\n", 334);<br>             $command[] = array($this->_password . "\r\n", 235);<br>         }<br> <br>         //设置发件人<br>         $command[] = array("MAIL FROM: _from . ">\r\n", 250);<br>         $header = "FROM: _from . ">\r\n";<br> <br>         //设置收件人<br>         $command[] = array("RCPT TO: _to . ">\r\n", 250);<br>         $header .= "TO: _to . ">\r\n";<br> <br>         $header .= "Subject: " . $this->_subject ."\r\n";<br>         $header .= "Content-Type: multipart/alternative;\r\n";<br> <br>         //邮件头分隔符<br>         $header .= "\t" . 'boundary="' . $separator . '"';<br> <br>         $header .= "\r\nMIME-Version: 1.0\r\n";<br>         $header .= "\r\n--" . $separator . "\r\n";<br>         $header .= "Content-Type:text/html; charset=utf-8\r\n";<br>         $header .= "Content-Transfer-Encoding: base64\r\n\r\n";<br>         $header .= $this->_body . "\r\n";<br>         $header .= "--" . $separator . "\r\n";<br> <br>         //结束数据<br>         $header .= "\r\n.\r\n";<br> <br>         $command[] = array("DATA\r\n", 354);<br>         $command[] = array($header, 250);<br>         $command[] = array("QUIT\r\n", 221);<br> <br>         return $command;<br>     }<br> <br>     /**<br>     * 发送命令<br>     * @access protected<br>     * @param string $command 发送到服务器的smtp命令<br>     * @param int $code 期望服务器返回的响应吗<br>     * @return boolean<br>     */<br>     protected function sendCommand($command, $code) {<br>         echo 'Send command:' . $command . ',expected code:' . $code . '<br>';<br>         //发送命令给服务器<br>         try{<br>             if(socket_write($this->_socket, $command, strlen($command))){<br> <br>                 //当邮件内容分多次发送时,没有$code,服务器没有返回<br>                 if(empty($code))  {<br>                     return true;<br>                 }<br> <br>                 //读取服务器返回<br>                 $data = trim(socket_read($this->_socket, 1024));<br>                 echo 'response:' . $data . '<br><br>';<br> <br>                 if($data) {<br>                     $pattern = "/^".$code."/";<br>                     if(preg_match($pattern, $data)) {<br>                         return true;<br>                     }<br>                     else{<br>                         $this->_errorMessage = "Error:" . $data . "|**| command:";<br>                         return false;<br>                     }<br>                 }<br>                 else{<br>                     $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>                     return false;<br>                 }<br>             }<br>             else{<br>                 $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>                 return false;<br>             }<br>         }catch(Exception $e) {<br>             $this->_errorMessage = "Error:" . $e->getMessage();<br>         }<br>     }<br> <br>     /**<br>     * 建立到服务器的网络连接<br>     * @access private<br>     * @return boolean<br>     */<br>     private function socket() {<br>         if(!function_exists("socket_create")) {<br>             $this->_errorMessage = "Extension sockets must be enabled";<br>             return false;<br>         }<br>         //创建socket资源<br>         $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));<br> <br>         if(!$this->_socket) {<br>             $this->_errorMessage = socket_strerror(socket_last_error());<br>             return false;<br>         }<br> <br>         socket_set_block($this->_socket);//设置阻塞模式<br> <br>         //连接服务器<br>         if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {<br>             $this->_errorMessage = socket_strerror(socket_last_error());<br>             return false;<br>         }<br>         socket_read($this->_socket, 1024);<br> <br>         return true;<br>     }<br> <br>     /**<br>     * 关闭socket<br>     * @access private<br>     * @return boolean<br>     */<br>     private function close() {<br>         if(isset($this->_socket) && is_object($this->_socket)) {<br>             $this->_socket->close();<br>             return true;<br>         }<br>         $this->_errorMessage = "No resource can to be close";<br>         return false;<br>     }<br> }<br> <br> /**************************** Test ***********************************/<br> $mail = new MySendMail();<br> $mail->setServer("XXXX", "XXX@XXX", "XXX");<br> $mail->setFrom("XXX@XX");<br> $mail->setReceiver("XXX@XX");<br> $mail->setMailInfo("test", "<b>test</b>");<br> $mail->sendMail();

AD:真正免费,域名+虚机+企业邮箱=0元

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles