In the several PHP homepage spaces applied by the author, there are not many projects that can provide the mail function. After the mail () function is called
No more. However, I don't want to talk about the role of email in my online life. But what if the home page does not support mail () sending? I also thought about using socket to implement mail.
But I am not familiar with socket programming with php. In addition, the SMTP protocol is used to send emails, and I have to read a lot of English.
I have never studied it. One day I finally found an article about sending emails through socket programming. I copied it as soon as I got it.
And transformed it into a php available class for everyone to use. The original Article is just a simple example, and there is also
Some errors. After many experiments and transformations, I finally changed it to a class that directly uses socket to send emails to the specified mailbox, such
By combining with the previous article on sending MIME, you can send emails on websites that do not support the mail () function. Because sending
The mail sending process takes time. It may not be the same as the mail () processing mechanism, so the speed is slower, but it can solve the problem of sending
The mail sending function is pressing, and you can also learn to use php for socket programming. The following describes the implementation principles of this class,
At the same time, I will explain some basic SMTP knowledge to you.
Socket programming
I declare to everyone that I am not a TCP/IP programming expert, so here I just give my understanding and experience.
Use the fsockopen function to open an Internet connection. Function syntax format:
Int fsockopen (string hostname, int port, int [errno], string [errstr], int [timeout]);
I don't want to talk about the parameter. The port number is 25 because the SMTP protocol is used. After the connection is successfully opened,
Socket handle, which can be the same as the file handle. Available operations include fputs (), fgets (), feof (), and fclose ()
.
This is a simple introduction.
SMTP Basics
The common command formats of TCP/IP-based Internet protocols are all implemented through the request/response method, and all use text information.
It is easier to process. SMTP is short for the Simple Mail transmission protocol. It can send mail to the server by the client. So
The following commands refer to the request commands sent by the client to the server, and the response refers to the information that the server returns to the client.
SMTP is divided into the command header and information body. The command header is used to connect the client to the server and verify the connection. The entire process consists of multiple
. After each command is sent to the server, the server gives the response information, which is generally a three-digit response code and response text. Different servers
The response code returned by the server complies with the Protocol, but the response body does not have. Each Command and response end with a carriage return.
You can use fputs () and fgets () to process commands and responses. SMTP commands and response information are single rows. What is the beginning of Information ry?
The final end line should be a separate "." As the end line.
Some common SMTP commands on the client are:
HELO hostname: Greet the server and inform the client of the name of the machine used.
Mail from: sender_id: indicates the address of the sender on the server.
Rcpt to: receiver_id: Tell the recipient's address on the server
DATA: transfer the mail content below, and end with a special line containing only.
RESET: cancel the command.
VERIFY userid: Check whether the account exists (this command is optional and may not be supported by the server)
QUIT: disconnect and end
The response information returned by the server is in the following format: response code + space + explanation ):
220 Service ready (this message is returned when the socket connection is successful)
221 processing in progress
250 the request email is correct and completed (HELO, mail from, rcpt to, QUIT command execution will return this message)
354 start sending DATA and end with. (This message will be returned if the DATA command is executed successfully. The client should send the message)
500 syntax error. The command cannot be identified
550 the command cannot be executed, and the email address is invalid
552 interrupt handling: the user exceeds the file space
Below is a simple command header (this is done after opening the socket), which is the test result of sending an email to stmp.263.net:
HELO limodou
250 smtp.263.net
Mail from: chatme@263.net
250 OK
Rcpt to: chatme@263.net
250 OK
DATA
354 End data.
To: chatme@263.net
From: chatme@263.net
Subject: test
From: chatme@263.net
Test
.
QUIT
250 OK: queued as C46411C5097E0
This is some simple SMTP knowledge. For more information, see RFC.
RFC 821 defines instructions for receiving/sending emails.
RFC 822 defines the email content format.
RFC 2045-2048 defines the multimedia mail content format,
RFC 1113,142-discusses how to enhance the confidentiality of emails.
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
$ Lastmessage and $ lastact are used to record the last response information and the command to be executed. When an error occurs, you can use it.
. 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
Any output. $ 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 determines and sets 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 ("$", $ message ))
12 $ message. = "<br> ";
13 $ message = nl2br ($ message );
14 echo "<font color = #999999 >$ {m }$ {message} </font> ";
15}
16}
--------------------------------------------------------------------------------