Use SOCKET to send emails in PHP (2)

Source: Internet
Author: User
Tags ereg

Introduction: This is a detailed page for sending an email using socket in PHP (2). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 327699 'rolling = 'no'>

Send_mail class implementation
Now we will introduce the mail class I have compiled. With the above preparation knowledge, the following is the implementation.

Class member variables

VaR $ lastmessage; // record the last response
VaR $ lastact; // The final action, in the string format
VaR $ welcome; // used after Helo. Welcome
VaR $ debug; // whether to display debugging information
VaR $ SMTP; // SMTP Server
VaR $ port; // SMTP port number
VaR $ FP; // socket handle
Among them, $ lastmessage and $ lastact are used to record the last response information and executed commands. When an error occurs, you can use them. For testing purposes, I also defined the $ debug variable. When its value is true, some execution information is displayed during the running process; otherwise, no output is provided. $ FP is used to save the opened socket handle.

Class Construction

--------------------------------------------------------------------------------
Function send_mail ($ SMTP, $ welcome = \ "\", $ DEBUG = false)
{
If (empty ($ SMTP) Die (\ "SMTP cannt be null! \");
$ This-> SMTP = $ SMTP;
If (empty ($ welcome ))
{
$ This-> welcome = gethostbyaddr (\ "localhost \");
}
Else
$ This-> welcome = $ welcome;
$ This-> DEBUG = $ debug;
$ This-> lastmessage = \"\";
$ This-> lastact = \"\";
$ This-> Port = \ "25 \";
}
------------------------------------------------------------------------------ This constructor mainly completes the determination and setting of some initial values. $ Welcome is used in the HELO command to tell the server user's name. The HELO command must be a machine name. If $ welcome is not provided, the local machine name is automatically searched.

Display debugging information

--------------------------------------------------------------------------------
1 function show_debug ($ message, $ inout)
2 {
3 if ($ this-> Debug)
4 {
5 if ($ inout = \ "in \") // Response Information
6 {
7 $ M = \ '<\';
8}
9 else
10 $ M = \ '> \';
11 if (! Ereg (\ "\ N $ \", $ message ))
12 $ message. = \ "<br> \";
13 $ message = nl2br ($ message );
14 echo \ "<font color = #999999 >$ {M }$ {message} </font> \";
15}
16}
------------------------------------------------------------------------------ This function is used to display debugging information. You can specify whether to upload the command or return the response in $ inout. If it is an upload command, use \ "Out \"; if it is a returned response, use \ "in \".

Line 3: Determine whether to output debugging information.
Row 3: determines whether the response information is returned. If yes, add \ "<\" before the information of Row 3 to differentiate the information; otherwise, add \ "> \" to the second line to differentiate the Upload Command.
Line 11-12: determine whether the information string is a line break. If not, add the HTML line break Mark. Line breaks of lines 13th are converted into line breaks of HTML.
Line 3 outputs the entire information, and sets the color of the information to gray to show the difference. Execute a command

--------------------------------------------------------------------------------
1 function do_command ($ command, $ code)
2 {
3 $ this-> lastact = $ command;
4 $ this-> show_debug ($ this-> lastact, \ "Out \");
5 fputs ($ this-> FP, $ this-> lastact );
6 $ this-> lastmessage = fgets ($ this-> FP, 512 );
7 $ this-> show_debug ($ this-> lastmessage, \ "in \");
8 If (! Ereg (\ "^ $ Code \", $ this-> lastmessage ))
9 {
10 return false;
11}
12 else
13 return true;
14}
-------------------------------------------------------------------------------- In writing socket processing, some commands are very similar in processing, such as helo, mail from, rcpt to, quit, Data command, all requests are required to display the relevant content based on whether the debugging information is displayed. If the returned response code is expected, the response should be processed. If not, the response should be interrupted. Therefore, for sake of clarity and simplicity, a general processing function is specially designed for the processing of these commands. In the function parameter, $ code is the expected response code. If the response code is the same, the processing is successful. Otherwise, an error occurs.

Line 3: record the last command to be executed.
Line 3 shows the Upload Command.
Line 3: Use fputs to transfer commands to the server.
Line 3: The server receives the response information in the final response message variable.
Line 3 shows the response information.
Row 3 determines whether the response is expected. If yes, Row 3 returns true; otherwise, Row 3 returns false ).

In this way, this function completes the sending and display of commands and information, and judges whether the returned response is successful.

More articles about "sending emails with sockets in PHP (2)"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/327699.html pageno: 12.

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.