Fax Configuration (Receiving)
Ideally, I would like to handle both receiving and sending via Asterisk's built-in fax functionality, but sending currently raises many organizational issues (such as how to specify the number, convert formats, etc.). Here, I will cover the configuration for receiving faxes.
The company Digium has developed and sells software fax for Asterisk. It can be downloaded and tried in free mode (with a limit of one line) or additional lines can be purchased for $40 each, which is quite inexpensive (compared to traditional fax machines).
Let's try to configure the fax so that received faxes are automatically sent to a selected email address.
Installing Digium Fax
First, we download the program itself. I used yum:
Next, purchase a license or obtain a free one for a single connection, run the /usr/sbin/register utility, and register our fax.
Create the file /etc/asterisk/res_fax.conf (I took all parameters from the initial example):
Restart Asterisk and check that the fax module has loaded.
To load without restarting, you can use the command module load res_fax_digium
Configuring the Dialplan
In my previous article, I described the "embryo" of the fax context. It's time to complete it.
I took part of the file from the fax documentation and completed the rest myself. Please note that Asterisk must have write permissions to the folder you designate for receiving faxes.
; obtain a filename in the format fax-[fax number].tif
exten => fax,n,Set(GLOBAL(FAXCOUNT)=${IF($["${GLOBAL(FAXCOUNT)}"=""]?0:${GLOBAL(FAXCOUNT)})})
exten => fax,n,Set(GLOBAL(FAXCOUNT)=$[${GLOBAL(FAXCOUNT)} + 1])
exten => fax,n,Set(FAXCOUNT=${GLOBAL(FAXCOUNT)})
exten => fax,n,Set(FAXFILE=/tmp/faxes/fax-${FAXCOUNT}.tif)
; receive the fax into a file
exten => fax,n,ReceiveFax(${FAXFILE})
exten => fax,n,Hangup()
; when the fax hangs up, we will analyze what
; it has received
exten => h,1,NoOp(fax hangup)
; get the reception status
exten => h,n,GotoIF($["${FAXOPT(status)}" != "SUCCESS" ]?finish)
; reception status - successful.
; now it is necessary to pass this file name to the script with the action description
; the script accepts the following parameters:
; -f=file name -m=action -p=parameter
; -m=del - delete file
; -m=mail -p="address" - send via email
; -m=print -p="printer name" - print
; at the first stage we will execute all three, then we will choose
; print
exten => h,n,NoOp(${SHELL(php /var/lib/asterisk/rittal/fax_work.php -f ${FAXOPT(filename)} -m print -p printer1)})
; send
exten => h,n,NoOp(${SHELL(php /var/lib/asterisk/rittal/fax_work.php -f ${FAXOPT(filename)} -m mail -p user@rittal.ru)})
; delete
exten => h,n,NoOp(${SHELL(php /var/lib/asterisk/rittal/fax_work.php -f ${FAXOPT(filename)} -m del)})
; finish
exten => h,n(finish),NoOp(finish fax hangup)
create the file /var/lib/asterisk/rittal/fax_work.php
// check that the mode parameter is set
if(!isset($args['m'])) die("use -m to give mode name\n");
switch ($args['m'])
{
case 'del':
// delete file
unlink($args['f']);
break;
case 'mail':
// send email
if( (!isset($args['p'])) || ($args['p']=='')) die("use -p to give dest mail\n");
$to=$args['p']; // to whom
$subject="Fax received ".date("d.m.Y H:i:s"); // subject
$random_hash = md5(date('r', time()));
// Headers
$headers = "From: fax@rittal.ru\r\nReply-To: postmaster@rittal.ru";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($args['f'])));
$body="";
$body .="\r\n".'--PHP-mixed-'.$random_hash;
$body .="\r\n".'Content-Type: multipart/alternative; boundary="PHP-alt-'.$random_hash.'"';
$body .="\r\n";
$body .="\r\n".'--PHP-alt-'.$random_hash;
$body .="\r\n".'Content-Type: text/plain; charset="iso-8859-1"';
$body .="\r\n".'Content-Transfer-Encoding: 7bit';
$body .="\r\n";
$body .="\r\n".'Please, see fax message in attachment';
$body .="\r\n";
$body .="\r\n".'--PHP-alt-'.$random_hash.'--';
$body .="\r\n".'--PHP-mixed-'.$random_hash;
$body .="\r\n".'Content-Type: application/tiff; name="fax.tiff"';
$body .="\r\n".'Content-Transfer-Encoding: base64';
$body .="\r\n".'Content-Disposition: attachment';
$body .="\r\n";
$body .="\r\n".$attachment;
$body .="\r\n".'--PHP-mixed-'.$random_hash.'--';
$body .="\r\n";
mail( $to, $subject, $body, $headers );
break;
case 'print':
// print file on printer
if( (!isset($args['p'])) || ($args['p']=='')) die("use -p to give printer name\n");
exec('tiff2ps "'.$args['f'].'" | lpr -P'.$args['p']);
break;
default:
die("wrong mode!\n");
}
?>
