How PHP sends emails

Source: Internet
Author: User
Tags qmail

This article is reprinted

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 simple, a mail script sometimes makesProgramThe staff was 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.
Component method.

SMTP is short for Simple Mail Transfer Protocol, 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. windows NT Service
Some or the SMTP service programs built in Windows 2000 are typical.

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 virtual hosting package, the SMTP service program should have been installed on this computer. if you are a system administrator of an ISP or indoor computer, you are likely
Some SMTP software has been installed on this computer to handle the process of sending emails 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 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.

"If a server is not running now, how can I use it? "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 a 56 Kbps modem to connect to the Internet through EarthLink, you can use mail.earthlink.net as your SMTP server. whether you send emails to your external email server
What mail clients (Eudora, outlook, Netscape Mail, etc.) are used by the server and your phpCodeThe process of using your SMTP server is the same. 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:

<? Phpinfo ()?>

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 usually used as the default value. The following is an example of this configuration command. Youraddress@yourdomain.com It refers to your own email 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-: 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-: 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.

Once you know how to send emails to a person, you may not be able to send the same email to many recipients-just like a news letter. all of these involve a nice and lightweight loop to traverse a list of recipients and send emails to them. the form of the mail function is the same.

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.