An explanation of the mail () function usage and SMTP working principle in PHP

Source: Internet
Author: User
Tags phpinfo qmail
A script that sends an e-mail message may be one of the most common scripts you can find on a Web site, although it's simple, and a mail script can sometimes make the programmer very frustrated. PHP has a function called mail (), it only needs to know the recipient's address and the letter body can send the message, but to let mail () as you want to run you also need to solve some difficult problems.

For mail () to work, you must have an SMTP server in order for PHP to connect. No matter how important the server is to the mail program, most people have no stint concept of how it works. In this tutorial, we will reveal the SMTP Secret and solve some common problems with sending mail using PHP. Other topics in this article will include methods for looping through an address list and sending a message to the receiver in both text and HTML formats.

SMTP is an abbreviation for Simple Mail Transfer Protocol (easy Mail Transferprotocol), and an SMTP server is a computer that runs this protocol and sends out messages. Running such a protocol actually means running a SendMail and QMail Programs-if you're using a non-Windows computer. On the Windows platform, as part of Windows NT ServicePack or built into Windows 2000 internal SMTP Service programs are typical of this kind of program.

I'm not saying that SMTP packages are the only ones, but they are the most common. If your Web site uses a portion of the Internet service provider's host package (Internet ServiceProvider's virtual hostingpackage), Then the SMTP service should already be installed on this computer. If you are a system administrator on a computer that is located in an ISP or in the room, you probably already have some kind of SMTP software installed on this computer to handle the process of sending mail from the Web server.

However, if you are an individual user and have only one development Web service program running on your PC, you may not be running SMTP software on your own machine. Here is a very simple but accurate thumb law: if you are a Windows users and never see the word SMTP server, then you do not run this program. If you do not, then you have two options: Install, configure, and maintain an SMTP service program (if you don't know what that is, it is not recommended to use this method) Or use an existing SMTP server.

"If a server is not running now, how do I use it?" You may have to ask. If your computer is connected to the Internet via a dial-up connection (or DSL or cable), you can use your ISP's outgoing mail server. For example, if you are developing a computer that is a Windows98 system and uses 56kbpsmodem to connect to the Internet via Earthlink, then you can use Mail.earthlink.net as your SMTP server. No matter what mail client you use for your outgoing mail server (Eudora, Outlook, Netscapemail, and so on), They will all be the same as your PHP code using your SMTP server. The trick is to make PHP know a little bit about the facts.

In the PHP.ini management profile, several entries need to be set so that the mail () function works correctly. Before changing them, find out what they are all about. You can use the Phpinfo () function to create a file that shows the current configuration of your system, including:

Save the file, place it in the file root of your Web service program, and then access it through your browser. You should be able to see a beautifully formatted message that shows your configuration. The entry you want to view is as follows:

Smtp

Sendmail_from

Sendmail_path

If you're not using Windows, then the Sendmail_path directive is the only thing you have to worry about. If you're using Windows, you'll need to look at the last two instructions.

If you're using Linux or a UNIX variant, Sendmail_path should look like this:

Sendmail_path =/usr/sbin/sendmail

Or if you use QMail:

Sendmail_path =/var/qmail/bin/sendmail

In this command, you can also set the configuration parameters to indicate the queue buffer option or the setting Return-path header that is displayed, as follows:

Sendmail_path =/usr/sbin/sendmail-t-fyou@yourdomain.com

As a non-Windows user, that's all you have to do. If you're using Windows, you have more things to do. You also need to look at the values for SMTP and Sendmail_from. Don't be sendmail_. The SendMail in the name of the from instruction was confused. Although you did not use the program called SendMail on Windows, it was just the name of the instruction. Don't be frightened by it.

In the results shown in your phpinfo (), look at the default values for SMTP and Sendmail_from-they are either blank or contain random values. You should change them to meaningful values.

If you are determined to run an SMTP service on this computer, your entry in the php.ini file should read as follows:

SMTP = localhost

However, if you want to use the outgoing mail server of your ISP (in this case, Earthlink), then the messages in php.ini should look like this:

SMTP = Mail.earthlink.net

You can also use an IP address instead of a domain name because the computer does not differentiate between the two entries.

The second configuration directive is sendmail_from, which should be set to the e-mail address in the from header. It can be modified in the script but is usually used as the default. Here is an example of this configuration Directive youraddress@ YourDomain.com refers to your own e-mail address.

Sendmail_from =youraddress@yourdomain.com

After you have made these configuration changes, restart the Web service program and use the Phpinfo () function to verify the changes. After the work is done, you can use PHP to send e-mails.

The Mail () function is very simple: there are only five parameters, and two of them are optional. These parameters are:

Address of the Receiving party

Theme

Letter Content

Additional file information header (optional)

Additional configuration options for the SMTP service program (optional)

Additional header parameters control mail functions such as CC, BCC, reply-to, or other features that follow the SMTP protocol. In this example, I only use the From and reply-to information headers.

If you want to email me but you are using a non-Windows system, the program code should look like this: 

   $to = "julie@thickbook.com";  $subject = "ZDNet Developer article";  $msg = "I completely understand SMTP servers now!";  $headers = "from:me@mycompany.comnreply-to:someoneelse@mycompany.com";  $config = "-fwebmaster@mycompany.com";  Mail ("$to", "$subject", "$msg", "$headers", "$config"); echo "finished!";

If you are using a Windows-based SMTP service, then you may not need to use the fifth parameter, and in the additional header information parameters (that is, the fourth parameter), you need to write them separately-using RN instead of N. So, The same message is sent through the Windows-based SMTP service with the following code: 

   $to = "julie@thickbook.com";  $subject = "ZDNet Developer article";  $msg = "I completely understand SMTP servers now!";  $headers = "from:me@mycompany.comrnreply-to:someoneelse@mycompany.com";  Mail ("$to", "$subject", "$msg", "$headers"); echo "finished!";

The Echo statement in the script causes your Web browser to display a message to you when the script is finished. If you do not write the Echo statement, you will get an "Empty file" dialog box because no output can be sent to the browser side.

The Mail () function returns the true value as long as it is able to connect to the specified SMTP server. But this does not mean that the message was successfully reached by the receiver. The mail () function does not wait or report the success/error code sent by the SMTP server.

The mail () function may return false values and then give you a warning "cannot connect, on line X" or "Unknown error, on line x." If any of these two messages appear, you should check the SMTP values in the php.ini. There are two possible causes for these messages: the SMTP server is down, or PHP is unable to connect to it. Either way, your mail cannot be sent out.

This script uses hard-coded values for these parameters. With a simple HTML form, you can insert values into these parameters and have a good feedback form.

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.