It’s difficult to start my first post regarding Asterisk. I have so many opinions and examples in my Asterisk file. I suppose I can start by saying that Asterisk is the worst — that is most poorly designed and implemented — piece of software I have ever used with any regularity, but it’s also pretty much the only thing we’ve got.
VoIP itself is a difficult subject. The Voice side of the argument is full of large companies with phalanxes of people writing standards that meet all manner of business objectives. The IP side of the argument is full of startups making things work on the internet. These are in constant conflict.
Asterisk is a good example of that latter. It exposes both the strengths and the flaws of the startup mentality. Asterisk’s strength is that it breaks all the rules and does something genuinely useful. Asterisk’s weakness is that it ignores wisdom and does things just plain wrong. Just not wrong enough to fail.
Today, however, we’re going to lead by example and talk about how to get asterisk voicemail to send the contents of the voicemail in an email as an MP3 file. By default, asterisk would email various forms of .WAV file … all of which are incompatible with many devices that might be reading the email.
The “right” way to provide this facility would be an option to run a task to filter the file before bundling it into email. It doesn’t significantly change the work the programmer needs to do and it reduces most cases from a script to a single command line that accomplishes the task. Asterisk doesn’t do it the “right” way.
Instead, Asterisk asks you to substitute another command for “sendmail” that can then do what it likes with the mail that asterisk will pass. This requires that the sysadmin now know something about how sendmail works, how mail works, etc. This HowTo is here to help you out.
When I first approached this problem, I checked voip-info.org — a good resource for asterisk and other VoIP platforms. While their page on the subject gave two solutions for the problem, both of them were overly complex and used software that I couldn’t easily find. The core, however, that you substitute your “sendmailmp3” for the regular “sendmail” command remains. Here is my “Sendmailmp3” script…
#! /bin/bash # save the current directory CURDIR=`pwd` # create a temporary directory and cd to it TMPDIR=`mktemp -d /tmp/voicemail.XXXXXX` cd $TMPDIR # dump the stream to a temporary file cat >> stream.org # This creates textfile and msgXXXX.WAV ripmime -i stream.org -d $TMPDIR # convert wav file to mp3 file name=`basename *.WAV .WAV` sox $name.WAV -r 48100 $name.mp3 to=`grep ^To: stream.org | cut -d\ -f2-` from=`grep ^From: stream.org | cut -d\ -f2-` subj=`grep ^Subject: stream.org | cut -d\ -f2-` msgid=`grep ^Message-ID: stream.org` clid=`grep ^X-Asterisk-CallerID: stream.org` clidn=`grep ^X-Asterisk-CallerIDName: stream.org` 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-standard packages: ripmime, sox and mime-convert. Of those, It’s important that the sox binary knows how to make mp3 files (or whatever output file type you’re going to use — it’s possible to use ogg here). Beyond this script, you simply need to put this script in place of sendmail in the voicemail.conf of your asterisk install.