PHP send email with socket _php Tutorial

Source: Internet
Author: User
Tags php send email 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
There's no context. But the role of e-mail in online life is becoming more and more important. I don't want to talk about it, but what if my home space doesn't support mail ()? I also thought of using a socket to implement mail
Send, but helpless on the socket programming with PHP unfamiliar, plus send mail to use the SMTP protocol, but also read a lot of English,
To have never been studied. Finally one day I found an article about using socket programming to send mail. I unsanitary environment it like a cuff.
PayPal, and transformed it into a PHP-usable class for everyone to use. The original article was just a simple example, and there was a
Some mistakes, after many experiments, I finally changed it into a class that directly uses the socket to send mail to the specified mailbox, such as
In combination with the previous article about sending mime, you can send a message on a Web site that does not support the mail () function. Because the hair
The process of sending mail takes time and may not be the same as the processing mechanism of mail (), so it is slower, but it can solve the need to send
e-mail features, and you can also learn to use PHP for socket programming. Here we introduce the implementation principle of this class to you,
At the same time, we will explain some basic knowledge about 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 the Internet protocol based on TCP/IP is implemented by request/reply method, and the text information is used, so
It's easier to deal with. 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 returned to the client by the server.

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 whole process consists of multiple lives
The composition of the order. 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. Different services.
The response code returned by the service is in compliance with the Protocol, but the response body is not required. Each command and response has a carriage return at the end so that
Commands and responses can be handled with fputs () and fgets (). Both the command and response information for SMTP are single-line. The information of the body of the cowardly beginning?
The final end line should be in 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.

Implementation of the Send_mail class
Now I'm going to start by describing the sending message Class I wrote. With the above preparatory knowledge, the following is achieved.

member variables of the class

var $lastmessage; Record the last returned response information
var $lastact; The last action, the string form
var $welcome; Use behind helo, welcome user
var $debug; Whether to display debug information
var $smtp; SMTP server
var $port; SMTP port number
var $fp; Socket handle

Where $lastmessage and $lastact are used to record the last response information and the command executed, the user can use it when an error occurs
Children To test the need, I also defined the $debug variable, and when its value is true, some execution information is displayed during the run, otherwise none
Any output. $FP is used to save the open socket handle.

Construction of Class


--------------------------------------------------------------------------------
function Send_mail ($smtp, $welcome = "", $debug =false)
{
if (empty ($SMTP)) Die ("SMTP cannt is 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 completes the determination and setting of some initial values. $welcome used in the HELO directive to tell the server user the name.
The helo instruction requires a machine name, but it is not possible. If the user does not give $welcome, the local machine name is automatically found.

Display Debug 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 =>>;
if (!ereg ("$", $message))
$message. = "
";
$message =nl2br ($message);
echo "${m}${message}";
15}
16}
--------------------------------------------------------------------------------

http://www.bkjia.com/PHPjc/486554.html www.bkjia.com true http://www.bkjia.com/PHPjc/486554.html techarticle 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 in the online life of ...

  • Related Article

    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.