Of the several PHP home pages that the author is applying for, there is not much that can be done to provide mail functionality, and there is no context after the mail () function is always called. But e-mail plays an increasingly important role in online life. Think of the net worm online not to receive mail can call the real net worm? The role of mail I don't want to talk about it, but what if home space doesn't support mail ()? I also think through the socket to achieve mail delivery, but helpless to use PHP socket programming is not familiar with, plus send mail to use the SMTP protocol, but also read a lot of English, so has not been studied. Finally one day I found an article about using socket programming to send mail. I unlike it as a copy and transformed it into a PHP-usable class for everyone to use. The original article is just a simple example, and there are some mistakes, after many experiments, the transformation has finally changed it to a direct use of the socket, to the designated mailbox to send mail class, if you and the previous article on the sending of MIME combined, you can implement in the Mail () function to send mail on the Web site. Because the process of sending a message takes time, it may not be the same as the mail () processing mechanism, so the speed is slower, but can solve the need to send the mail function of the urgent, but you can also learn to use PHP for socket programming. Here is the implementation of this class introduction to everyone, while explaining some of the basic knowledge of SMTP.
Introduction to Socket Programming
To all of you, I am not a TCP/IP programming experts, so here is only to tell me a little 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]);
The meaning of the parameter I don't want to talk about it. Because you want to use the SMTP protocol, the port number is 25. After the connection is opened successfully, it returns a
Socket handle, which can be used just like a file handle. Available operations are fputs (), fgets (), feof (), fclose ()
Wait
It's very simple to introduce it here.
The basics of SMTP
TCP/IP based Internet Protocol General Command format is implemented through the request/Response mode, using the text information, so it is easier to deal with. SMTP is the abbreviation for Simple Mail Transfer Protocol, which enables clients to send mail to the server. So the command described below means that the client sends a request to the server, and the response refers to the information that the server returns to the client.
SMTP is divided into two parts, the command header and the information body. The command header completes the client-server connection, authentication, and so on. The whole process consists of several commands. After each command is sent to the server, the response information is given by the server, typically a 3-digit response code and response text. The response code returned by the different servers is compliant, but the response body is not necessary. There is a carriage return at the end of each command and response, so that fputs () and fgets () can be used to process commands and responses. The command and response information for SMTP is a single line. The body is the body part of the message, and the final end line should be a separate "." As the end line.
Some common SMTP directives for clients are:
HELO hostname: Say hello to the server and tell the client the name of the machine, you can fill it out casually.
MAIL from:sender_id: Tell the server the sender's address
RCPT to:receiver_id: Tell the server the address of the addressee
DATA: Starts with the transmission of the letter and ends with a special line containing only.
RESET: Cancel the instructions just now and start anew
VERIFY UserID: Verify the existence of the account number (this instruction is optional, the server may not support)
QUIT: Exit connection, end
The response information returned by the server is (in the format: Response code + SPACE + explanation):
220 The service is ready (this information is returned when the socket connection succeeds)
221 being processed
250 The Request mail action is correct, completes (helo,mail from,rcpt to,quit instruction execution Success will return this information)
354 Start sending data, end with. (The data instruction succeeds returns this information, the client should send the information)
500 syntax error, command not recognized
550 command cannot be executed, invalid mailbox
552 Interrupt Processing: User exceeds file space
The following is a simple command header (which is done after opening the socket) and is the test result of my email to stmp.263.net:
HELO Limodou
Smtp.263.net
MAIL from:chatme@263.net
Ok
RCPT to:chatme@263.net
Ok
DATA
354 end data with.
To:chatme@263.net
From:chatme@263.net
Subject:test
From:chatme@263.net
Test
.
QUIT
Ok:queued as C46411c5097e0
This is the simple knowledge of some SMTP. Relevant content can be consulted in the RFC.
RFC 821 defines the instructions for receiving/sending e-mail.
RFC 822 Formats the contents of the message.
RFC 2045-2048 has a format for multimedia mail content,
RFC 1113, 1422-1424 discusses how to improve the confidentiality of e-mail messages.
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.