Send email with socket in PHP (ii)

Source: Internet
Author: User
Tags empty ereg execution response code return socket variable port number
Implementation of Send_mail Class
Now I'm going to start by introducing the Send mail Class I wrote. With the above preparatory knowledge, the following is achieved.

class's member variable

var $lastmessage; Record the last response information returned
var $lastact; The last action, the string form
var $welcome; Used behind the helo, welcome to the user
var $debug; Whether to display debug information
var $smtp; SMTP server
var $port; SMTP port number
var $fp; Socket handle

Where $lastmessage and $lastact are used to record the last response information and the commands that are executed, and when an error occurs, the user can use them. For testing purposes, I also defined the $DEBUG variable, which displays some execution information when the value is true, otherwise there is no output. $FP is used to save the open socket handle.

The construction of a class


--------------------------------------------------------------------------------
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 used in helo instructions, tell the server user's name. The HELO directive requires a machine name, but it is not necessary either. If the user does not give a $welcome, the local machine name is automatically found.

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
$m = ' >> ';
One if (!ereg ("\n$", $message))
$message. = "<br>";
$message =nl2br ($message);
echo "<font color= #999999 >${m}${message}</font>";
15}
16}
--------------------------------------------------------------------------------
This function is used to display debugging information. You can specify whether to upload an instruction or return a response in $inout, use "out" if it is an upload instruction, or use "in" for the returned response.

Line 3rd, to determine if you want to output debug information.
Line 5th, to determine whether the response information, if it is, the 7th line of information before the "<<" to distinguish the information, otherwise in the 10th row plus ">>" to distinguish between the upload instructions.
第11-12 the line to determine if the message string is the last line break, and if not, add the HTML line-wrapping tag. Line 13th lines so that the line break is translated into an HTML newline tag.
Line 14th, the whole piece of information is printed, and the information color is grayed out to make 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-&GT;FP, 512);
7 $this->show_debug ($this->lastmessage, "in");
8 if (!ereg ("^ $code", $this->lastmessage))
9 {
return false;
11}
Or else
return true;
14}
--------------------------------------------------------------------------------
In the Writing socket Processing section found that some of the processing of the command is very similar, such as Helo,mail from,rcpt to,quit,data command, are required to display the debugging information based on whether the relevant content display, and the return response code, if it is expected, should continue processing, If it is not expected, the rationale should be interrupted. So for clarity and simplification, a common processing function is written specifically for the processing of these commands. $code is the expected response code in the parameter of the function, and if the response code is the same, the processing succeeds or an error occurs.

Line 3rd, record the final execution command.
Line 4th, display the upload command.
Line 5th, use fputs to actually transfer instructions to the server.
Line 6th, receiving response information from the server is placed in the last response message variable.
The 7th line shows the response information.
Line 8th, to determine if the response information is expected, and if so, the 13th row returns success (true), otherwise it fails on line 10th (false).

This function, on the one hand, completes the display function of instruction and information, and does not judge the success of the return response.




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.