Php mail class, php mail _ PHP Tutorial

Source: Internet
Author: User
Php mail, php mail. Php mail class: php Mail is used to write a class for sending mails using phpsocket, which is easy to use. When php programs are used to send mails, in the 163 server, you can use the RCPT command to check php mail and php mail.

Write a class for sending emails using php socket, which is easy to use. when sending emails using php programs,

On the 163 server, the RCPT command can also be used to verify whether the 163 mailbox exists,

I haven't thought of it yet.

For more information, see

Class mail {

/**
* Emails can be sent.
*/
Public $ host;
Public $ port;
Public $ user;
Public $ password;
Public $ mail_form;
Public $ rcpt;

Public $ body;

/**
* Attach the information in the header
*/
Public $ to_name;
Public $ from_name;
Public $ subject;
Public $ html;

Public $ connection;

Public $ msg = array ();


// Initialize parameters
Public function _ construct ($ conf, $ rcpt, $ header, $ body, $ html = true ){

Try {

$ This-> host = $ conf ['host'];
$ This-> port = $ conf ['port'];
$ This-> user = $ conf ['user'];
$ This-> password = $ conf ['password'];

$ This-> rcpt = $ rcpt;

$ This-> to_name = $ header ['to _ name'];
$ This-> from_name = $ header ['from _ name'];
$ This-> subject = $ header ['subobject'];
$ This-> html = $ html;

$ This-> body = base64_encode ($ body );

} Catch (Exception $ e ){
Throw new Exception ($ e-> getMessage ());
}

}



Public function connect (){
$ This-> connection = @ fsockopen ($ this-> host, '25', $ errno, $ errstr, 10 );
If (! $ This-> connection ){
Throw new Exception ('connect failed! ');
}
$ Greet = $ this-> callback_code ();

}

Public function helo (){
If ($ this-> is_connect ()&&
$ This-> send_data ('helo'. $ this-> host )&&
$ This-> callback_code () = 250
){
Return true;
} Else {
Throw new Exception ($ this-> get_callback_msg_lastest ());

}
}

Public function send_data ($ cmd ){
If (is_resource ($ this-> connection )){
Return @ fwrite ($ this-> connection, $ cmd. "\ r \ n", strlen ($ cmd) + 2 );
}
}

Public function auth (){
If ($ this-> is_connect ()&&
$ This-> send_data ('auth login') & $ this-> callback_code () = 334 &&
$ This-> send_data (base64_encode ($ this-> user) & $ this-> callback_code () = 334 &&
$ This-> send_data (base64_encode ($ this-> password) & $ this-> callback_code () = 235 ){
Return true;
} Else {
Throw new Exception ($ this-> get_callback_msg_lastest ());
}
}

Public function Mail (){

If ($ this-> is_connect ()&&
$ This-> send_data ("mail from: <$ this-> user> ")&&
$ This-> callback_code () = 250
){
Return true;
} Else {
Throw new Exception ($ this-> get_callback_msg_lastest ());
}
}

Public function is_connect (){
Return is_resource ($ this-> connection );
}

Public function callback_code (){
If ($ this-> is_connect ()){
$ Msg = fgets ($ this-> connection );
If (! Empty ($ msg )){
$ Code = substr ($ msg, 0, strpos ($ msg ,''));
} Else {
Return '';
}
$ This-> msg [] = $ msg;
Return $ code;
}
}

Public function get_callback_msg_lastest (){
Return end ($ this-> msg );
}

Public function rcpt ($ rcpt ){
If ($ this-> is_connect ()&&
$ This-> send_data ("rcpt to: <$ rcpt> ")&&
$ This-> callback_code () = 250
){
Return true;
} Else {
Throw new Exception ($ this-> get_callback_msg_lastest ());
}
}

Public function data (){
If ($ this-> is_connect ()&&
$ This-> send_data ('data ')&&
$ This-> callback_code () = 354 ){
Return true;
} Else {
Throw new Exception ($ this-> get_callback_msg_lastest ());
}
}


Public function send_mail (){

Try {

$ This-> connect ();
$ This-> helo ();
$ This-> auth ();
$ This-> Mail ();

If (is_array ($ this-> rcpt )){
Foreach ($ this-> rcpt as $ rcpt ){
$ This-> rcpt ($ rcpt );
}

} Else {
$ This-> rcpt ($ this-> rcpt );
}

$ This-> data ();

$ Header = str_replace ("\ r \ n ". '. ', "\ r \ n ". '.. ', trim (implode ("\ r \ n", $ this-> get_header ())));
$ Body = str_replace ("\ r \ n". '.', "\ r \ n". ', $ this-> body );
$ Body = substr ($ body, 0, 1) = '.'? '.'. $ Body: $ body;

$ This-> send_data ($ header );
$ This-> send_data ('');
$ This-> send_data ($ body );
$ This-> send_data ('.');

If ($ this-> callback_code ()! = 250 ){
Throw new Exception ('send mail falied! ');
}
Return true;
} Catch (Exception $ e ){
Throw new Exception ($ e-> getMessage ());
}


}



// Only the email address on the same email server can be detected
Public function is_email_exist (){




}


Public function get_header (){

$ Header = array ();
$ Content_type = $ this-> html? 'Text/html; ': 'Text/plain ;';

$ Headers [] = 'date: '. gmdate ('d, j m y h: I: s').' + 100 ';
$ To_mail = is_array ($ this-> rcpt )? Implode (',', $ this-> rcpt): $ this-> rcpt;
$ Headers [] = 'to: "'.' =? Utf8? B? '. Base64_encode ($ this-> to_name ).'? = '.' "<'. $ To_mail.'> ';
$ Headers [] = 'from: "'.' =? Utf8? B? '. Base64_encode ($ this-> from_name ).'? = '.' "<'. $ This-> user.'> ';
$ Headers [] = 'Subject: '.' =? Utf8? B? '. Base64_encode ($ this-> subject ).'? = ';
$ Headers [] = 'content-Type: '. $ content_type. 'charset = utf8; format = flowed ';
$ Headers [] = 'content-Transfer-Encoding: base64 ';
$ Headers [] = 'content-Disposition: inline ';

Return $ headers;
}



}


/**
* Encapsulate classes with functions
*/
Function send_mail ($ conf, $ rcpt, $ header, $ body ){

$ Mail = new mail ($ conf, $ rcpt, $ header, $ body );

If ($ mail-> send_mail ()){
Echo 'send email successfully! ';
} Else {
Echo 'falied! ';
}


}


$ Conf = array (
'Host' => 'smtp .163.com ',
'Port' => '25 ',
'User' => 'z1298703836 @ 163.com ',
'Password' => 'zeiwei123 ',
);

$ Rcpt = array (
'Z1298703836 @ sina.com ',
'Wei. zen@baisonmail.com'
);

$ Header = array (
'To _ name' => 'willhang ',
'From _ name' => 'sinawill ',
'Subobject' => 'tst of reo ',
);

$ Body = 'sfsfsd ';

$ Mail = new mail ($ conf, $ rcpt, $ header, $ body );

$ Mail-> send_mail ();


Echo'

';
Print_r ($ mail-> msg );

Familiar with the Mail sending process, its stmp protocol,

There are also some details of sending, such as: the content to be sent is divided into two parts, one part is in the header, and the other part is the body of the email,

Http://www.bkjia.com/PHPjc/965030.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/965030.htmlTechArticlephp mail class, php mail to write a php socket to send mail class, simple and easy to use, when using php program to send mail, and in the 163 server, you can also verify the code in the RCPT command...

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.