This article mainly introduces php's method of sending SMTP mail based on socket. The example analyzes the principles and skills of php's socket-based smtp mail sending, which has some reference value, for more information, see
This article mainly introduces php's method of sending SMTP mail based on socket. The example analyzes the principles and skills of php's socket-based smtp mail sending, which has some reference value, for more information, see
This example describes how php uses socket to send SMTP mails. Share it with you for your reference. The specific analysis is as follows:
Php uses socket to send emails through SMTP.
Php-sockets extension is used to send plain text and html emails. The Code is as follows:
The Code is as follows:
<? Php
/**
* Mail sending class
* Supports Sending plain text emails and HTML emails.
* @ Example
* $ Config = array (
* "From" => "*****",
* "To" => "***",
* "Subject" => "test ",
* "Body" =>"Test",
* "Username" => "***",
* "Password" => "****",
* "IsHTML" => true
*);
*
* $ Mail = new MySendMail ();
*
* $ Mail-> setServer ("smtp.126.com ");
*
* $ Mail-> setMailInfo ($ config );
* If (! $ Mail-> sendMail ()){
* Echo $ mail-> error ();
* Return 1;
*}
*/
Class MySendMail {
/**
* @ Var mail Transmission proxy Username
* @ Access private
*/
Private $ _ userName;
/**
* @ Var mail Transmission proxy Password
* @ Access private
*/
Private $ _ password;
/**
* @ Var address of the mail Transmission Proxy Server
* @ Access protected
*/
Protected $ _ sendServer;
/**
* @ Var the port of the mail Transmission Proxy Server
* @ Access protected
*/
Protected $ _ port = 25;
/**
* @ Var sender
* @ Access protected
*/
Protected $ _ from;
/**
* @ Var recipient
* @ Access protected
*/
Protected $ _;
/**
* @ Var topic
* @ Access protected
*/
Protected $ _ subject;
/**
* @ Var mail body
* @ Access protected
*/
Protected $ _ body;
/**
* @ Var: whether the email is in HTML Format
* @ Access protected
*/
Protected $ _ isHTML = false;
/**
* @ Var socket Resource
* @ Access protected
*/
Protected $ _ socket;
/**
* @ Var error message
* @ Access protected
*/
Protected $ _ errorMessage;
Public function _ construct ($ from = "", $ to = "", $ subject = "", $ body = "", $ server = "", $ username = "", $ password = "", $ isHTML = "", $ port = ""){
If (! Empty ($ from )){
$ This-> _ from = $ from;
}
If (! Empty ($ )){
$ This-> _ to = $;
}
If (! Empty ($ subject )){
$ This-> _ subject = $ subject;
}
If (! Empty ($ body )){
$ This-> _ body = $ body;
}
If (! Empty ($ isHTML )){
$ This-> _ isHTML = $ isHTML;
}
If (! Empty ($ server )){
$ This-> _ sendServer = $ server;
}
If (! Empty ($ port )){
$ This-> _ port = $ port;
}
If (! Empty ($ username )){
$ This-> _ userName = $ username;
}
If (! Empty ($ password )){
$ This-> _ password = $ password;
}
}
/**
* Set Email transmission proxy
* @ Param string $ ip address or domain name of the server Proxy server
* @ Param int $ port indicates the port of the proxy server. smtp defaults to port 25.
* @ Param int $ localPort local port
* @ Return boolean
*/
Public function setServer ($ server, $ port = 25 ){
If (! Isset ($ server) | empty ($ server) |! Is_string ($ server )){
$ This-> _ errorMessage = "first one is an invalid parameter ";
Return false;
}
If (! Is_numeric ($ port )){
$ This-> _ errorMessage = "first two is an invalid parameter ";
Return false;
}
$ This-> _ sendServer = $ server;
$ This-> _ port = $ port;
Return true;
}
/**
* Set email
* @ Access public
* @ Param array $ config mail configuration information
* The verification information includes the sender, recipient, subject, content, and email transmission proxy.
* @ Return boolean
*/
Public function setMailInfo ($ config ){
If (! Is_array ($ config) | count ($ config) <6 ){
$ This-> _ errorMessage = "parameters are required ";
Return false;
}
$ This-> _ from = $ config ['from'];
$ This-> _ to = $ config ['to'];
$ This-> _ subject = $ config ['subobject'];
$ This-> _ body = $ config ['body'];
$ This-> _ userName = $ config ['username'];
$ This-> _ password = $ config ['Password'];
If (isset ($ config ['ishtml ']) {
$ This-> _ isHTML = $ config ['ishtml '];
}
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;
}
}
$ This-> close (); // In fact, there is no need to close it here. After smtp command: After QUIT is sent, the server closes the connection and the local socket resources are automatically released.
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 (){
If ($ this-> _ isHTML ){
$ Mail = "MIME-Version: 1.0 \ r \ n ";
$ Mail. = "Content-type: text/html; charset = UTF-8 \ r \ n ";
$ Mail. = "FROM: test <". $ this-> _ from. "> \ r \ n ";
$ Mail. = "TO: <". $ this-> _ to. "> \ r \ n ";
$ Mail. = "Subject:". $ this-> _ subject. "\ r \ n ";
$ Mail. = $ this-> _ body. "\ r \ n. \ r \ n ";
}
Else {
$ Mail = "FROM: test <". $ this-> _ from. "> \ r \ n ";
$ Mail. = "TO: <". $ this-> _ to. "> \ r \ n ";
$ Mail. = "Subject:". $ this-> _ subject. "\ r \ n ";
$ Mail. = $ this-> _ body. "\ r \ n. \ r \ n ";
}
$ Command = array (
Array ("HELO sendmail \ r \ n", 250 ),
Array ("auth login \ r \ n", 334 ),
Array (base64_encode ($ this-> _ userName). "\ r \ n", 334 ),
Array (base64_encode ($ this-> _ password). "\ r \ n", 235 ),
Array ("mail from: <". $ this-> _ from. "> \ r \ n", 250 ),
Array ("rcpt to: <". $ this-> _ to. "> \ r \ n", 250 ),
Array ("DATA \ r \ n", 354 ),
Array ($ email, 250 ),
Array ("QUIT \ r \ n", 221)
);
Return $ 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?
* @ Param 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 ))){
// Read the Server Response
$ Data = trim (socket_read ($ this-> _ socket, 1024 ));
Echo 'response: '. $ data .'
';
If ($ data ){
$ Pattern = "/^". $ code. "http://www.jb51.net /";
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 php-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;
}
// 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 ********** *************************/
$ Config = array (
"From" => "XXXXX ",
"To" => "XXXXX ",
"Subject" => "test ",
"Body" =>"Test",
"Username" => "XXXXX ",
"Password" => "******",
// "IsHTML" => true
);
$ Mail = new MySendMail ();
$ Mail-> setServer ("smtp.126.com ");
$ Mail-> setMailInfo ($ config );
If (! $ Mail-> sendMail ()){
Echo $ mail-> error ();
Return 1;
}
I hope this article will help you with php programming.