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:
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
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
Theme
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 = "julie@thickbook.com";
$subject = "ZDNet Developer article";
$msg = "I completely understand SMTP servers now!";
$headers = "from:me@mycompany.comnreply-to:someoneelse@mycompany.com";
$config = "-fwebmaster@mycompany.com";
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 (that is, the fourth parameter), you need to write them separately--using RN instead of N. So, The same message is sent through the windows-based SMTP service as follows:
$to = "julie@thickbook.com";
$subject = "ZDNet Developer article";
$msg = "I completely understand SMTP servers now!";
$headers = "from:me@mycompany.comrnreply-to:someoneelse@mycompany.com";
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.
Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .