Use SOCKET to send emails in PHP (1)

Source: Internet
Author: User

Introduction: This is a detailed page for sending emails using socket in PHP (I). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 327700 'rolling = 'no'>

In the several PHP homepage spaces applied by the author, there are not many mail functions available. After the mail () function is called, there will be no more details. However, emails play an increasingly important role in online life. Do you think that online worms do not receive emails and can be called real worms? I don't want to talk about the role of the email, but what if the home page does not support mail () sending? I also thought about sending emails through socket, but I am not familiar with socket programming using PHP. In addition, sending emails requires SMTP protocol and reading a lot of English, so I have never studied it. Finally one day I found an articleArticleFor more information, use socket programming to send emails. I copied it as a treasure and transformed it into a usable PHP class for your use. The original Article is just a simple example, and there are still some errors. After many experiments and transformations, I finally changed it to a direct socket, class for sending emails to a specified mailbox. If you combine the class with the previous article about sending mime, you can send emails on websites that do not support the mail () function. Because 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 urgent need for the mail sending function, you can also use PHP for socket programming. The following describes the implementation principles of this class, and explains some basic SMTP knowledge.

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 implemented through the request/response method and all adopt text information, so it is easier to process them. SMTP is short for the Simple Mail transmission protocol. It can send mail to the server by the client. Therefore, 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 commands. 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. The Response codes returned by different servers follow the protocol, but the response body does not have. Each Command and response end with a carriage return. Using fputs () and fgets (), you can process commands and responses. SMTP commands and response information are single rows. The message body is the body of the email. The ending line should be separated.

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.

More articles about "sending email via socket in PHP (1)"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/327700.html pageno: 12.

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.