tony-landis (owner)

Revisions

gist: 69820 Download_button fork
public
Public Clone URL: git://gist.github.com/69820.git
Embed All Files: show embed
asterisk-voicemail-send.php #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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;
}
}
?>