Send an e-mail with a socket in PHP (a) _php

Source: Internet
Author: User
Keywords email send mail server command response message SMTP
Tags response code rfc
In the author's application of several PHP home space, the ability to provide mail functionality is not much, always call the mail () function after the end of nothing. But e-mail is becoming more and more important in online life. Think of internet worms online can not receive e-mail to call the real net worm? The role of the message I don't want to talk about it, but what if the page space doesn't support mail ()? I also think through the socket to achieve mail delivery, but helpless on the socket programming with PHP unfamiliar, and 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 copied it unsanitary environment and transformed it into a PHP-usable class for everyone to use. The original article is just a simple example, and there are some errors, in my after many experiments, the transformation finally changed it to a direct use of the socket, send mail to the specified mailbox class, if you and the previous article about sending mime together, can be implemented in does not support mail () function to send mail on the website. Because the process of sending the message takes time, it may not be the same as the processing mechanism of mail (), so it is slower, but it can solve the urgent need to send the mail function, and you can also learn to use PHP for socket programming. The following will introduce the implementation principle of this class to everyone, at the same time to explain some of the basic knowledge of SMTP.

Introduction to Socket Programming
To everyone, I am not a TCP/IP programming expert, so this is just a little bit of my understanding and experience.

Use the Fsockopen function to open an Internet connection with the 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, here because the SMTP protocol is used, the port number is 25. After a successful connection is opened, a
Socket handle, you can use it as if you were using a file handle. Available operations are fputs (), fgets (), feof (), fclose ()
such as

It's easy to introduce.

Basics of SMTP
The General Command format of Internet protocol based on TCP/IP is implemented by request/reply method, and it is easy to deal with text information. SMTP is the short name of the Simple Mail Transfer Protocol, which enables the client to send mail to the server. So the following command refers to the client issuing a request instruction to the server, while the response refers to the information that the server returns to the client.

SMTP is divided into two parts: command header and information body. The command header mainly completes the client-server connection, authentication and so on. The entire process consists of multiple 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 protocol compliant, but the response body is not required. Each command and response has a carriage return, so that the command and response can be processed using fputs () and fgets (). Both the command and response information for SMTP are single-line. The information 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 to use the machine name, can fill in freely
MAIL from:sender_id: Tell the server the address of the sender
RCPT to:receiver_id: Tell the server the address of the addressee
DATA: The content of the message begins to be transferred and ends with a special line containing only.
RESET: Cancel the instructions just now and start again
VERIFY userid: Verify that the account exists (this directive is optional and may not be supported by the server)
Quit: Quit connection, end
The response information returned by the server is (in the format: Response code + SPACE + explanation):

220 Service Ready (This information is returned when the socket connection is successful)
221 being processed
250 Request mail Action is correct, complete (helo,mail from,rcpt to,quit instruction execution succeeds returns this information)
354 Start sending data, end with. (The data instruction executes successfully returns this information, the client should send the message)
500 syntax error, command not recognized
550 command cannot be executed, invalid mailbox
552 Interrupt Handling: User exceeds file space
Here is a simple command header (this is done after opening the socket), which is the test result I sent to the stmp.263.net email:

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. The relevant content can be consulted in RFC.

RFC 821 defines the instructions for receiving/sending e-mails.
RFC 822 developed the format of the message content.
RFC 2045-2048 developed a format for multimedia mail content,
RFC 1113, 1422-1424, discusses how to improve the confidentiality of e-mail.

  • 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.