Asterisk Faxing Part 1: Receiving Faxes

Published on Apr 22, 2021
Topics: Coding, Asterisk, VOIP

We recently decided to eliminate some legacy fax lines and utilize asterisk's built in fax functionality. It was surprisingly straight forward to do. We don't do a ton of faxing, but we deal with enough old school companies that we have to support it.

Setting up a DID to receive a fax looks like...

exten => 5551234567,1,NoOp(Lets get a fax!)
 same => SET(email=email@example.com)
 same => SET(datetime=${STRFTIME(,,%T%F)})
 ; where we put the TIF
 same => SET(tiffpath=/var/spool/received-faxes/${datetime}.tif)
 ; where we put the PDF
 same => SET(pdfpath=/var/spool/asterisk/converted-faxes/${email}/${datetime}.pdf)
 ; get the fax and store the given tif file
 same => ReceiveFAX(${tifffpath})
 ; Use convert the tif to a PDF (install ImageMagick to get convert command)
 same => System(/usr/bin/convert ${tiffpath} ${pdfpath})
 ; Send the PDF in an e-mail
 same => System(mail -s "Received Fax" -a ${pdfpath} ${email} << "You got a fax!")
 same => Hangup()

Once we got one number and running it was simple enough to extract that in to a subroutine. Now we can easily set up different numbers to send faxes to different e-mails.

[receivefax]
; ARG1 - the e-mail to send to
exten => 1,s,NoOp(lets receive a fax - subroutine style)
 same => SET(LOCAL(email)=${ARG1})
 same => SET(LOCAL(datetime)=${STRFTIME(,,%T%F)})
 ; where we put the TIF
 same => SET(LOCAL(tiffpath)=/var/spool/received-faxes/${datetime}.tif)
 ; where we put the PDF
 same => SET(LOCAL(pdfpath)=/var/spool/asterisk/converted-faxes/${email}/${datetime}.pdf)
 ; get the fax and store the given tif file
 same => ReceiveFAX(${tifffpath})
 ; Use convert the tif to a PDF (install ImageMagick to get convert command)
 same => System(/usr/bin/convert ${tiffpath} ${pdfpath})
 ; Send the PDF in an e-mail
 same => System(mail -s "Received Fax" -a ${pdfpath} ${email} << "You got a fax!")
 same => Return()

[incomingdids]
exten => 5551234567,1,Gosub(receivefax,s,1(email@example.com))
 same => n,Hangup()
 
exten => 5551234568,1,Gosub(receivefax,s,1(email2@example.com))
 same => n,Hangup()
  
exten => 5551234569,1,Gosub(receivefax,s,1(email3@example.com))
 same => n,Hangup()

I'll write up part 2 about how send faxes later this week. It was definitely a more complex problem than receiving the faxes.