PHP mail class: Add attachments for sending

Source: Internet
Author: User
Supports text-only and HTML-formatted mails, including multiple recipients, multiple CC messages, multiple private CC messages, and attachments. Comments are welcome. I am sending an email with an attachment. I have a txt file of 0 B each time. I don't know why! Last article: www. oschina. netcodesnippet_1182150_25162 the code at both ends is provided here,

Supports text-only and HTML-formatted mails, including multiple recipients, multiple CC messages, multiple private CC messages, and attachments. Comments are welcome. I am sending an email with an attachment. I have a txt file of 0 B each time. I don't know why! Previous address: http://www.oschina.net/code/snippet_1182150_25162 here provides code at both ends,

Supports text-only and HTML-formatted mails, including multiple recipients, multiple CC messages, multiple private CC messages, and attachments.
Comments are welcome.

I am sending an email with an attachment. I have a txt file of 0 B each time. I don't know why!

Previous address: http://www.oschina.net/code/snippet_1182150_25162

The code at both ends is provided here. The first part is fully functional. The second part is to send plain text and HTMl emails. Generally, system notification emails are enough! 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 reource socket resource * @ access protected */protected $ _ socket ;/*** @ Var string error message * @ access protected */protected $ _ errorMessage;/*** sets the mail Transmission proxy. If it is a server that can send an email anonymously, you only need to pass the proxy server address * @ 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, which can be plain text, it is also an HTML text * @ param string $ attachment, file address * @ return boolean */public function setMailInfo ($ subject, $ body, $ attachment = "") {$ this-> _ subject = $ subject; $ this-> _ body = base64_encode ($ body); if (! Empty ($ attachment) {$ this-> _ attachment = $ attachment;} return true ;} /*** send an email * @ access public * @ return boolean */public function sendMail () {$ command = $ this-> getCommand (); $ this-> socket (); foreach ($ command as $ value) {if ($ this-> sendCommand ($ value [0], $ value [1]) {continue;} else {return false ;}// in fact, there is no need to close it either. After the smtp command: QUIT is sent, the server closes the connection, local socket resources will be automatically released $ this-> close (); echo 'mail OK! '; 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); if (! Empty ($ this-> _ userName) {$ command [] = array ("auth login \ r \ n", 334 ); $ command [] = array ($ this-> _ userName. "\ r \ n", 334); $ command [] = array ($ this-> _ password. "\ r \ n", 235);} $ command [] = array ("mail from: <". $ this-> _ from. "> \ r \ n", 250); $ separator = "---- = _ Part _". md5 ($ this-> _ from. time ()). uniqid (); // delimiter // set 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); if ($ I = 0) {$ header. = "TO: <". $ this-> _ to [$ I]. ">";} elseif ($ I + 1 = $ count) {$ header. = ", <". $ this-> _ to [$ I]. "> \ r \ n";} else {$ header. = ", <". $ this-> _ to [$ I]. ">" ;}}} 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); if ($ I = 0) {$ header. = "CC: <". $ this-> _ cc [$ I]. ">";} elseif ($ I + 1 = $ count) {$ header. = ", <". $ this-> _ cc [$ I]. "> \ r \ n";} else {$ header. = ", <". $ this-> _ cc [$ I]. ">" ;}}} 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); if ($ I = 0) {$ header. = "BCC: <". $ this-> _ bcc [$ I]. ">";} elseif ($ I + 1 = $ count) {$ header. = ", <". $ this-> _ bcc [$ I]. "> \ r \ n";} else {$ header. = ", <". $ this-> _ bcc [$ I]. ">" ;}}} else {$ command [] = array ("rcpt to: <". $ this-> _ bcc. "> \ r \ n", 250); $ header. = "BCC: <". $ this-> _ bcc. "> \ r \ n" ;}}$ header. = "Subject :". $ this-> _ subject. "\ r \ n"; if (isset ($ this-> _ attachment) {// declare this $ header as the header containing attachments. = "Content-Type: multipart/mixed; \ r \ n";} elseif (false) {// This $ header must be declared if the email body contains image resources. = "Content-Type: multipart/related; \ r \ n";} else {// html or plain text emails declare this $ header. = "Content-Type: multipart/alternative; \ r \ n";} // email header separator $ header. = "\ t ". 'boundary = "'. $ separator. '"'; $ header. = "\ r \ nMIME-Version: 1.0 \ r \ n"; $ header. = "\ r \ n --". $ separator. "\ r \ n"; $ header. = "Content-Type: text/html; charset = UTF-8 \ r \ n"; $ header. = "Content-Transfer-Encoding: base64 \ r \ n"; $ header. = $ this-> _ body. "\ r \ n"; $ header. = "--". $ separator. "\ r \ n"; // Add the attachment if (isset ($ this-> _ attachment) {$ header. = "\ r \ n --". $ separator. "\ r \ n"; $ header. = "Content-Type :". $ this-> getMIMEType (). '; name = "'. basename ($ this-> _ attachment ). '"'. "\ r \ n"; $ header. = "Content-Transfer-Encoding: base64 \ r \ n"; $ header. = 'content-Disposition: attachment; filename = "'. basename ($ this-> _ attachment ). '"'. "\ r \ n"; $ header. = "\ r \ n"; $ header. = $ this-> readFile (); $ header. = "\ r \ n --". $ separator. "\ r \ n" ;}$ header. = "\ r \ n. \ r \ n "; $ command [] = array (" DATA \ r \ n ", 354); $ command [] = array ($ header, 250 ); $ command [] = array ("QUIT \ r \ n", 221); return $ command ;} /*** send command ** @ access protected * @ param string $ command the smtp command sent to the server * @ param int $ code do you expect the server to return a response * @ return 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;}/*** obtain the attachment MIME type * @ access protected * @ return mixed */protected function getMIMEType () {if (isset ($ this-> _ attachment) & file_e Xists ($ this-> _ attachment) {$ mime = mime_content_type ($ this-> _ attachment); if (! Preg_match ("/gif | jpg | png | jpeg/", $ mime) {$ mime = "application/octet-stream";} return $ mime ;} else {return false ;}}/*** establishes 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 @ XXXXX "); $ mail-> setReceiver ("XXXXX @ XXXXX"); // $ mail-> setReceiver ("XXXXX @ XXXXX "); $ mail-> setCc ("XXXXX @ XXXXX"); $ mail-> setCc ("XXXXX @ XXXXX"); $ mail-> setBcc ("XXXXX @ XXXXX "); $ mail-> setBcc ("XXXXX @ XXXXX"); $ mail-> setBcc ("XXXXX @ XXXXX"); $ mail-> setMailInfo ("test ","Test"," Sms.zip "); $ mail-> sendMail ();
 SetServer ("XXXXX", "XXXXX @ XXXXX", "XXXXX"); Set smtp server * $ mail-> setFrom ("XXXXX "); set the sender * $ mail-> setReceiver ("XXXXX"); set the recipient * $ 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 private */private $ _ sendServer; /*** @ var int mail Transmission proxy server port * @ access protected */protected $ _ port = 25; /*** @ var string sender * @ access protected */protected $ _ from;/*** @ var string recipient * @ acc Ess protected */protected $ _ to;/*** @ var string Topic * @ access protected */protected $ _ subject; /*** @ var string email body * @ access protected */protected $ _ body;/*** @ var reource socket resource * @ access protected */protected $ _ socket; /*** @ var string error message * @ access protected */protected $ _ errorMessage;/*** sets the mail Transmission proxy. If it is a server with an anonymous email, you only need to pass the proxy server address * @ access public * @ param string $ server Proxy server ip address or domain name * @ param string $ usern Ame authentication account * @ param string $ password Authentication password * @ param int $ port proxy server port, 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 ;} /*** set the recipient * @ access public * @ param string $ to the recipient address * @ return boolean */public function setReceiver ($) {$ this-> _ to = $ to; return true;}/*** set email information * @ access public * @ param string $ body Email Subject Subject * @ param string $ content of the subject, which can be plain text or HTML text * @ return boolean */public function setMailInfo ($ subject, $ body) {$ this-> _ subject = $ subject; $ this-> _ body = base64_encode ($ body); if (! Empty ($ attachment) {$ this-> _ attachment = $ attachment;} return true ;} /*** send an email * @ access public * @ return boolean */public function sendMail () {$ command = $ this-> getCommand (); $ this-> socket (); foreach ($ command as $ value) {if ($ this-> sendCommand ($ value [0], $ value [1]) {continue;} else {return false ;}// in fact, there is no need to close it either. After the smtp command: QUIT is sent, the server closes the connection, local socket resources will be automatically released $ this-> close (); echo 'mail OK! '; 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 () {$ separator = "---- = _ Part _". md5 ($ this-> _ from. time ()). uniqid (); // delimiter $ command = array ("HELO sendmail \ r \ n", 250); if (! Empty ($ this-> _ userName) {$ command [] = array ("auth login \ r \ n", 334 ); $ command [] = array ($ this-> _ userName. "\ r \ n", 334); $ command [] = array ($ this-> _ password. "\ r \ n", 235);} // set the sender $ command [] = array ("mail from: <". $ this-> _ from. "> \ r \ n", 250); $ header = "FROM: <". $ this-> _ from. "> \ r \ n"; // set the recipient $ command [] = array ("rcpt to: <". $ this-> _. "> \ r \ n", 250); $ header. = "TO: <". $ this-> _. "> \ r \ n"; $ header. = "Subject :". $ this-> _ subject. "\ r \ n"; $ header. = "Content-Type: multipart/alternative; \ r \ n"; // mail header separator $ header. = "\ t ". 'boundary = "'. $ separator. '"'; $ header. = "\ r \ nMIME-Version: 1.0 \ r \ n"; $ header. = "\ r \ n --". $ separator. "\ r \ n"; $ header. = "Content-Type: text/html; charset = UTF-8 \ r \ n"; $ header. = "Content-Transfer-Encoding: base64 \ r \ n"; $ header. = $ this-> _ body. "\ r \ n"; $ header. = "--". $ separator. "\ r \ n"; // end data $ header. = "\ r \ n. \ r \ n "; $ command [] = array (" DATA \ r \ n ", 354); $ command [] = array ($ header, 250 ); $ command [] = array ("QUIT \ r \ n", 221); return $ command ;} /*** send command ** @ access protected * @ param string $ command the smtp command sent to the server * @ param int $ code do you expect the server to return a response * @ return 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 () ;}}/*** 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 ("XXXX", "XXXXX @ XXXXX", "XXXX"); $ mail-> setFrom ("XXXXX @ XXXXX "); $ mail-> setReceiver ("XXXXX @ XXXXX"); $ mail-> setMailInfo ("test ","Test"); $ Mail-> sendMail ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.