The principle of sending mail in PHP

Source: Internet
Author: User
Tags php server php email php web server mx record qmail

Sending mail to customers in a Web application is a common feature. The SMTP protocol seems simple and informative, but it is not easy to fully understand the identity and relationship processing between client servers.
This paper briefly describes the identity and conditions in the SMTP Exchange process, so that the first contact with the development of SMTP can have a clear idea. Please correct me in the wrong place.

Author: Wdy, this article was originally published in Http://blog.csdn.net/aryang, reproduced please indicate the source.

First of all, the basic principles of SMTP, between the mail client (Outlook) and the sender SMTP server, between the sender SMTP server and the recipient SMTP server, the SMTP protocol is all about.

Let's talk about our most common situation: email with Outlook, send it from jack@abc.com to rose@xyz.com. When you send a message in Outlook, you connect to the SMTP server 25 port registered in your account and start the SMTP session:

Mail from: <jack@abc.com>
RCPT to:<rose@xyz.com>

The abc.com server discovers that mail from is a domain (abc.com), in which case the SMTP server generally needs to authenticate the user and verify that the message is sent to the server's send queue by submitting the message. The server posts the process or thread of the message, scans the send queue, and after the message is removed, the analysis is sent. The server discovers that the mail is destined for xyz.com, not this domain, first query the MX record of the xyz.com domain through the DNS server, assuming that smtp.xyz.com, the mail delivery process connects to the Smtp.xyz.com 25 port, starts the SMTP session

Mail from: <jack@abc.com>
RCPT to:<rose@xyz.com>

The xyz.com server determines that the message is from another domain and is sent to this domain, so there is no need to authenticate the user. However, it is possible that the Xyz.com server first check the IP address of the originator server and the corresponding MX records of the domain name in mail from, and reject the match. If there is no rejection condition, then xyz.com under the letter, put Rose's mail folder, Rose can receive mail through POP3.

If the abc.com send a message failed, the message into the send failed queue, may be thrown directly, or try to focus on sending n times, if all unsuccessful, will notify the sender or something, this depends on the server's processing.

Now say PHP email, first of all to find out what identity to send the mail. The first is to treat yourself as a client such as Outlook, first connect to the sending server, submit the message and send out the outgoing server. The second type: Send yourself as a sending server, sending mail directly through the server that connects to the recipient via SMTP.

Here I do not recommend the second way, the reason is: if your PHP server's domain name, IP,MX records are not strictly set up, the General collection server has a great chance of rejection; the transmission process of sending the message itself cannot be controlled, and if there is a large delay in the delivery server, Can seriously affect the work of their Web server, and also do not have a failure to repeat processing.

For sending mail, Windows and UNIX are very different. Windows generally uses the SMTP protocol to send mail to the sending server in a way that Outlook does. UNIX has its own traditional way, that is, the UNIX host itself with SMTP server, the traditional is sendmail. There is also a "sendmail" concept, here is the name of a program, whether it is sendmail,qmail,postfix, and so on, all provide this command program, through which, you can put the message into the local mail send queue, let Sendmail,qmail, Postfix, such as the delivery program to send delivery. SendMail programs typically read mail content from standard input. The PHP mail () function actually opens the SendMail program, passes the message content to it through the standard input, is sent by the SendMail program, and the rest does not matter.

So you can see that PHP in the description of the mail (), if it is on Windows, it is generally set php.ini SMTP and Smtp_port option values, through the class Outlook client way to send, But the Achilles heel of this approach is that SMTP authentication is not supported, and now the mail server basically requires SMTP authentication. Uxni generally need to set the Sendmail_path to explain the path of the SendMail program. Of course, Windows can also be sent using the SendMail program.

If it is UNIX, ideally, your PHP Web server itself is also a SMTP server, set up an MX record, and so on, or you have a SendMail program to send mail (write yourself, package SMTP send) then configure the php.ini The Sendmail_path is sent directly through the mail () function.

If there is no SendMail program to use, then it will be sent through the socket with the SMTP protocol, PHP, there are many extensions of the library, you can use directly. It is recommended that you connect to a server so that he can send it to you, it is not recommended to connect the server directly (the reason is above). Here there is not recommended directly on the Web page in PHP to send the link, the recommended way is to write the message to the database or file, so that another program (Php,perl,python can write) after the scan through the SOKCET connection sent. This will give the user a good response experience, but also can control the process of sending.

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.