Wed Apr 18 15:29:28 EDT 2012

Email notifications for missed calls in Asterisk

Cached from [1].
Author: Matt Schmandt

A feature I have wanted for a long time is to get emails about missed
calls. I have a Blackberry and knowing that people are calling and
hanging up before leaving a vm is very useful when I am out in the
field. I wrote a small shell script to add this functionality to

I place the script in the h extension of the stdexten marco. The h
extension triggers once a call is hung up from that context. The first
and only priority for the h extension is a System() call. It calls
this shell script and passes many parameters to it. The first
parameter is the email address to send the email to. To make this work
for the marco, I adjust the macro to have a third parameter which is
email address. I would like to pull the email address from that
extensions vm but that function does not exist and I could not figure
out how to add that function in the C source code. If anyone knows a
better way, please let me know. The script requires you have the mailx
package installed.

Download the script[2]

Rename the file processCallEmail.sh.
  mv processCallEmail.txt processCallEmail.sh

Put it in the /var/lib/asterisk/agi-bin directory.
  mv processCallEmail.sh /var/lib/asterisk/agi-bin/"

Make sure the asterisk user has execute permissions on the script.
  chmod 755 /var/lib/asterisk/agi-bin/processCallEmail.sh

Add one line of documentation to macro-stdexten context on the line
after ${ARG2}. ; ${ARG3} - email address to send emails about missed
calls to

Add the h extension line to the macro-stdexten context. exten =>
h,1,System(processCallEmail.sh "${ARG3}" "${CALLERID(num)}"

Alter the line that calls the macro to include an email address. exten
=> 500,1,Macro(stdexten,500,SIP/${EXTEN},abc@test.com)

Here is an example of an updated stdexten macro.

; Standard extension macro:
; ${ARG1} - Extension (we could have used ${MACRO_EXTEN} here as well
; ${ARG2} - Device(s) to ring
; ${ARG3} - email address to send emails about missed calls to
exten => s,1,AGI(ldaplookupphone2.agi)
exten => s,n,Dial(${ARG2},20) ; Ring the interface, 20 seconds maximum
exten => s,n,Goto(s-${DIALSTATUS},1) ; Jump based on status NOANSWER,BUSY,CHANUNAVAIL,CONGESTION,ANSWER)

exten => s-NOANSWER,1,Voicemail(${ARG1},u) ; If unavailable, send to voicemail w/ unavail announce
exten => s-NOANSWER,2,Goto(default,s,1) ; If they press #, return to start
exten => s-BUSY,1,Voicemail(${ARG1},b) ; If busy, send to voicemail w/ busy announce
exten => s-BUSY,2,Goto(default,s,1) ; If they press #, return to start
exten => _s-.,1,Goto(s-NOANSWER,1) ; Treat anything else as no answer
exten => a,1,VoicemailMain(${ARG1}) ; If they press *, send the user into VoicemailMain
exten => h,1,System(processCallEmail.sh "${ARG3}" "${CALLERID(num)}" "${CALLERID(name) }" "${DIALSTATUS}" "${VMSTATUS}")


#Used for email alerting of incoming calls
#Written by Matt Schmandt
#version .1
#License: GPL
#$1  email address
#$2  callerid num
#$3  callerid name
#$4  dial status
#$5  vm status

#Store command line args in nice variables

MAILCMD="mailx -s"
SENDMAIL=1	#Set to 1 if you want it to email the alert.  0 is useful for debugging.
DEBUG=0		#Set to 0 for standard operation.  1 will log inputs and mail commands for debugging.

#log mail command
if [ $DEBUG -eq 1 ]; then
	echo $1 $2 \"${3}\" $4 $5 >> $LOGFILE

#Check we have an email address if not quit
if [ "$EMAIL" == "" ]; then
        exit 0

#check for call canceled.  ex. 1 ring then hangup
if [ $DIALSTATUS == "CANCEL" ]; then
        BODY="${CALLERIDNAME} (${CALLERIDNUM}) hung up quickly."

#check for answered call.  ex. someone picks up
if [ $DIALSTATUS == "ANSWER" ]; then
#        BODY="$CALLERIDNAME ($CALLERIDNUM) was answered by $PEERNAME."
#	Use the above line if you want alerts about answered calls.
#	At work this is not useful but at home it is. :)
        exit 0

#check for unanswered call.  ex. phone rang and no one picked up
if [ $DIALSTATUS == "NOANSWER" ]; then
        BODY="${CALLERIDNAME} (${CALLERIDNUM}) hung up."

        #check for hangup in vm menu.  ex call went to vm and user hung up
        if [ $VMSTATUS == "USEREXIT" ]; then
                BODY="$CALLERIDNAME ($CALLERIDNUM) hung up on vm."
        #check for hangup in vm menu.  ex call went to vm and user hung up
        if [ $VMSTATUS == "FAILED" ]; then
                BODY="$CALLERIDNAME ($CALLERIDNUM) hung up on vm."
        #if they left a vm we already would get an email.  Don't need a 2nd
        if [ $VMSTATUS == "SUCCESS" ]; then
                exit 0

#log mail command
if [ $DEBUG -eq 1 ]; then

#send email
if [ $SENDMAIL -eq 1 ]; then
        `echo $BODY | $MAILCMD $SUBJECT $EMAIL`

exit 0

[1] http://www.theschmandts.org/blog/email-notifications-for-missed-calls-in-asterisk
[2] http://www.theschmandts.org/blog/wp-content/uploads/2007/05/processcallemail.txt