Skip to content

Instantly share code, notes, and snippets.

@tony-landis
Created February 24, 2009 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tony-landis/69820 to your computer and use it in GitHub Desktop.
Save tony-landis/69820 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
/**
* ASTERISK PBX VOICEMAIL MAILER
*
* @author Tony Landis
* @link http://www.tonylandis.com
* @license Use how you like it, just please don't remove or alter this PHPDoc
*/
#
# config smtp info
#
$mail_lib_path = "/var/www/inc/phpmailer/"; // path to the phpmailer library
$ast_vm_path = "/var/spool/asterisk/voicemail/default/{MAILBOX}/INBOX/"; // where asterisk mailboxes are located
$ast_vm_ext = "wav"; // voicemail recording file extensions to look for
$from = "asterisk-pbx@domain.com"; // from e-mail address to use
$fromName = "Asterisk PBX Voicemail"; // from name to use when sending email
$host = "mail.domainn.com"; // smtp host
$username = ""; // smtp username
$password = ""; // smtp password
$port = "25"; // smtp port
#
# a few php settings
#
error_reporting(0);
set_time_limit(30);
#
# get args from STDIN
#
$args = array();
for($i=0; $i<5; $i++) add2array(trim(fgets(STDIN)), $args);
#
# parse out the recipient info
#
if(preg_match('/^\"([a-zA-Z0 ]{1,})\" \<(.+)\>/', $args['To'], $matches)) {
$toEmail = $matches[2]; # to email
$toName = $matches[1]; # to name
}
#
# Parse the contents of the subject
#
$subject = explode("|", $args['Subject']);
$mailbox = $subject[0];
$msgnum = $subject[1];
$callerid= $subject[2];
$duration= $subject[3];
#
# compose the email subject and body
#
$subject = "New voicemail for " . ucfirst($toName);
$body = "Hello " . ucfirst($toName) . ",\r\n" .
"You have a new message in for mailbox # {$mailbox} from {$callerid}, with a duration of {$duration}";
#
# mail class
#
require($mail_lib_path . "class.phpmailer.php");
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($toEmail, $toName);
$mail->From = $from;
$mail->FromName = $fromName;
$mail->Host = $host;
$mail->Mailer = "smtp";
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Port = $port;
#
# attempt to attach the voicemail recording
#
if(!empty($mailbox) && !empty($msgnum))
{
for($i=0; $i<=(5-strlen($msgnum)); $i++) $msgnum = "0" . $msgnum;
$attach = str_replace("{MAILBOX}", $mailbox, $ast_vm_path) . "msg" . $msgnum . "." . $ast_vm_ext;
#$mail->Body .= "\r\n" . $attach; # uncomment to debug if messages are not attached
if(is_file($attach))
$mail->AddAttachment($attach, "Message-". $mailbox ."-". $msgnum .".". $ast_vm_ext);
}
#
# send the email
#
if($mail->Send())
{
echo "\r\nSent Ok! \r\n";
} else {
echo "\r\nSend Failed... \r\n";
echo $mail->ErrorInfo;
}
#
# function add to the args array
#
function add2array($str, &$args)
{
if(preg_match("/^([a-zA-Z0-9]{1,}): (.+)/", $str, $matches))
{
$key = $matches[1];
$val = $matches[2];
$args[$key] = $val;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment