How to email with PHP (1)

Source: Internet
Author: User
Tags config error code header html form ini connect php code phpinfo

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, a mail script can sometimes make programmers very frustrated. There is a function called mail () in PHP that only needs to know the receiver's address and the subject of the letter to send the message, but to have mail () run the way you want it to work you still need to solve some tricky problems.

To enable mail () to run, you must have an SMTP server so that PHP can 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'll uncover the secrets of SMTP and solve some common problems with using PHP to send mail. Other topics in this article will include ways to iterate through an address list and send a message to the recipient in both text and HTML.

SMTP is the abbreviation for the Simple Mail Transfer Protocol (Transfer Protocol). An SMTP server is a computer that runs this protocol and sends mail. Running this protocol actually means running programs such as SendMail and qmail-if you're using a non-Windows computer. On the Windows platform, as Windows NT Part of the service pack, or the SMTP service program built inside Windows 2000, is typical of this 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 Services Provider ' s virtual hosting package), The SMTP service should already be installed on this computer. If you are a system administrator on an ISP or an indoor computer, you probably have some kind of SMTP software installed on this computer that handles the process of sending mail from a Web server.

However, if you are a personal user and only one development Web service program runs 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 user and you never see the word SMTP server, then you don't have to run the program. If you do not, then you have two options: Install, configure, and maintain an SMTP service (if you don't know what it's all about, it's not recommended for you to use it.) Or use an existing SMTP server.

"If a server isn't running now, how do I use it?" You might 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 develop a computer that is a Windows98 system and uses 56kbps Modem connects to the Internet via Earthlink, then you can use as your SMTP server. No matter what mail client you use for your outgoing mail server (Eudora, Outlook, Netscape Mail and so on), and they all will be the same as your PHP code using your SMTP server. The trick is to make PHP know a little bit of truth.

In the php.ini management profile, there are several entries that need to be set up so that the mail () function works correctly. Before you change them, figure out what they are for. You can use the Phpinfo () function to display the current configuration of the system by creating a file that includes:

? Phpinfo ()?>

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 showing your configuration. The entry you want to view is as follows:

If you're not using Windows, then the Sendmail_path command is the only thing you have to worry about. If you are using Windows, you 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 instruction, you can also set configuration parameters to indicate the queue buffering option or display the settings Return-path header, as follows:

Sendmail_path =/usr/sbin/

As a non-Windows user, that's all you have to do. If you are using Windows, you have more things to do. You also need to look at the SMTP and Sendmail_from values. Don't be sendmail_. The sendmail in the from instruction name is confused. Although you do not use Windows on the name of the SendMail program, but that is only the names of instructions. Don't be frightened by it.

In the results of 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 program on this computer, your entry in the php.ini file should read as follows:

SMTP = localhost

However, if you want to use an outgoing mail server from your ISP (which is earthlink in this case), then the messages in php.ini should look like the following:


You can also use an IP address instead of a domain name because the computer does not distinguish between these 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 value. Here is an example of this configuration Directive youraddress@ refers to your own email address.

Sendmail_from =

After you have made these configuration changes, restart the Web service and then use the Phpinfo () function to validate the changes. After the work is done, you can use PHP to send emails.

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

Address of the Receiving party
Letter Contents
Other file information headers (optional)
Additional configuration options for the SMTP service (optional)

Additional header parameters Control message 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 headers.

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

$to = "";
$subject = "ZDNet Developer article";
$msg = "I completely understand SMTP servers now!";
$headers = "\";
$config = "";
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 (i.e., the fourth parameter), you need to write them separately--using \ r \ n instead of \ n. So, The same message is sent through the windows-based SMTP service as follows:

$to = "";
$subject = "ZDNet Developer article";
$msg = "I completely understand SMTP servers now!";
$headers = "\r\";
Mail ("$to", "$subject", "$msg", "$headers");
echo "finished!";

The Echo statement in the script lets your Web browser 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 true value is returned as long as you can connect to the specified SMTP server mail () function. But this does not mean that the Mail has successfully reached the receiver. The mail () function does not wait for or report on the success/error code sent by the SMTP server.

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

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

Once you know how to send a message to a person, you may be tempted to send the same message to many recipients-just like news letters. All of this involves a nice, lightweight loop that iterates through a list of recipients and sends them to them. Mail functions are the same in 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: 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.