For more information about the Scoket code used by phpsmtp to send emails, see.
The code is as follows:
/*
* Php smtp mail Scoket class
* ZhozPhpSmtpSendMail. php
* Created on 2008/09/02 zhoz.com
* @ Author zhoz
* V 1.72 update 2008/09/26 zhoz. Com
*/
Class ZhozPhpSmtpSendMail {
// Your SMTP server supplier, which can be a domain name or IP address
Var $ smtp = "";
// For SMTP, you need to set the authentication value to 1, and do not need to set the authentication value to 0. Currently, most SMTP service providers need to verify the value. if you are not clear, contact your smtp service provider.
Var $ check = 1;
// Your email account name
Var $ username = "";
// Your email password
Var $ password = "";
// This email must be an email on the sending server
Var $ s_from = "";
/*
* Function: Mail initialization settings
* $ From your email sending system is scratching's family?
* $ Password your email password
* $ Smtp your SMTP server supplier, which can be a domain name or IP address
* $ Check SMTP requires authentication. if the value is set to 1, no authentication is required. Currently, most SMTP service providers must verify the value.
*/
Function ZhozPhpSmtpSendMail ($ from, $ password, $ smtp, $ check = 1 ){
If (preg_match ("/^ [^ \ d \-_] [\ w \-] * [^ \-_] @ [^ \-] [a-zA-Z \ d \-] + [^ \-] (\. [^ \-] [a-zA-Z \ d \-] * [^ \-]) * \. [a-zA-Z] {2, 3}/", $ from )){
$ This-> username = substr ($ from, 0, strpos ($ from ,"@"));
$ This-> password = $ password;
$ This-> smtp = $ smtp? $ Smtp: $ this-> smtp;
$ This-> check = $ check;
$ This-> s_from = $ from;
}
}
/*
* Function: send an email.
* $ To target email address
* $ From source email
* $ Subject Mail title
* $ Message Content
*/
Function zhoz_send ($ to, $ from, $ subject, $ message ){
// Connect to the server
$ Fp = fsockopen ($ this-> smtp, 25, $ errno, $ errstr, 60 );
If (! $ Fp) return "failed to connect to the server". _ LINE __;
Set_socket_blocking ($ fp, true );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 220) return "error message 1: $ lastmessage". _ LINE __;
// HELO
$ Yourname = "YOURNAME ";
If ($ this-> check = "1") $ lastact = "EHLO". $ yourname. "\ r \ n ";
Else $ lastact = "HELO". $ yourname. "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 220) return "error message 2: $ lastmessage". _ LINE __;
While (true ){
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 3,1 )! = "-") Or (emptyempty ($ lastmessage )))
Break;
}
// Authentication
If ($ this-> check = "1 "){
// Verification starts
$ Lastact = "auth login". "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334) return "error message 3: $ lastmessage". _ LINE __;
// User name
$ Lastact = base64_encode ($ this-> username). "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334) return "error message 4: $ lastmessage". _ LINE __;
// User password
$ Lastact = base64_encode ($ this-> password). "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = "235") return "error message 5: $ lastmessage". _ LINE __;
}
// FROM:
$ Lastact = "mail from: <". $ this-> s_from. "> \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250) return "error message 6: $ lastmessage". _ LINE __;
//:
$ Lastact = "rcpt to: <". $ to. "> \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250) return "error message 7: $ lastmessage". _ LINE __;
// DATA
$ Lastact = "DATA \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 354) return "error message 8: $ lastmessage". _ LINE __;
// Process the Subject header
$ Head = "Subject: $ subject \ r \ n ";
$ Message = $ head. "\ r \ n". $ message;
// Process the From header
$ Head = "From: $ from \ r \ n ";
$ Message = $ head. $ message;
// Process the To header
$ Head = "To: $ to \ r \ n ";
$ Message = $ head. $ message;
// Add the end string
$ Message. = "\ r \ n. \ r \ n ";
// Send information
Fputs ($ fp, $ message );
$ Lastact = "QUIT \ r \ n ";
Fclose ($ fp );
Error_log ("send over -------> OK !! ", 0 );
Return true;
}
}
?>
/*
* Php smtp mail Scoket class
* ZhozPhpSmtpSendMail. php
* Created on 2008/09/02 zhoz.com
* @ Author zhoz
* V 1.72 update 2008/09/26 zhoz. Com
*/
Class ZhozPhpSmtpSendMail {
// Your SMTP server supplier, which can be a domain name or IP address
Var $ smtp = "";
// For SMTP, you need to set the authentication value to 1, and do not need to set the authentication value to 0. Currently, most SMTP service providers need to verify the value. if you are not clear, contact your smtp service provider.
Var $ check = 1;
// Your email account name
Var $ username = "";
// Your email password
Var $ password = "";
// This email must be an email on the sending server
Var $ s_from = "";
/*
* Function: Mail initialization settings
* $ From the email address on your sending server
* $ Password your email password
* $ Smtp your SMTP server supplier, which can be a domain name or IP address
* $ Check SMTP requires authentication. if the value is set to 1, no authentication is required. Currently, most SMTP service providers must verify the value.
*/
Function ZhozPhpSmtpSendMail ($ from, $ password, $ smtp, $ check = 1 ){
If (preg_match ("/^ [^ \ d \-_] [\ w \-] * [^ \-_] @ [^ \-] [a-zA-Z \ d \-] + [^ \-] (\. [^ \-] [a-zA-Z \ d \-] * [^ \-]) * \. [a-zA-Z] {2, 3}/", $ from )){
$ This-> username = substr ($ from, 0, strpos ($ from ,"@"));
$ This-> password = $ password;
$ This-> smtp = $ smtp? $ Smtp: $ this-> smtp;
$ This-> check = $ check;
$ This-> s_from = $ from;
}
}
/*
* Function: send an email.
* $ To target email address
* $ From source email
* $ Subject Mail title
* $ Message Content
*/
Function zhoz_send ($ to, $ from, $ subject, $ message ){
// Connect to the server
$ Fp = fsockopen ($ this-> smtp, 25, $ errno, $ errstr, 60 );
If (! $ Fp) return "failed to connect to the server". _ LINE __;
Set_socket_blocking ($ fp, true );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 220) return "error message 1: $ lastmessage". _ LINE __;
// HELO
$ Yourname = "YOURNAME ";
If ($ this-> check = "1") $ lastact = "EHLO". $ yourname. "\ r \ n ";
Else $ lastact = "HELO". $ yourname. "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 220) return "error message 2: $ lastmessage". _ LINE __;
While (true ){
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 3,1 )! = "-") Or (empty ($ lastmessage )))
Break;
}
// Authentication
If ($ this-> check = "1 "){
// Verification starts
$ Lastact = "auth login". "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334) return "error message 3: $ lastmessage". _ LINE __;
// User name
$ Lastact = base64_encode ($ this-> username). "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334) return "error message 4: $ lastmessage". _ LINE __;
// User password
$ Lastact = base64_encode ($ this-> password). "\ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = "235") return "error message 5: $ lastmessage". _ LINE __;
}
// FROM:
$ Lastact = "mail from: <". $ this-> s_from. "> \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250) return "error message 6: $ lastmessage". _ LINE __;
//:
$ Lastact = "rcpt to: <". $ to. "> \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250) return "error message 7: $ lastmessage". _ LINE __;
// DATA
$ Lastact = "DATA \ r \ n ";
Fputs ($ fp, $ lastact );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 354) return "error message 8: $ lastmessage". _ LINE __;
// Process the Subject header
$ Head = "Subject: $ subject \ r \ n ";
$ Message = $ head. "\ r \ n". $ message;
// Process the From header
$ Head = "From: $ from \ r \ n ";
$ Message = $ head. $ message;
// Process the To header
$ Head = "To: $ to \ r \ n ";
$ Message = $ head. $ message;
// Add the end string
$ Message. = "\ r \ n. \ r \ n ";
// Send information
Fputs ($ fp, $ message );
$ Lastact = "QUIT \ r \ n ";
Fclose ($ fp );
Error_log ("send over -------> OK !! ", 0 );
Return true;
}
}
?>
The test method is the same.
Run the code to view the code.
/**
* ZhozPhpSmtpSendMail. php
* Created on 2008/09/02 zhoz.com
* Php smtp mail sending class
* Replace mail (): because it reports an error, I don't want
*/
// Configure the mail server information here. it can be used normally if it is not changed:
$ From = 'zhoz @ zhoz.com '; // The email address on your sending server
$ Password = 'zhoz'; // your email password
$ Smtp = 'smtp .zhoz.com '; // your smtp server provider, which can be a domain name or IP address
// Class initialization
$ Zhoz_obj = new ZhozPhpSmtpSendMail ($ from, $ password, $ smtp );
// Start sending
$ Status = $ zhoz_obj-> zhoz_send ("zhoz008@126.com", "zhoz@zhoz.com", "subject is zhoz", "body is zhoz.com ");
If ($ status ){
Echo $ status;
Echo "no ---------";
} Else {
Echo "send OK -----------------! ";
Die;
}
// The result is of course: send OK -----------------!
?>