Php implements SMTP mail sending method based on socket. socketsmtp_PHP tutorial

Source: Internet
Author: User
Php uses socket to implement the SMTP mail sending method, socketsmtp. Php implements SMTP mail sending method based on socket. socketsmtp this article describes how php implements SMTP mail sending method based on socket. Share it with you for your reference. The specific analysis is as follows: php implements the SMTP mail sending method based on socket, socketsmtp

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 ."/";
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.

In this example, php uses socket-based SMTP to send mails. Share it with you for your reference. The specific analysis is as follows :...

Related Article

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.