Send email with socket

Source: Internet
Author: User
Tags header mail response code socket client port number

PHP home space, the ability to provide mail functionality is really not much, always call the mail () function after the no context. 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. When the connection is opened successfully, a socket handle is returned and can be used as if it were a file handle. The operations available are fputs (), fgets (), feof (), fclose (), and so on.

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



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.