PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Networking

by W.J. Gilmore
04/05/2001

I thought I'd take some time to acquaint you with yet another facet of my favorite language: the network-related functions. In this article I touch upon several cool applications, including how to use PHP to send e-mail, verify e-mail addresses, call operating system functions from within a script, and finally conclude the article with an explanation of how to query a WHOIS database for domain information.

The mail() function

Perhaps the most commonly used network-oriented function of the PHP repertoire is the mail() function. Quite simply, the mail() function is used to send e-mail to some destination. Take a moment to review its syntax:

bool mail(str to, str subject, str message [, str addl_headers])

The first input parameter, to, specifies the e-mail destination to which the e-mail is to be directed. The parameters subject and message specify the subject header and message body, respectively. Finally, the optional input parameter addl_headers specifies any optional information that is to appear within the message header. Although this final parameter is not required, I think that you'll find it particularly useful. Before I delve into a few examples, I'll take a moment to introduce the perfunctory configuration procedures, which simply involve the possible modification of one or more directives found within the php.ini file.

Using mail() on non-Windows platforms

By default, PHP's mail function uses the mail transfer agent sendmail to execute the delivery of e-mail sent via the mail() function. Therefore the php.ini file calls for the directive sendmail_path to be supplied with the path where sendmail can be found. Therefore, if sendmail can be found in the directory /usr/bin, you could set the directive as follows:

sendmail_path = "/usr/bin/sendmail -t -i"

You'll also want to ensure that the user running PHP (probably "nobody") is capable of executing sendmail.

Using mail() on Windows platforms

Since the Windows platform does not offer sendmail, which is the default mail transfer agent for the mail() function, an SMTP (Simple Mail Transfer Protocol, the protocol used for exchanging e-mails over the Internet) address is required. Furthermore, it's possible to specify the From: address directly within the php.ini file. The two directives responsible for Windows configuration are shown here:

SMTP = localhost                   ; for win32 only
sendmail_from = me@localhost.com   ; for win32 only

Once you have the configuration procedures out of the way, it's time to begin playing with the function. In the section "Using mail()," I'll present a few examples that will introduce you to the various nuances of this cool function.

Sendmail switches

Incidentally, it's also possible to supply options to sendmail. In fact, by default, sendmail is called as sendmail -t -i. The -i switch tells sendmail to ignore any lines consisting of just a single dot, which otherwise would cause sendmail to quit reading in the message. The -t switch will tell sendmail to search the message for To:, Cc:, and Bcc: lines, deleting the Bcc: line before sending the message. For a complete listing of sendmail options, check out the respective man page on your server.

Using mail()

As you have seen from the mail() syntax, it's a rather straightforward function. However, there are a few tricks that may not be initially apparent. Let's begin with a simple example:

Listing 1-1: Sending e-mail with mail()

<?
$email = "wjgilmore@hotmail.com";
$subject = "Check out O'Reillynet.com!";
$message = <<<Message
Dear Reader,
O'Reillynet.com's PHP section is really cool, offering insightful material from that< PHP Phanatic Urb LeJeune, among others. <a href="mailto:ccoleman@oreilly.com">Email us</a> should you have any suggestions or comments!";

Thanks!
The O'Reilly PHP crew
Message;

mail($email, $subject, $message);

?>

As you may have surmised, this short script will send an e-mail to wjgilmore@hotmail.com having the subject line of "Check out O'Reillynet.com!" and a message body specified by $message. Simple enough, right? However, those of you with particularly keen insight may be wondering who exactly the message will be attributed to, since it is not specified in the mail() function. In fact, it will be addressed as from the user the webserver is running as. For example, let's assume that O'Reillynet.com's server is running as user "Matilda." Thus the From: header would state: Matilda@oreillynet.com.

Chances are, you are going to want to customize this From: attribute, in addition to others such as Reply-To. This is where the addl_headers parameter comes into play. For example, assume that I would like the same message to be addressed from "The O'Reilly PHP Crew", and also use the Reply-To address of O'Reillynet's PHP editor, "ccoleman@oreilly.com." Therefore I would modify the call to the above mail() function as shown here:

<?
$headers = "From: The O'Reilly PHP Crew\nReply-To: ccoleman@oreilly.com";

mail($email, $subject, $message, $headers);
?>

There are two points worth noting regarding the addl_headers parameter. The first is that each header must be separated by a newline character, as is shown in the above example. The second point to make is that you are certainly not limited to using just From and Reply-To; you are free to use any and all available message headers. In summary, using the addl_headers parameter will result in a more organized and personalized e-mail.

A final note about mail()

It is important to keep in mind that the PHP mail() function is to be used for typical e-mail usage, and not for sending bulk mail. For those of you interested in sending bulk e-mail, I would suggest taking some time to review sendmail's switches carefully, and also check out several of the available bulk e-mail management packages like Majordomo or Listserv. Also check out www.faqs.org for a comprehensive comparison summary of the most popular e-mail management packages.

Pages: 1, 2, 3

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: