PHP mail () function and SMTP working principle and practice

Source: Internet
Author: User
Tags header ini php code 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, 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 will uncover the SMTP Secrets and solve some common problems with using PHP to send mail. Other topics in this article will include a way to iterate through an address list and send a message to the recipient in both text and HTML format.

SMTP is the abbreviation for Simple Mail Transfer Protocol (plain Mail Transferprotocol), and an SMTP server is a computer that runs this protocol and sends mail. Running this protocol actually means running such 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 in Windows 2000 The Internal SMTP Service program 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 part of the Internet service provider's host package (Internet ServiceProvider ' s virtual hostingpackage), 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 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 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 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:

Smtp

Sendmail_from

Sendmail_path

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/sendmail-t-fyou@yourdomain.com

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:

SMTP = Mail.earthlink.net

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@ YourDomain.com refers to your own email address.

Sendmail_from =youraddress@yourdomain.com

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.