Asterisk Voicemail: Obstreperous.

It’s dif­fi­cult to start my first post regard­ing Aster­isk.  I have so many opin­ions and exam­ples in my Aster­isk file.  I sup­pose I can start by say­ing that Aster­isk is the worst — that is most poor­ly designed and imple­ment­ed — piece of soft­ware I have ever used with any reg­u­lar­i­ty, but it’s also pret­ty much the only thing we’ve got.

VoIP itself is a dif­fi­cult sub­ject.  The Voice side of the argu­ment is full of large com­pa­nies with pha­lanx­es of peo­ple writ­ing stan­dards that meet all man­ner of busi­ness objec­tives.  The IP side of the argu­ment is full of star­tups mak­ing things work on the inter­net.  These are in con­stant con­flict.

Aster­isk is a good exam­ple of that lat­ter.  It expos­es both the strengths and the flaws of the start­up men­tal­i­ty.  Aster­isk’s strength is that it breaks all the rules and does some­thing gen­uine­ly use­ful.  Aster­isk’s weak­ness is that it ignores wis­dom and does things just plain wrong.  Just not wrong enough to fail.

Today, how­ev­er, we’re going to lead by exam­ple and talk about how to get aster­isk voice­mail to send the con­tents of the voice­mail in an email as an MP3 file.  By default, aster­isk would email var­i­ous forms of .WAV file … all of which are incom­pat­i­ble with many devices that might be read­ing the email.

The “right” way to pro­vide this facil­i­ty would be an option to run a task to fil­ter the file before bundling it into email.  It does­n’t sig­nif­i­cant­ly change the work the pro­gram­mer needs to do and it reduces most cas­es from a script to a sin­gle com­mand line that accom­plish­es the task.  Aster­isk does­n’t do it the “right” way.

Instead, Aster­isk asks you to sub­sti­tute anoth­er com­mand for “send­mail” that can then do what it likes with the mail that aster­isk will pass.  This requires that the sysad­min now know some­thing about how send­mail works, how mail works, etc.  This How­To is here to help you out.

When I first approached this prob­lem, I checked — a good resource for aster­isk and oth­er VoIP plat­forms.  While their page on the sub­ject gave two solu­tions for the prob­lem, both of them were over­ly com­plex and used soft­ware that I could­n’t eas­i­ly find.  The core, how­ev­er, that you sub­sti­tute your “sendmailmp3” for the reg­u­lar “send­mail” com­mand remains.  Here is my “Sendmailmp3” script…

#! /bin/bash

# save the current directory

# create a temporary directory and cd to it
 TMPDIR=`mktemp -d /tmp/voicemail.XXXXXX`


# dump the stream to a temporary file
cat >>
# This creates textfile[01] and msgXXXX.WAV
ripmime -i -d $TMPDIR

# convert wav file to mp3 file
name=`basename *.WAV .WAV`
sox $name.WAV -r 48100 $name.mp3

to=`grep ^To: | cut -d\  -f2-`
from=`grep ^From: | cut -d\  -f2-`
subj=`grep ^Subject: | cut -d\  -f2-`
msgid=`grep ^Message-ID:`
clid=`grep ^X-Asterisk-CallerID:`
clidn=`grep ^X-Asterisk-CallerIDName:`

mime-construct \
 --to "$to" \
 --header "From: $from" \
 --subject "$subj" \
 --header "$msgid" \
 --header "$clid" \
 --header "$clidn" \
 --file textfile1 \
 --file-attach $name.mp3

# go back to original directory
cd /

# remove all temporary files and temporary directory
rm -rf $TMPDIR

This code needs three non-stan­dard pack­ages: rip­mime, sox and mime-con­vert.  Of those, It’s impor­tant that the sox bina­ry knows how to make mp3 files (or what­ev­er out­put file type you’re going to use — it’s pos­si­ble to use ogg here).  Beyond this script, you sim­ply need to put this script in place of send­mail in the voicemail.conf of your aster­isk install.

This entry was posted in VoIP Tech and tagged , , , , , , . Bookmark the permalink.

