PHP mail class [full version]

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. Supports Sending plain text emails and HTML emails, including multiple recipients, multiple CC, and multiple confidential CC, emails with attachments.
/**
* Mail sending class
* Supports text-only and HTML-format mails, including multiple recipients, multi-CC, multi-secret CC, and attachments.
* Required php extensions: sockets and Fileinfo.
* @ Example
* $ Mail = new MySendMail ();
* $ Mail-> setServer ("XXXXX", "XXXXX @ XXXXX", "XXXXX"); set the smtp server
* $ Mail-> setFrom ("XXXXX"); set the sender
* $ Mail-> setReceiver ("XXXXX"); sets the recipient, multiple recipients, and calls
* $ Mail-> setCc ("XXXX"); Sets CC, multiple CC, and multiple calls
* $ Mail-> setBcc ("XXXXX"); sets private CC, multiple private CC, and multiple calls
* $ Mail-> setMailInfo ("test ","Test"); Set the subject and content of the email.
* $ Mail-> sendMail (); send
*/
Class MySendMail {
/**
* @ Var string mail Transmission proxy Username
* @ Access private
*/
Private $ _ userName;

/**
* @ Var string the email transmission proxy Password
* @ Access private
*/
Private $ _ password;

/**
* @ Var string address of the mail Transmission Proxy Server
* @ Access protected
*/
Protected $ _ sendServer;

/**
* @ Var int port of the mail Transmission Proxy Server
* @ Access protected
*/
Protected $ _ port = 25;

/**
* @ Var string sender
* @ Access protected
*/
Protected $ _ from;

/**
* @ Var string recipient
* @ Access protected
*/
Protected $ _;

/**
* @ Var string CC
* @ Access protected
*/
Protected $ _ cc;

/**
* @ Var string private CC
* @ Access protected
*/
Protected $ _ bcc;

/**
* @ Var string topic
* @ Access protected
*/
Protected $ _ subject;

/**
* @ Var string body of the email
* @ 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;

/**
* Set the mail Transmission proxy. If it is a server that can send emails anonymously, you only need to pass the proxy server address.
* @ Access public
* @ Param string $ ip address or domain name of the server Proxy server
* @ Param string $ username: authenticated account
* @ Param string $ password Authentication password
* @ Param int $ port indicates the port of the proxy server. smtp defaults to 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 recipients. Multiple recipients are called multiple times in a row.
* @ Access public
* @ Param string $ to recipient address
* @ Return boolean
*/
Public function setReceiver ($ ){
If (isset ($ this-> _ )){
If (is_string ($ this-> _ )){
$ This-> _ to = array ($ this-> _ );
$ This-> _ to [] = $;
Return true;
}
Elseif (is_array ($ this-> _ )){
$ This-> _ to [] = $;
Return true;
}
Else {
Return false;
}
}
Else {
$ This-> _ to = $;
Return true;
}
}

/**
* Sets CC and multiple CC requests, which are called multiple times in a row.
* @ 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_array ($ this-> _ cc )){
$ This-> _ cc [] = $ cc;
Return true;
}
Else {
Return false;
}
}
Else {
$ This-> _ cc = $ cc;
Return true;
}
}

/**
* Sets the private cc. Multiple private CC messages are called multiple times in a row.
* @ 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;
Return true;
}
Else {
Return false;
}
}
Else {
$ This-> _ bcc = $ bcc;
Return true;
}
}

/**
* Set email information
* @ Access public
* @ Param string $ body subject
* @ Param string $ content of the subject, which can be plain text or 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 here. The smtp command: After QUIT is sent, the server closes the connection and the local socket resources are automatically released.
$ This-> close ();
Echo 'mail OK! ';
Return true;
}

/**
* Error message returned
* @ 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 (
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 the sender
$ Header = "FROM: test <". $ this-> _ from. "> \ r \ n ";

// Set the recipient
If (is_array ($ this-> _ )){
$ 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-> _ to. "> \ r \ n", 250 );
$ Header. = "TO: <". $ this-> _ to. "> \ r \ n ";
}

// Set 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 ";
}
}

// Set 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 the email header containing the attachment as this
$ Header. = "Content-Type: multipart/mixed; \ r \ n ";
}
Elseif (false ){
// Declare the email body containing image resources
$ Header. = "Content-Type: multipart/related; \ r \ n ";
}
Else {
// An html or plain text email is declared as this
$ 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 ";

// 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 $ smtp command sent by command to the server
* @ Param int $ code: Do you want the server to return a response?
* @ Return boolean
*/
Protected function sendCommand ($ command, $ code ){
Echo 'send command: '. $ command.', expected code: '. $ code .'
';
// Send a 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;
}

// Read the Server Response
$ 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 MIME type of the attachment.
* @ Access protected
* @ Return mixed
*/
Protected function getMIMEType (){
If (isset ($ this-> _ attachment) & file_exists ($ 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;
}
}

/**
* 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); // sets the 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-> _ socket, 1024 );

Return true;
}

/**
* Disable 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 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 ();

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.