- While testing I discovered there was a limit to the number of emails that can be sent to the secret email address for Blogger
- It could do with checking the bounced emails to see which Facebook status updates haven't gone through
You'll also need to set up a cron job to schedule the running of the script. Most hosts have a cpanel with a cron option. Once an hour would be "0 * * * *" and the command would be "
/usr/local/bin/php -q /home/yourdirectory/public_html/atomtoemail.php
"
I would also highly recommend using a separate file (dbu.php in this case) that contains your email and password - this avoids showing the world your password if the script fails.
You could use a MySQL table for storing the id's but as its only going to be a small file I've kept the flat file.
You can find the Facebook status feed in your browser - look for the RSS icon in the address bar, if there isn't one, then your status updates aren't public.
In theory, this will work with any RSS/ATOM feed.
Heres the code
atomtoemail.php
<?php
/*
Original flatfile DB Author: Michael Shipley (http://www.michaelpshipley.com/)
Amended by Russell England to email new items (http://www.russellengland.com)
*/
// Location of simplepie - I've renamed the extension from .inc to .php
// Download from http://www.simplepie.org
require_once( '/home/yourdirectory/public_html/includes/simplepie.php');
// db holder
$savedItems = array();
// db file name
$savedItemsFilename = 'saveditems.php';
// max days to keep items in db
$numberOfDays = 14;
$numberOfDaysInSeconds = ($numberOfDays*24*60*60);
$expireDate = time() - $numberOfDaysInSeconds;
// Separate urls with commas for multiple feeds
$urls = array('http://www.facebook.com/feeds/page.php?format=atom10&id=youratomfeedid');
$feed = new SimplePie();
$feed->set_feed_url($urls);
$feed->set_cache_duration(100);
$feed->init();
// Set up emailer for Google mail
// Might already be in your includes directory or download from http://phpmailer.worxware.com/
include "/home/yourdirectory/public_html/includes/phpmailer/class.phpmailer.php";
// dbu.php includes the passwords, usernames and emails
include "dbu.php";
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $mailUsername; // your SMTP username or your gmail username
$mail->Password = $mailPassword; // your SMTP password or your gmail password
$mail->From = $mailFrom;
$mail->FromName = $mailFromName; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($bloggersecretemail,"blogger");
$mail->IsHTML(true); // send as HTML
/*
load flat file db into array
*/
if(file_exists($savedItemsFilename))
{
$savedItems = unserialize(file_get_contents($savedItemsFilename));
if(!$savedItems)
{
$savedItems = array();
}
}
/*
Loop through items to find new ones and insert them into db
*/
foreach($feed->get_items() as $item)
{
// if item is too old dont even look at it
if($item->get_date('U') < $expireDate)
continue;
// make id
$id = md5($item->get_id());
// if item is already in db, skip it
if(isset($savedItems[$id]))
{
continue;
}
// Found new item, display on the page for reference and send it off in an email
echo "<h2>" . $item->get_title() . "</h2>";
echo "<p>" . $item->get_description() . "</p>";
$mail->Subject = $item->get_title() ;
$mail->Body = $item->get_description(); //HTML Body
$mail->AltBody = $item->get_content(); //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo . "</br>";
}
else
{
echo "Message has been sent</br>";
}
// found new item, add it to db
$i = array();
$i['date'] = $item->get_date('U');
$savedItems[$id] = $i;
}
/*
remove expired items from db
*/
$keys = array_keys($savedItems);
foreach($keys as $key)
{
if($savedItems[$key]['date'] < $expireDate)
{
unset($savedItems[$key]);
}
}
/*
sort items in reverse chronological order
*/
function customSort($a,$b)
{
return $a['date'] <= $b['date'];
}
uasort($savedItems,'customSort');
/*
save db
*/
if(!file_put_contents($savedItemsFilename,serialize($savedItems)))
{
echo ("<strong>Error: Can't save items.</strong><br>");
}
?>