5 Responses to Asterisk Voicemail: Obstreperous.

  1. Jamaal says:

    I just want to make a voice­mail mes­sage clean­ly. I have a spe­cif­ic short clip(less than 5 sec­onds) that I want to make into my greet­ing. With my speak­ers how­ev­er I get too much feed­back an it’s tin­ny, and hard to hear. I’ve looked around and can’t seem to find a solu­tion.

    If it’s worth your time maybe you or some­one else can drop me a line about mak­ing it work.


    • dgilbert says:

      Maybe I’ll email you — or maybe this reply on my blog will. Good exper­i­ment.

      One way I’ve found to make rea­son­able aster­isk record­ings is to leave your­self a voice­mail and then move the file it cre­ates into the loca­tion you want… I use this for both IVR record­ings and for voice­mail record­ings. In fact, for voice­mail, IIRC, you can do it from the voice­mail appli­ca­tion — you just select the option to record a greet­ing and you do it from a reg­u­lar phone (so your PC sound set­up does­n’t mat­ter).

  2. Gary says:

    While I agree that there are many things Aster­isk could do bet­ter, I could say that about any soft­ware — espe­cial­ly some­thing as pow­er­ful as Aster­isk and FREE. If you don’t want to take the time to learn the ins and outs, you have the option to hire some­one to do it for you… just like any oth­er soft­ware. Giv­en the free price point, I’m sure one could eas­i­ly jus­ti­fy pay­ing a pro­fes­sion­al to imple­ment cus­tom fea­tures in the sys­tem. There are many will­ing and able Aster­isk pro­fes­sion­als out there.

    • dgilbert says:

      Nice of you to take the time to com­ment, but you’re off base. I make com­ments about aster­isk while being ful­ly famil­iar with it. I use it to route sev­er­al mil­lion min­utes of traf­fic each month and I even hack on it’s code, if nec­es­sary. And… yes… there are oth­er open­source projects with qual­i­ty prob­lems … some even as bad as aster­isk’s, but there are many that are bet­ter. With some more thought on the issue, I think that aster­isk both suf­fers and ben­e­fits from being chained to Digium. It ben­e­fits in that it gets a fair amount of paid devel­op­ment and it suf­fers from being dri­ven in the direc­tion of a busi­ness plan that is at odds with it’s users.

      Look that the rela­tion­ship between Enter­priseDB and Post­greSQL as a con­trary exam­ple. Enter­priseDB offers com­ple­men­tary prod­ucts to Post­greSQL (sim­i­lar to Digium for Aster­isk) but they man­age to keep their busi­ness plan aligned with keep­ing Post­greSQL at the top of it’s form…

  3. Brett K says:

    I installed this and it would work, when I try to run the script the fol­low­ing is gen­er­at­ed as out­put, any help please?

    /usr/sbin/sendmailmp3: line 9: save: com­mand not found
    /usr/sbin/sendmailmp3: line 10: pushd: .#: No such file or direc­to­ry
    /usr/sbin/sendmailmp3: line 12: cd: /tmp/tmp.rPClLU3556#: No such file or direc­to­ry
    cat: get: No such file or direc­to­ry
    cat: the: No such file or direc­to­ry
    cat: bound­ary: No such file or direc­to­ry
    grep: No such file or direc­to­ry
    awk: cmd. line:1: fatal: can­not open file ‘’ for read­ing (No such file or direc­to­ry)
    cat: stream.part1: No such file or direc­to­ry
    sed: can’t read stream.part3: No such file or direc­to­ry
    sed: can’t read stream.part3: No such file or direc­to­ry
    dos2unix: con­vert­ing file stream.part3.wav.base64 to UNIX for­mat …
    Warn­ing: unsup­port­ed audio for­mat
    base64: stream.part3.mp3: No such file or direc­to­ry
    mv: can­not stat ‘stream.part’: No such file or direc­to­ry
    cat: stream.part1: No such file or direc­to­ry
    cat: stream.part2: No such file or direc­to­ry
    dos2unix: con­vert­ing file to UNIX for­mat …
    unix2dos: con­vert­ing file stream.part3.mp3.base64 to DOS for­mat …
    cat: stream.part4: No such file or direc­to­ry
    dos2unix: con­vert­ing file stream.tmp to UNIX for­mat …
    send­mail: fatal: No recip­i­ent address­es found in mes­sage head­er
    /usr/sbin/sendmailmp3: line 82: popd: direc­to­ry stack emp­ty

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available