www.thorko.de Thorsten Kohlhepp - Systems administrator | Email to SMS notifier

 

Email to SMS notifier

Before you can start with this howto you are going to need a proper postfix and procmail setup, so that users can use their own .procmailrc file to create custom filter rules.

Next create an email address which will be used for this purpose (ex. notify@thorko.de).
Create a filter rule in the users .procmailrc file

:0 c
* ^To: sms@thorko.de
| /usr/bin/php -q /opt/scripts/email_to_sms.php


Next create the "email_to_sms.php" file in the appropriate place

<?
$output = fopen("/tmp/sms_mail.txt", "a+");
$fd = fopen("php://stdin", "r");

while(!feof($fd)) {
        $line = fread($fd, 1024);
        fwrite($output, $line);
}

fclose($fd);
fclose($output);

// build sms file
$flag = 0;
$fd = fopen("/tmp/sms_mail.txt", "r");
$output = fopen("/tmp/sms.txt", "a+");
while(!feof($fd)) {
        $line = fgets($fd, 1024);
        if($flag == 1) {
                fwrite($output, $line);
        }
        if(preg_match('/^From:|^Subject:/', $line)) {
                // output to file
                fwrite($output, $line);
        }
        if(preg_match('/^$/', $line)) {
                $flag++;
        }
}

fclose($fd);
fclose($output);

// send sms
$message = shell_exec('/bin/cat /tmp/sms.txt');
system('/opt/scripts/smsclickatell.php <your mobile number> "'.$message.'"');

// clean up
unlink("/tmp/sms_mail.txt");
unlink("/tmp/sms.txt");
?>

Create an account on "www.clickatell.com" and replace your user information in the following script

<?php
/*****************************************
 *smsclickatell
 *
 * A simple script to send an SMS via
 * www.clickatell.com.
 *
 *
 *****************************************/

// Set the following three values:

$apiid="<your api id>";
$user="<your user name>";
$password="<your password>";


// Do not change anything beneath this line:

if($_SERVER["argc"]<3){
 die("Usage: ".$_SERVER["argv"][0]." recipientmobilenumber message\n");
}

$apiargs=array();
$apiargs["api_id"]=$apiid;
$apiargs["user"]=$user;
$apiargs["password"]=$password;
$apiargs["to"]=$_SERVER["argv"][1];
$apiargs["text"]=$_SERVER["argv"][2];

$url="http://api.clickatell.com/http/sendmsg?";
foreach($apiargs as $k=>$v)$url.="$k=".urlencode($v)."&";

@file_get_contents($url);

?>


Finally test your email-to-sms notifier by sending an email to sms@thorko.de.
If you have any questions don't hesitate to ask me.