Crmgo Blog

Configuring Fax

Configuring Fax

Configuring fax (reception) Ideally, I would like to set up both reception and sending through Asterisk's built-in fax module, 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…

Configuring Fax

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:

yum install asterisk18-res_fax_digium
Since I compiled Asterisk manually, I downloaded the module from the official website and used the original instructions:
Notes:
In version 1.8, the res_fax module is already installed; we only need to install res_fax_digium by copying it to the modules directory.

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):

[general] maxrate=14400 minrate=2400 statusevents=no modems=v17,v27,v29 ecm=yes

Restart Asterisk and check that the fax module has loaded.

To load without restarting, you can use the command module load res_fax_digium

asterisk*CLI> module show like res_fax Module                         Description                              Use Count res_fax.so                     Generic FAX Applications                 1 res_fax_digium.so              Digium G.711 and T.38 FAX Technologies ( 0

 

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.

777 - internal fax extension
....
; from-fax exten => 777,1,GoTo(fromfax,fax,1)
...
[fromfax] ; software fax context. exten => fax,1,NoOp(group_rittal_softfax from ${CALLERID(NUM)})

; 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

<?php date_default_timezone_set('Europe/Moscow'); $args=getopt("f:m:p:");// check that the "file" parameter is set and the file exists if(!isset($args['f'])) die("use -f to give file name\n"); if(!file_exists($args['f'])) die("file '".$args['f']."' is not exists\n");

// 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");

}
?>

article taken from

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.