In the previous article, the www. oschina. netcodesnippet_1182150_25127 attachment function was not written, but it was reserved. PHP source code and demo: source code? Php *** mail sending class ** supports Sending plain text mails and HTML-format mails, which can be multi-recipient, multi-CC, and multi-secret CC * @ example * $ mailnewMy
Previous upgrade: http://www.oschina.net/code/snippet_1182150_25127 accessories function did not write, just reserved a little position. PHP source code and demo: source code? Php/*** mail sending class * supports Sending plain text mails and HTML-format mails, which can be multi-recipient, multi-CC, and multi-secret CC * @ example * $ mail = new My
Previous upgrade: http://www.oschina.net/code/snippet_1182150_25127
The attachment function is not written, but a bit reserved. PHP
Source code and demo:Source code
SetServer ("XXXXX", "XXXXX @ XXXXX", "XXXXX"); Set smtp server * $ mail-> setFrom ("XXXXX "); set the sender * $ mail-> setReceiver ("XXXXX"); set the recipient, multiple recipients, call multiple times * $ mail-> setCc ("XXXX"); Set CC, multiple CC requests, multiple calls * $ mail-> setBcc ("XXXXX"); Set secret CC, multiple secret CC, call multiple times * $ mail-> setMailInfo ("test ","Test"); Set the subject and content * $ mail-> sendMail (); send */class MySendMail {/*** @ var string mail Transmission proxy userName * @ access private */private $ _ userName; /*** @ var string mail Transmission proxy password * @ access private */private $ _ password; /*** @ var string mail Transmission proxy server address * @ access protected */protected $ _ sendServer; /*** @ var int mail Transmission proxy server port * @ access protected */protected $ _ port = 25; /*** @ var string sender * @ access protected */protected $ _ from;/*** @ var string recipient * @ Access protected */protected $ _ to;/*** @ var string cc * @ access protected */protected $ _ cc; /*** @ var string secretly CC * @ access protected */protected $ _ bcc;/*** @ var string Topic * @ access protected */protected $ _ subject; /*** @ var string message body * @ access protected */protected $ _ body;/*** @ var string attachment * @ access protected */protected $ _ attachment; /*** @ var boolean whether it is a plain text email * @ access protected */protected $ _ isPlain = fals E;/*** @ var reource socket resource * @ access protected */protected $ _ socket; /*** @ var string error message * @ access protected */protected $ _ errorMessage; /*** set the email transmission proxy * @ access public * @ param string $ server Proxy server ip address or domain name * @ param string $ username authentication account * @ param string $ password Authentication password * @ param int $ port the port of the proxy server, smtp default port 25 * @ return boolean */public function setServer ($ server, $ username = "", $ password = "", $ port = 25) {$ this-> _ SendServer = $ server; $ this-> _ port = $ port; if (! Empty ($ username) {$ this-> _ userName = base64_encode ($ username);} if (! Empty ($ password) {$ this-> _ password = base64_encode ($ password);} return true ;} /*** set the sender * @ access public * @ param string $ from sender address * @ return boolean */public function setFrom ($ from) {$ this-> _ from = $ from; return true;}/*** sets the recipient. Multiple recipients are called multiple times in a row. * @ access public * @ param string $ to recipient address * @ return boolean */public function setReceiver ($ to) {if (isset ($ this-> _ )) {if (is_string ($ this-> _ to) {$ this-> _ to = array ($ This-> _ to); $ this-> _ to [] = $ to; return true;} elseif (is_array ($ this-> _ )) {$ this-> _ to [] = $ to; return true;} else {return false ;}} else {$ this-> _ to = $; return true;}/*** sets CC, multiple CC, and multiple consecutive calls. * @ access public * @ param string $ cc address * @ return boolean */public function setCc ($ cc) {if (isset ($ this-> _ cc )) {if (is_string ($ this-> _ cc) {$ this-> _ cc = array ($ this-> _ cc ); $ this-> _ cc [] = $ cc; return true;} elseif (is_arr Ay ($ this-> _ cc) {$ this-> _ cc [] = $ cc; return true;} else {return false ;}} else {$ this-> _ cc = $ cc; return true ;}/ *** sets the secret cc, multiple secret cc, multiple consecutive calls * @ access public * @ param string $ bcc private CC address * @ return boolean */public function setBcc ($ bcc) {if (isset ($ this-> _ bcc) {if (is_string ($ this-> _ bcc )) {$ this-> _ bcc = array ($ this-> _ bcc); $ this-> _ bcc [] = $ bcc; return true ;} elseif (is_array ($ this-> _ bcc) {$ this-> _ bcc [] = $ bcc; retur N true;} else {return false;} else {$ this-> _ bcc = $ bcc; return true ;}} /*** set email information ** @ access public * @ param string $ body Email subject * @ param string $ subject content * @ param boolean $ whether isPlain is a plain text email, default No * @ param string $ attachment, file address * @ return boolean */public function setMailInfo ($ subject, $ body, $ isPlain = false, $ attachment = "") {$ this-> _ subject = $ subject; $ this-> _ body = $ body; $ this-> _ isPlain = $ isPlain; if (! Empty ($ attachment) {$ this-> _ attachment = $ attachment;} return true ;} /*** send an email * @ access public * @ return boolean */public function sendMail () {$ command = $ this-> getCommand (); $ this-> socket (); // print_r ($ command); exit; foreach ($ command as $ value) {if ($ this-> sendCommand ($ value [0], $ value [1]) {continue;} else {return false ;}} $ this-> close (); // In fact, there is no need to close it here. After smtp command: QUIT is sent, the server closes the connection, the local socket resource will automatically release echo 'mail O K! '; Return true;}/*** return error message * @ return string */public function error () {if (! Isset ($ this-> _ errorMessage) {$ this-> _ errorMessage = "";} return $ this-> _ errorMessage ;} /*** return mail command * @ access protected * @ return array */protected function getCommand () {$ command = array ("HELO sendmail \ r \ n ", 250), array ("auth login \ r \ n", 334), array ($ this-> _ userName. "\ r \ n", 334), array ($ this-> _ password. "\ r \ n", 235), array ("mail from: <". $ this-> _ from. "> \ r \ n", 250); // mail header $ header = "MIME-Version: 1.0 \ r \ n"; if ($ this-> _ isPlain) {$ header. = "Content-type: text/plain; charset = UTF-8 \ r \ n";} else {$ header. = "Content-type: text/html; charset = UTF-8 \ r \ n" ;}// set the sender $ header. = "FROM: test <". $ this-> _ from. "> \ r \ n"; // set the recipient if (is_array ($ this-> _ to) {$ count = count ($ this-> _ ); for ($ I = 0; $ I <$ count; $ I ++) {$ command [] = array ("RCPT TO: <". $ this-> _ to [$ I]. "> \ r \ n", 250); $ header. = "TO: <". $ this-> _ to [$ I]. "> \ r \ n" ;}} else {$ command [] = array ("rcpt to: <". $ this-> _. "> \ r \ n", 250); $ header. = "TO: <". $ this-> _. "> \ r \ n" ;}// set to cc if (isset ($ this-> _ cc) {if (is_array ($ this-> _ cc )) {$ count = count ($ this-> _ cc); for ($ I = 0; $ I <$ count; $ I ++) {$ command [] = array ("rcpt to: <". $ this-> _ cc [$ I]. "> \ r \ n", 250); $ header. = "CC: <". $ this-> _ cc [$ I]. "> \ r \ n" ;}} else {$ command [] = array ("rcpt to: <". $ this-> _ cc. "> \ r \ n", 250); $ header. = "CC: <". $ this-> _ cc. "> \ r \ n" ;}}// sets the secret CC if (isset ($ this-> _ bcc) {if (is_array ($ this-> _ bcc )) {$ count = count ($ this-> _ bcc); for ($ I = 0; $ I <$ count; $ I ++) {$ command [] = array ("rcpt to: <". $ this-> _ bcc [$ I]. "> \ r \ n", 250); $ header. = "BCC: <". $ this-> _ bcc [$ I]. "> \ r \ n" ;}} else {$ command [] = array ("rcpt to: <". $ this-> _ bcc. "> \ r \ n", 250); $ header. = "BCC: <". $ this-> _ bcc. "> \ r \ n" ;}}$ header. = "Subject :". $ this-> _ subject. "\ r \ n"; $ body = $ this-> _ body. "\ r \ n. \ r \ n "; $ command [] = array (" DATA \ r \ n ", 354); $ command [] = array ($ header ,""); $ command [] = array ($ body, 250); $ command [] = array ("QUIT \ r \ n", 221); return $ command ;} /*** @ access protected * @ param string $ command the smtp command sent to the server * @ param int $ code do you expect the response from the server * @ param boolean */protected function sendCommand ($ command, $ code) {echo 'send command :'. $ command. ', expected code :'. $ code.'
'; // Send the command to the server try {if (socket_write ($ this-> _ socket, $ command, strlen ($ command ))) {// when the email content is sent multiple times, no $ code is returned, and the server does not return if (empty ($ code) {return true ;} // The read server returns $ data = trim (socket_read ($ this-> _ socket, 1024); echo 'response :'. $ data.'
'; If ($ data) {$ pattern = "/^ ". $ code. "/"; if (preg_match ($ pattern, $ data) {return true;} else {$ this-> _ errorMessage = "Error :". $ data. "| ** | command:"; return false ;}} else {$ this-> _ errorMessage = "Error :". socket_strerror (socket_last_error (); return false ;}} else {$ this-> _ errorMessage = "Error :". socket_strerror (socket_last_error (); return false ;}} catch (Exception $ e) {$ this-> _ errorMessage = "Error :". $ E-> getMessage () ;}}/*** read the content of the attachment file and return the base64 encoded file content * @ access protected * @ return mixed */protected function readFile () {if (isset ($ this-> _ attachment) & file_exists ($ this-> _ attachment) {$ file = file_get_contents ($ this-> _ attachment ); return base64_encode ($ file);} else {return false;}/*** establish a network connection to the server * @ access private * @ return boolean */private function socket () {if (! Function_exists ("socket_create") {$ this-> _ errorMessage = "Extension sockets must be enabled"; return false ;} // create a socket resource $ this-> _ socket = socket_create (AF_INET, SOCK_STREAM, getprotobyname ('tcp '); if (! $ This-> _ socket) {$ this-> _ errorMessage = socket_strerror (socket_last_error (); return false;} socket_set_block ($ this-> _ socket ); // set blocking mode // connect to the server if (! Socket_connect ($ this-> _ socket, $ this-> _ sendServer, $ this-> _ port) {$ this-> _ errorMessage = socket_strerror (socket_last_error ()); return false;} socket_read ($ this-> _ sockets, 1024); return true ;} /*** close socket * @ access private * @ return boolean */private function close () {if (isset ($ this-> _ socket) & is_object ($ this-> _ socket) {$ this-> _ socket-> close (); return true ;} $ this-> _ errorMessage = "No resource can to be close"; return false ;}} /**************************** Test ********** * ***********************/$ mail = new MySendMail (); $ mail-> setServer ("XXXXX", "XXXXX @ XXXXX", "XXXXX"); $ mail-> setFrom ("XXXXX "); $ mail-> setReceiver ("XXXXX"); $ mail-> setReceiver ("XXXXX"); // $ mail-> setCc ("XXXXXX "); // $ mail-> setCc ("XXXXXX"); $ mail-> setBcc ("XXXXX"); $ mail-> setBcc ("XXXXX "); $ mail-> setMailInfo ("test ","Test"); $ Mail-> sendMail ();