PHP, Email and Twitter – updated


Update: The entire script is available at Snipt for your viewing pleasure.

Whenever we release a new Course at work an email is sent to all of the offices with all of the pertinent information including a link to the product page.

Looking for a way to keep people up-to-date on our releases through Twitter I decided the quickest, easiest and most hands-off way to do that would be to have a script that could take the email and extract the course name and a link to the product page and post it to twitter. This way we can keep people informed of the latest releases at the exact same time it’s being released.

So, I had three goals for this script.
1 – Grab the Subject and the product page link from the email.
2 – Shorten the product page link.
3 – Post the info to VTC’s twitter account

There’s nothing groundbreaking here. It’s basically reading a file with STDIN instead of a static file or webservice. But I thought I would share.


To do this I needed an email address that the Product Manager could add the the mailing list. There are several ways to do this but the method I use is to create a user on one of my servers and forward the email to a PHP script. I run postfix on my servers so inside the users directory I created a .forward script that sends the email to the PHP script instead of postfix.

Once the email is delivered it’s time to work.
First, php is called like any shell script.
#!/usr/bin/php
Then the email is read from STDIN and put it in a variable.

$fd = fopen("php://stdin", "r");

$email = "";

while (!feof($fd)) {

$email .= fread($fd, 1024);

}

fclose($fd);

Turn the email into an array and declare variables I will need. (Or may need in the future)

$lines = explode(“n”, $email);

// empty vars
$from = “”;
$subject = “”;
$headers = “”;
$message = “”;
$link = “”;
$headFrombody = true;

Emails come in two sections, the header and the body. The header is a series of key:value paramaters such as the subject, where the email came from, encoding, etc. The header ends with two newline characters. This makes it pretty easy to seerate the two and pull the header information you need using regulat expressions.

for ($i=0; $i < count($lines); $i++) {
if ($headFrombody) {
// this is a header
$headers .= $lines[$i].”n”;

// look out for special headers
if (preg_match(“/^Subject: (.*)/”, $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match(“/^From: (.*)/”, $lines[$i], $matches)) {
$from = $matches[1];
}

Once the script hits the two newline characters it knows it is in the body section of the email and looks for the product page link.

if(preg_match(“/http://www.vtc.com/products/[w-!]+.(htm)/”, $lines[$i],$matches))
{
$link = $matches[0];
}

$message .= $lines[$i].”n”;
}

if (trim($lines[$i])==”") {
// empty line, header section has ended
$headFrombody = false;
}
}

Now, the subject and the link are often over 140 characters, plus I want to append each post with the hashes #tutorial and #vtc so I need to shorten the url. Fortunately we have our own shortening service which allows me to shorten the url and keep the vtc name in the post.

// shorten the url and twitter
$productUrl = “http://vtc.es/create.php?url=” . $link;
$short = file_get_contents($productUrl);

All that’s left is to fire it off to the twitter account. To do that I use a great class created by Antonio Lupetti and added to by Scott Sloan. This is ridiculously easy.

// Send to twitter
include_once(‘twitter/twitter.php’);

$curTwitter = new twitter(“vtc”, “password”);

if (isset($short)) {

$twitter_status = $subject . ” – ” . $short . ” #tutorials #vtc”;

$curTwitter->setStatus($twitter_status);
}

That’s all there is to it, but it allows us to keep people informed of our courses in a more timely way than even RSS. Seems popular too. The VTC account had 4 fllowers when the script started running. It has grown to over 100 with new people everyday.

Pretty cool.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Tumblr
  • Twitter

, , , , ,

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Comments are closed.