Use PHP to send emails

Source: Internet
Author: User
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. PHP has a function called mail (), which only needs to know the recipient's address and... 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 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.

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

Recipient address, subject, letter content, and 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:

 

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 separate them for writing-use instead. therefore, the code for sending the same email through the Windows-based SMTP service is as follows:

 

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.

Address:

Reprinted at will, but please attach the article address :-)

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.