Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

happens, the mail bounces back to the user who sent the mail. Unfortunately, this is
probably the Web server itself.


Reading through the bounced email can be interesting. Those running an e-commerce site
may be concerned about order confirmations that go undelivered. Yet, the volume of mail
can be very large. Add to this that delivery failure is not immediate. To the process that
sends the mail, it appears to be successful. It may be worthwhile to verify an email
address before sending mail.


RFC 821 describes the SMTP protocol, which is used for exchanging email. You can
read it at the faqs.org Web site <http://www.faqs.org/ rfcs/rfc821.html>. It
lives up to its name, simple mail transfer protocol, in that it's simple enough to use
interactively from a telnet session. In order to verify an address, you can connect to the
appropriate SMTP server and begin sending a message. If you specify a valid recipient,
the server will return a 250 response code, at which point you can abort the process.


It sounds easy, but there's a catch. The domain name portion of an address, the part after
the @, is not necessarily the same machine that receives email. Domains are associated
with one or more mail exchangers—machines that accept STMP connections for delivery
of local mail. The getmxrr function returns all DNS records for a given domain.


Now consider Listing 18.7. The verifyEmail function is based on a similar
function written by Jon Stevens. As you can see, the function attempts to fetch a list of
mail exchangers. If a domain doesn't have mail exchangers, the script guesses that the
domain name itself accepts mail.


Listing 18.7 Verifying an Email Address


<?
/*
Function: verifyEmail
Input: STRING address, REFERENCE error
Output: BOOLEAN
Description: Attempts to verify an email address by
contacting a mail exchanger. Registered mail
exchangers are requested from the domain controller
first,
then the exact domain itself. The error argument will
contain relevant text if the address could not be
* verified.
/


function verifyEmail($address, &$error)
{
global $SERVER_NAME;


list($user, $domain) = split("@", $address, 2);

Free download pdf