Whenever we get voicemail at home, MCI sends an email to whatever address I specify. This is great, but I don’t recognize most phone numbers. So, today I got the great idea to write a script that takes the inbound number, looks it up in an SQL database, and then sends the name to my cell phone. This is extremely cool; then I was able to import the names from my address book into the database. I learned out php, sed, and SQL in this particular exercise.I’m glad that I have the knowledge to learn whatever I need to in order to make technology work for me.
I used 2 scripts, the first is called from /etc/mail/aliases as in:
vm_notify: "|/usr/local/bin/vmnotify_stage1",
Where vmnotify_stage1 is:
#!/bin/sh
sed '
{
/You have received/!d
h
s/You have received a Voicemail message from //
h
s/.//
}' | /usr/local/bin/vmnotify_stage2
and
vmnotify_stage2 is:
#!/usr/bin/php -q
<?php
$server = "localhost"; // server to connect to.
$database = "contacts"; // the name of the database.
$db_user = "username"; // mysql username to access the database with.
$db_pass = "password"; // mysql password to access the database with.
$table = "callerid"; // database table
$phone_number = trim(fgets(STDIN)); // reads one line from STDIN
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
$result = mysql_query("SELECT * FROM $table WHERE `phone_number` like "%$phone_number%" ORDER BY `phone_number`", $link)
or die ("Could not read data because ".mysql_error());
$text = "";
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
while ($qry = mysql_fetch_array($result))
{
$text = $text . "$qry[name] ($qry[phone_number])n";
}
}
else
{
$text = $phone_number;
}
mail("example@example.com","Voicemail from:", $text);
mysql_close();
?>
This may not be the most efficient way, but it appears to work.