Working Principle and Practice of mail () function and SMTP in php

Source: Internet
Author: User
Tags qmail

A script that sends a E-MAIL may be one of the most common scripts you can find on the Web site, although it is very simple, a mail script can sometimes make programmers very frustrated. in PHP, there is a function called mail (). It only needs to know the recipient's address and the sender of the email to send the email, but requires mail () you still need to solve some difficult problems to run as you want.

To enable mail () to run, you must have an SMTP server so that PHP can connect to it. no matter how important the server is to the mail program, most people do not have a single star concept about how it operates. in this tutorial, we will reveal SMTP secrets and solve some common problems of sending emails using PHP. another topic in this article will include the method of traversing an address list cyclically and sending a mail to the recipient in both text and HTML formats.

SMTP is short for Simple Mail TransferProtocol, and an SMTP server is a computer that runs this protocol and sends emails. running this Protocol actually means running programs such as Sendmail and Qmail-if you are using a non-Windows computer. on Windows, as part of Windows NT ServicePack or an internal SMTP service program built in Windows 2000, it is a typical program.

I'm not saying that SMTP packages only have those, but they are the most common. if your Web site uses part of the Internet service provider's host package (Internet ServiceProvider's virtual hostingpackage), the SMTP service program should have been installed on this computer. if you are a system administrator of an ISP or indoor computer, you may have installed some SMTP software on this computer to process emails sent from the Web server.

However, if you are a personal user and only one Developer Web service is running on your PC, you may not have run the SMTP software on your own machine. the following is a simple but accurate law of thumb: if you are a Windows user and never see the word "SMTP server", then you have not run this program. if you do not have one, you have two options: install, configure, and maintain an SMTP service program. (If you do not know what it is, you are not recommended to use this method) or use an existing SMTP server.

"How can I use a server that is not running now? "You may want to ask. if your computer is connected to the Internet through a dial-up connection (or DSL or cable), you can use your ISP's external mail server. for example, if you use a Windows 98 system and use 56kbpsmodem to connect to the Internet through EarthLink, you can use mail.earthlink.net as your SMTP server. no matter what kind of mail client you use for your external mail server (Eudora, Outlook, NetscapeMail, etc.), they will be the same process as using your PHP code to use your SMTP server. the trick is to let PHP know a little truth.

In php. in the ini management configuration file, several entries need to be set so that the mail () function can run normally. before changing them, you can figure out what they are. you can use the phpinfo () function to create a file to display the current configuration of the system. This file includes:

Save the file, place it in the root directory of the file of your Web Service Program, and then access it through your browser. you should be able to see a beautifully formatted information that shows your configuration. the entries you want to view are as follows:

SMTP

Sendmail_from

Sendmail_path

If you are not using windows, the sendmail_path command is the only thing you need to worry about. If you are using Windows, you need to check the last two commands.

If you are 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 specify the queue buffer option or the displayed Return-Path header, as shown below:

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

As a non-Windows user, this is all you have to do. if you are using Windows, you have more things to do. you also need to take a look at the values of SMTP and sendmail_from. do not confuse sendmail in the sendmail_from command name. although you have not used a program named Sendmail on Windows, it is only the command name. don't be scared by it.

In the result displayed in your phpinfo (), check the default values of 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 program on this computer, your entries in the php. ini file should be as follows:

SMTP = localhost

However, if you want to use the external email server of your ISP (EarthLink in this example), the mail in php. ini should look as follows:

SMTP = mail.earthlink.net

You can also use IP addresses instead of domain names, because computers do not distinguish these two entries.

The second configuration command is sendmail_from, which should be set to the email address in the From header. it can be modified in the script but is usually used as the default value. the following is a sample youraddress@yourdomain.com for this configuration directive referring to your own mail address.

Sendmail_from = youraddress@yourdomain.com

After making these configuration changes, restart the Web service program and use the phpinfo () function to verify the changes. after these tasks are completed, you can use PHP to send emails.

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

Recipient address

Topic

Letter content

Other File Information headers (optional)

Other configuration options of the SMTP service program (optional)

The additional header Parameters Control mail functions such as CC, BCC, and Reply-To, or other functions that follow the SMTP protocol. in this example, I only use the From and Reply-To information headers.

If you want to send me an email but you are using a non-Windows system, the program code should be as follows:

$ To = "julie@thickbook.com ";

$ Subject = "ZDNet Developer article ";

$ Msg = "I completely understand SMTP servers now! ";

$ Headers = "From: me@mycompany.com \ nReply-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, you may not need to use the fifth parameter and add it to the header information parameter (that is, the fourth parameter ), you need to write them separately -- use \ r \ n instead of \ n. therefore, the code for sending the same email through the Windows-based SMTP service is as follows:

$ To = "julie@thickbook.com ";

$ Subject = "ZDNet Developer article ";

$ Msg = "I completely understand SMTP servers now! ";

$ Headers = "From: me@mycompany.com \ r \ nReply-To: someoneelse@mycompany.com ";

Mail ("$ to", "$ subject", "$ msg", "$ headers ");

Echo "finished! ";

?>

The echo statement in the script enables your Web browser to display a message to you when the script is executed. if you do not write the echo statement, you will get a "empty file" dialog box, because no output can be sent to the browser.

As long as you can connect to the specified SMTP server mail () function, the true value will be returned. however, this does not mean that the email has successfully arrived at the receiver. the mail () function does not wait for or report the successful/error code sent by the SMTP server.

The mail () function may return a false value, and then warn you that "cannot connect, in row x" or "Unknown error, in row x. "If any of the two messages appears, you should check php. SMTP value in ini. there are two possible causes for these messages: the SMTP server is paralyzed, or PHP cannot connect to it. no matter which of the two cases, your email cannot be sent out.

This script uses hardcoded values for these parameters. Using a simple HTML form, you can insert some values in these parameters and have a good feedback form.

Note: For more exciting articles, please follow the help houseProgramming TutorialTopic.

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.