I have been throwing around a few ideas lately to automate certain tasks via my cell phone. Let’s say that I’m in need of some information stored in a database on my webserver, or maybe I just want to upload a photo to my website from my cell phone… what kind of tools do I have to make this happen?

After a few days of thinking about the process I have came up with a nifty little method involving a PHP shell script which can be used to pipe e-mail commands.

Here is the PHP script:


#!/usr/bin/php -q
<?php
// read from stdin

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

$email = "";

while (!feof($fd)) {

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

}

fclose($fd);

// handle email

$lines = explode("\n", $email);

// empty vars

$from = "";

$subject = "";

$headers = "";

$message = "";

$splittingheaders = true;

for ($i=0; $i &lt; count($lines); $i++) {

if ($splittingheaders) {

// 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];

}

} else {

// not a header, but message

$message .= $lines[$i]."\n";

}

if (trim($lines[$i])=="") {

// empty line, header section has ended

$splittingheaders = false;

}

}

$from = addslashes($from);

$subject = addslashes($subject);

$message = addslashes($message);

$carrierEmail = $from;

$triggerName = trim($message);

return 0;

?>

So as you can see that PHP file is grabbing values from an email message. Specially we are interested in the message body or what I like to call the trigger word (command). This is what will be used to write more PHP code to handle just about any task you could imagine.

So here is how it works:

  • Setup a new email forwarder on your server or at your hosting provider. My host happens to be running Cpanel which makes this really simple.
  • Tell the forwarder to take mail from some [email protected] and forward it to this
  • | php -q /home/user/mailparser.php

Think it of like having your cell phone send an email to your email address with the words “last10″ and it automatically sent you back a list of the last 10 website you went to. You could program a list of commands in a database and have users sign up to manage these commands and build a whole scripting langauge behind automating task whether is be web based or by client installed desktop software. You could even program something with this to interface with a desktop application that turned on and off appliances in your house remotely.