Here’s a simple webbot that, when run, sends an email notification if a web page has changed since the last time it was checked.[50] Such a webbot could have many practical uses. For example, it could monitor online auctions or pages on your fantasy football league’s website. A modified version of this webbot could even notify you when the balance of your checking account changes. The webbot simply downloads a web page and stores a page signature, a number that uniquely describes the content of the page, in a database. This is also known as a hash, or a series of characters, that represents a test message or a file. In this case, a small hash is used to create a signature that references a file without the need to reference the entire contents of the file. If the signature of the page differs from the one in the database, the webbot saves the new value and sends you an email indicating that the page has changed. Example 15-4 shows the script for this webbot.[51]
Example 15-4. A simple webbot that sends an email when a web page changes
# Get libraries include("LIB_http.php"); # include PHP/CURL library include("LIB_mysql.php"); # include MySQL library include("LIB_mail.php"); # include mail library # Define parameters $webbot_email_address = "webbot@YourDomain.com"; $notification_email_address = "yourEmail@YourDomain.com "; $target_web_site = "www.trackrates.com"; # Download the website $download_array = http_get($target_web_site, $ref=""); $web_page = $download_array['FILE']; # Calculate a 40-character sha1 hash for use as a simple signature $new_signature = sha1($web_page); # Compare this signature to the previously stored value in a database $sql = "select SIGNATURE from signatures where WEB_PAGE='".$target_web_site."'"; list($old_signature) = exe_sql(DATABASE, $sql); # If the new signature is different than the old one, update the database and # send an email notifying someone that the web page changed. if($new_signature != $old_signature) { // Update database if(isset($data_array)) unset($data_array); $data_array['SIGNATURE'] = $new_signature; update(DATABASE, $table="signatures", $data_array, $key_column="WEB_PAGE", $id=$target_web_site); // Send email $subject = $target_web_site." has changed"; $message = $subject . "\n"; $message = $message . "Old signature = ".$old_signature."\n"; $message = $message . "New signature = ".$new_signature."\n"; $message = $message . "Webbot ran at: ".date("r")."\n"; $address['from'] = $webbot_email_address; $address['replyto'] = $webbot_email_address; $address['to'] = $notification_email_address; formatted_mail($subject, $message, $address, $content_type="text/plain"); }
When the webbot finds that the web page’s signature has changed, it sends an email like the one in Example 15-5.
Example 15-5. Email generated by the webbot in Example 15-4
www.trackrates.com has changed Old signature = baf73f476aef13ae48bd7df5122d685b6d2be2dd New signature = baf73f476aed685b6d2be2ddf13ae48bd7df5124 Webbot ran at: Sun, 20 Mar 2011 17:08:00 -0600
Many spam filters automatically reject any email in which the domain of the sender doesn’t match the domain of the mail server used to send the message. For this reason, it is wise to verify that the domains for the From and Reply-to addresses match the outgoing mail server’s domain.
The idea here is not to fool spam filters into letting you send unwanted email, but rather to ensure that legitimate email makes it to the intended Inbox and not the Junk folder, where no one will read it.
It’s easy to send HTML-formatted email with images, hyperlinks, or any other media found in web pages. To send HTML-formatted emails with the formatted_mail()
function, do the following:
Set the $content_type
variable to text/html
. This will tell the routine to use the proper MIME in the email header.
Use fully formed URLs to refer to any images or hyperlinks. Relative address references will resolve to the mail client, not the online media you want to use.
Since you never know the capabilities of the client reading the email, use standard formatting techniques. Tables work well.
Avoid CSS. Traditional font tags are more predictable in HTML email.
For debugging purposes, it’s a good idea to build your message in a string, as shown in Example 15-6.
Example 15-6. Sending HTML-formatted email
# Get library include("LIB_mail.php"); # Include mail library # Define addresses $address['from'] = "mikeSchrenk@yahoo.com"; $address['replyto'] = $address['from']; $address['to'] = "mikeSchrenk@yahoo.com"; # Define subject line $subject = "Example of an HTML-formatted email"; # Define message $message = ""; $message = $message . "<table bgcolor='#e0e0e0' border='0' cellpadding='0' cellspacing='0'>"; $message = $message . "<tr>"; $message = $message . "<td><img src='http://www.schrenk.com/logo.gif'><td>"; $message = $message . "</tr>"; $message = $message . "<tr>"; $message = $message . "<td>"; $message = $message . "<font face='arial'>"; $message = $message . "Here is an example of a clean HTML-formatted email"; $message = $message . "</font>"; $message = $message . "<td>"; $message = $message . "</tr>"; $message = $message . "<tr>"; $message = $message . "<td>"; $message = $message . "<font face='arial'>"; $message = $message . "with an image and a <a href='http://www.schrenk.com'>hyperlink</a>."; $message = $message . "</font>"; $message = $message . "<td>"; $message = $message . "</tr>"; $message = $message . "</table>"; echo $message; // Send email formatted_mail($subject, $message, $address, $content_type="text/html"); ?>
The email sent by Example 15-6 looks like Figure 15-1.
Be aware that not all mail clients can render HTML-formatted email. In those instances, you should send either text-only emails or a multi-formatted email that contains both HTML and unformatted messages.