Send an email that is compatible with both Sina cloud and Baidu cloud

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. Currently, only text information can be sent. You can make improvements. You are welcome to share the code for improvement.
Thanks to redspear for helping improve and release new code Class Email {
Public $ config = array (
'Host' => null,
'Port' => 25,
'User' => null,
'Pass' => null,
'From' => null,
'Debug' => false,
'Ishtml '=> false,
'Param' => array ('socket _ create', 'fsockopen '),
'Method' => null,
);

Public function _ set ($ key, $ value ){
$ This-> config [$ key] = $ value;
}

Public function _ get ($ key ){
Return $ this-> config [$ key];
}

Public function _ construct ($ config = array ()){
If (is_array ($ config) $ this-> config = array_merge ($ this-> config, $ config );

If (is_null ($ this-> host) $ this-> host = C ('email _ SMTP ');
If (is_null ($ this-> port) $ this-> port = C ('email _ port ');
If (is_null ($ this-> user) $ this-> user = C ('email _ user ');
If (is_null ($ this-> pass) $ this-> pass = C ('email _ pwd ');
If (is_null ($ this-> from) $ this-> from = C ('email _ from ');

Foreach ($ this-> param as $ index => $ method ){
If (function_exists ($ method )){
$ This-> method = $ index;
$ This-> record ("detection function {$ method} passed ");
Break;
}
}

$ This-> host = gethostbyname ($ this-> host );
$ This-> user = base64_encode ($ this-> user );
$ This-> pass = base64_encode ($ this-> pass );

If (is_null ($ this-> method) $ this-> record ('Emails cannot be sent in the current environment ', true );
}

// Sending Method
Public function send ($ to = '', $ subject ='', $ body = ''){
If (is_null ($ this-> method )){
$ This-> result = false;
Return false;
}

If (! $ To |! $ Subject |! $ Body ){
$ This-> record ('invalid recipient information', true );
$ This-> result = false;
Return false;
}

$ This-> to = $ to; // recipient
$ This-> subject = $ subject; // Email subject
$ This-> body = $ body; // email content

$ Method = $ this-> param [$ this-> method];
If (! Method_exists ($ this, $ method )){
$ This-> record ("the call method {$ method} does not exist", true );
$ This-> result = false;
Return false;
}

$ This-> $ method ();
}

Private function socket_create (){
$ This-> socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ this-> socket ){
$ This-> record ('create SOCKET: '. socket_strerror (socket_last_error ()));
} Else {
$ This-> record ('initialization failed, please check your network connection and parameter ', true );
$ This-> result = false;
Return false;
}
$ Conn = socket_connect ($ this-> socket, $ this-> host, $ this-> port );
If ($ conn ){
$ This-> record ('create a SOCKET connection: '. socket_strerror (socket_last_error ()));
} Else {
$ This-> record ('initialization failed, please check your network connection and parameter ', true );
$ This-> result = false;
Return false;
}
$ This-> record ("server response:". socket_read ($ this-> socket, 1024 )."");

$ This-> handle ();
}

Private function socket_create_call ($ params ){
Socket_write ($ this-> socket, $ params [0], strlen ($ params [0]);
$ This-> record ("client command: {$ params [0]}");
$ Msg = socket_read ($ this-> socket, 1024 );
$ This-> record ("Server Response: {$ msg }");

If (isset ($ params [1]) & strpos ($ msg, $ params [1]) == false ){
$ This-> record ($ params [2], true );
$ This-> result = false;
}
}

// Send the fsockopen Function
Private function fsockopen (){
$ This-> socket = fsockopen ($ this-> host, $ this-> port, $ errno, $ errstr, 60 );
If ($ this-> socket ){
$ This-> record ("create SOCKET connection:". $ this-> host. ':'. $ this-> port );
} Else {
$ This-> record ('initialization failed, please check your network connection and parameter '. $ errstr, true );
$ This-> result = false;
Return false;
}
Stream_set_blocking ($ this-> socket, true );
$ This-> handle ();
}

Private function fsockopen_call ($ params ){
Fputs ($ this-> socket, $ params [0]);
$ This-> record ("client command: {$ params [0]}");
$ Msg = fgets ($ this-> socket, 512 );
$ This-> record ("Server Response: {$ msg }");

If (isset ($ params [1]) & strpos ($ msg, $ params [1]) == false ){
$ This-> record ($ params [2], true );
$ This-> result = false;
}
}

Private function handle (){
$ All = array ();
Array_push ($ all, "From: {$ this-> from} \ r \ n ");
Array_push ($ all, "To: {$ this-> to} \ r \ n ");
Array_push ($ all, "Subject: =? UTF-8? B? ". Base64_encode ($ this-> subject )."? = \ R \ n ");
$ This-> isHtml? Array_push ($ all, "Content-Type: text/html; \ r \ n"): array_push ($ all, "Content-Type: text/plain; \ r \ n "); // mail type html or text
Array_push ($ all, "charset: UTF-8 \ r \ n ");
// Tell the browser that the mail content has been base64 encoded. A group of \ r \ n must be sent to generate a blank line, indicating that the header information has been sent.
Array_push ($ all, "Content-Transfer-Encoding: base64 \ r \ n ");
Array_push ($ all, base64_encode ($ this-> body ));

$ All = implode ('', $ all );

$ Method = $ this-> param [$ this-> method];
$ Call = $ method. '_ call ';

If (! Method_exists ($ this, $ call )){
$ This-> record ("the call method {$ call} does not exist", true );
$ This-> result = false;
Return false;
}

$ Items = array (
Array ("EHLO wangdong \ r \ n "),
Array ("auth login \ r \ n "),
Array ("{$ this-> user} \ r \ n "),
Array ("{$ this-> pass} \ r \ n", '20140901', 'smtp authentication failed '),
Array ("mail from: <{$ this-> from}> \ r \ n", '000000', 'email sending failed '),
Array ("rcpt to: <{$ this-> to}> \ r \ n", '000000', 'email sending failed '),
Array ("DATA \ r \ n", '20140901', 'email sending failed '),
Array ("{$ all} \ r \ n. \ r \ n", '20140901', 'email sending failed '),
Array ("QUIT \ r \ n "),
);

// The following is a session with the server
Foreach ($ items as $ index => $ params ){
$ This-> $ call ($ params );
If ($ this-> result = false) return false;

If ($ index = 0 ){
// Fsockopen must be processed separately
While ($ method = 'fsockopen '){
$ Msg = fgets ($ this-> socket, 512 );
$ This-> record ("Server Response: {$ msg }");
If (substr ($ msg, 3, 1 )! = '-') | Empty ($ msg) break;
}
}
}

If ($ this-> result! = False) $ this-> result = true;
}

// Debug records
Private function record ($ msg, $ save = false ){
If ($ save) $ this-> error = $ msg;
If ($ this-> debug) printf ("

% S

\ N ", $ msg );
}

// Close the socket
Public function _ destruct (){
$ Method = $ this-> param [$ this-> method];
Switch ($ method ){
Case 'socket _ create ':
Socket_close ($ this-> socket );
Break;
Case 'fsockopen ':
Fclose ($ this-> socket );
Break;
}
}
}

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.