Asterisk Fax to email
Asterisk Fax to email example
Description
This is a working example of a fax to email implementation for asterisk
Packages used
- Spandsp (receive faxes)
- Bash (script)
- sendEmail (sending emails) - http://caspian.dotconf.net/menu/Software/SendEmail/
Overview
The idea is quite simple. I needed a fax server that could send an email with attachment when a fax is received. Fax comes in on dedicated extensions (I am not doing any fax detection as I am using SIP only) , using the dialplan the fax is received and saves to a tiff file. The email address, To name and the hosting companys name is stored in the asterisk db using database put from the management console.
These are retrieved when the call is received and passed along to a bash script which generates an email and sends it with the fax as an attachment using sendEmail (opensource per script)
Configuration files
Extensions.conf
[macro-faxreceive]
exten => s,1,SetVar(FAXFILE=/var/spool/asterisk/fax/${CALLEDFAX}/${UNIQUEID})
exten => s,2,DBGet(EXTEMAIL=${MACRO_EXTEN}/xEmail)
exten => s,3,NoOP()
exten => s,4,DBGet(EXTNAME=${MACRO_EXTEN}/xName)
exten => s,5,NoOP()
exten => s,6,DBGet(EXTCOMPANY=${MACRO_EXTEN}/xCompany)
exten => s,7,rxfax(${FAXFILE}.tif)
exten => s,103,SetVar(EXTMAIL=fax@company.com)
exten => s,104,Goto(7)
exten => s,105,SetVar(EXTNAME=Unknown)
exten => s,106,Goto(7)
exten => s,107,SetVar(EXTCOMPANY=Company)
exten => s,108,Goto(7)
[fax]
exten => 123456789,1,Macro(faxreceive)
exten => h,1,System(/var/lib/asterisk/scripts/mailfax "${CALLERIDNUM}" "${CALLEDFAX}" "${EXTNAME}" "${EXTEMAIL}" "${FAXFILE}" "${EXTCOMPANY}")
[default]
exten => 123456789,1,SetVar(CALLEDFAX=${EXTEN})
exten => 123456789,2,Answer
exten => 123456789,3,Goto(fax,${EXTEN},1)
Bash Script
#!/bin/bash
echo Received paramters $1 $2 $3 $4 $5 $6 >>/var/log/faxmail.log
DATETIME=`date +"%A %d %b %Y %H:%M"`
if [ -e $5.tif ]
then
echo fax file $5.tif found. Sending email to $4 .... >>/var/log/faxmail.log
PAGES=$(tiffinfo $5.tif | grep "Page")
DT=$(tiffinfo $5.tif | grep "Date")
DTFAX=${DT#*:}
COUNT=${PAGES#*-}
rm -f $5.txt
echo Dear $3, >>$5.txt
echo >>$5.txt
echo You have just recieved a new fax document. Details as follow >>$5.txt
echo >>$5.txt
echo "From : "$1 >>$5.txt
echo "To : "$2 >>$5.txt
echo "When : "$DATETIME '['$DTFAX' ]'>>$5.txt
echo "Pages : "$COUNT>>$5.txt
echo >>$5.txt
echo >>$5.txt
echo You can view your faxes online by visiting https://fax.abc.com. Your login name is the full fax number >>$5.txt
echo >>$5.txt
echo Thank you for using $6 >>$5.txt
echo sendEmail -f $1@fax.abc.com -t $4 -u "New fax received" -a $5.tif -o message-file=$5.txt \ >> /var/log/faxmail.log
echo "<<<<<<<<<<<<<<<<<<<<---------------->>>>>>>>>>>>>>>>>>>>>>>>>" >> /var/log/faxmail.log
/usr/local/bin/sendEmail -l /var/log/sendEmail.log -q -s 195.219.151.8 -f $1@fax.abc.com -t $4 -u "New fax received" -a $5.tif -o "message-file=$5.txt"
else
rm -f $5.txt
echo Dear $3, >>$5.txt
echo >>$5.txt
echo A call was recieved on your fax line, however no fax was recieved or the attempt failed. Details as follow >>$5.txt
echo >>$5.txt
echo "From : "$1 >>$5.txt
echo "To : "$2 >>$5.txt
#echo $DATETIME >>$5.txt
echo "When : "$DATETIME >>$5.txt
#echo "Pages : "$COUNT>>$5.txt
echo >>$5.txt
echo This notification is for your conveniance, if it is not required please notify your system administrator >>$5.txt
#echo >>$5.txt
#echo You can view your faxes online by visiting https://fax.abc.com. Your login name is the full fax number >>$5.txt
echo >>$5.txt
echo Thank you for using $6 >>$5.txt
echo sendEmail -f $1@fax.abc.com -t $4 -u "Fax reception failed" -o message-file=$5.txt \ >> /var/log/faxmail.log
echo "<<<<<<<<<<<<<<<<<<<<---------------->>>>>>>>>>>>>>>>>>>>>>>>>" >> /var/log/faxmail.log
/usr/local/bin/sendEmail -l /var/log/sendEmail.log -q -s 195.219.151.8 -f $1@fax.abc.com -t $4 -u "Fax reception failed" -o "message-file=$5.txt"
exit
fi
Page created by : Umar Sear


Comments
333make spandsp
Making all in src
make1: Entering directory `/usr/src/iaxmodem-0.0.14/lib/spandsp/src'
cd .. && /bin/sh /usr/src/iaxmodem-0.0.14/lib/spandsp/config/missing --run autoheader
Can't locate object method "path" via package "Autom4te::Request" at /usr/bin/autom4te line 81.
autoheader: /usr/bin/autom4te failed with exit status: 1
make1: *** ../config-h.in Error 1
make1: Leaving directory `/usr/src/iaxmodem-0.0.14/lib/spandsp/src'
make: *** all-recursive Error 1
Can you help me how to solving it?
333Question about the System Call in this example
I've added the above configuration to my extensions_additional.conf
Everything seems to work, the tiff file is generated and stored in the /var/spool/asterisk/fax directory with the appropriate name given. However, the call to System doesn't seem to be working. No matter what I put in there, it doesn't seem to work, I tried the bash routine listed. Itried creating a routine that did nothing but create another test file, I even tried replacing it with /bin/touch 'file' and the file doesn''t get created.
Is ther any setup configuration needed to get the System function to work?
333Question about the System Call in this example
I've added the above configuration to my extensions_additional.conf
Everything seems to work, the tiff file is generated and stored in the /var/spool/asterisk/fax directory with the appropriate name given. However, the call to System doesn't seem to be working. No matter what I put in there, it doesn't seem to work, I tried the bash routine listed. Itried creating a routine that did nothing but create another test file, I even tried replacing it with /bin/touch 'file' and the file doesn''t get created.
Is ther any setup configuration needed to get the System function to work?
333Pages bug
I added a while loop that runs COUNT=${COUNT#*-} until in $COUNT there is no more "Page" in $COUNT.
The script looks like this:
PAGES=$(tiffinfo $5.tif | grep "Page")
COUNT=${PAGES#*-}
while echo $COUNT | grep -q "Page"
do
COUNT=${COUNT#*-}
done
maby there is a simpler or better way to do this, but this works
if somebody knows a simpler way please tell it to me
Peter
333Efax
333Not necessarily
333rowi.net
should be
exten => s,1,SetVar(FAXFILE=/var/spool/asterisk/fax${CALLEDFAX}/${UNIQUEID})
since it contains a not needed "/".
Rolf