Online Book Reader

Home Category

Webbots, Spiders, and Screen Scrapers - Michael Schrenk [69]

By Root 311 0
In contrast, Unix installations need the file path to your local mail server. In either case, you must have access to a mail server (preferably in the same network domain) that allows you to send email.

Only a few years ago, you could send email through almost any mail server on the Internet using relay host, which enables mail servers to relay messages from mail clients in one domain to a different domain. When using relay host, one can send nearly anonymous email, because these mail servers accept commands from any mail client without needing any form of authentication.

The relay host process has been largely abandoned by system administrators because spammers can use it to send millions of anonymous commercial emails. Today, almost every mail server will ignore commands that come from a different domain or from users that are not registered as valid clients.

An "open" mail server—one that allows relaying—is obviously a dangerous thing. I once worked for a company with two corporate mail servers, one of which mistakenly allowed mail relaying. Eventually, a spammer discovered it and commandeered it as a platform for dispatching thousands of anonymous commercial emails.[54] In addition to wasting our bandwidth, our domain was reported as one that belonged to a spammer and subsequently got placed on a watch list used by spam-detection companies. Once they identified our domain as a source of spam, many important corporate emails weren't received because spam filters had rejected them. It took quite an effort to get our domain off of that list. For this reason, you will need a valid email account to send email from a webbot.

Sending an Email with mail()

PHP provides a built-in function for sending email, as shown in Listing 16-2.

$email_address = "some.account@someserver.com";

$email_subject = "Webbot Notification Email";

$email_message = "Your webbot found something that needs you attention";

mail($email_address, $email_subject, $email_message);

Listing 16-2: Sending an email with PHP's built-in mail() function

In the simplest configuration, as shown in Listing 16-2, you only need to specify the destination email address, the subject, and the message. For the reasons mentioned in the relay host discussion, however, you will need a valid account on the same server as the one specified in your php.ini file.

There are, of course, more options than those shown in Listing 16-2. However, these options usually require that you build email headers, which tell a mail client how to format the email and how the email should be distributed. Since the syntax for email headers is very specific, it is easy to implement them incorrectly. Therefore, I've written a small email library called LIB_mail with a function formatted_mail(), which makes it easy to send emails that are more complex than what can easily be sent with the mail() function alone. The script for LIB_mail is shown in Listing 16-3.

function formatted_mail($subject, $message, $address, $content_type)

{

# Set defaults

if(!isset($address['cc'])) $address['cc'] = "";

if(!isset($address['bcc'])) $address['bcc'] = "";

# Ensure that there's a Reply-to address

if(!isset($address['replyto'])) $address['replyto'] = $address['from'];

# Create mail headers

$headers = "";

$headers = $headers . "From: ".$address['from']."\r\n";

$headers = $headers . "Return-Path: ".$address['from']."\r\n";

$headers = $headers . "Reply-To: ".$address['replyto']."\r\n";

# Add Cc to header if needed

if (strlen($address['cc'])< 0 )

$headers = $headers . "Cc: ".$address['cc']."\r\n";

# Add Bcc to header if needed

if (strlen($address['bcc'])< 0 )

$headers = $headers . "Bcc: ".$address['bcc']."\r\n";

# Add content type

$headers = $headers . "Content-Type: ".$content_type."\r\n";

# Send the email

$result = mail($address['to'], $subject, $message, $headers);

return $result;

}

Listing 16-3: Sending formatted email with LIB_mail

The main thing to take away from the script above is that the mail header is a very syntax-sensitive string that works better if

Return Main Page Previous Page Next Page

®Online Book Reader