PHP mail () function to send emails

Source: Internet
Author: User
Tags phpinfo qmail

Most website developers start to use

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 PHP mail (). It only needs to know the recipient's address and the sender of the email to send emails, but requires mail () you still need to solve some difficult problems to run as you want.

To enable the PHP mail () function to run, you must have an SMTP server so that PHP can be connected. 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 an email to the recipient in both text and HTML formats.

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. on the Windows platform, as part of the Windows NT Service Pack or the SMTP Service program built in Windows 2000 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 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 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 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. no matter what kind of Mail client you use for your external Mail server (Eudora, Outlook, Netscape Mail, etc.), they will be the same as the process of 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 PHP 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 instruction in the PHP mail () function, you can also set the configuration parameters to specify the queue buffer options or display the set 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 modifying the PHP mail () function configurations, restart the Web service program and use the phpinfo () function to verify the modifications. after these tasks are completed, you can use PHP to send emails.


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.