Email to Fax Gateway

HylaFAX email to fax gateway One of the main uses for HylaFAX is as an email to fax gateway. This gateway unifies a users communications, so that faxing is as simple as using an email client. The user can email john@08812345678.fax and know that the message typed, and any attachment will be faxed via HylaFAX.

*** NOTE*** This describe the old pre 4.4 version of faxmail. Begining with version 4.4.0, faxmail now handles attachments natively, according to the same typerules logic as sendfax.

To setup an email to fax gateway using HylaFAX, a few simple scripts are needed to convert attachments into postscript so that HylaFAX can then use them. You can send emails with pdf attachments, postscript attachments or tiff attachments. By default, HylaFAX will not decode any attachments, so you need to edit MIMEConverters: /var/spool/hylafax/mimetype
 * 1) /etc/hylafax/hyla.conf
 * 2) This line tells hylafax(faxmail) to decode attachments using scripts
 * 3) located here.

Create the mimetype directory, then inside that, a directory for the attachment mime type, eg application or image, then create a script inside that. The script is passed $1 as the decoded attachments filename, and HylaFAX expects postscript on standard out. The tree looks like this /var/spool/hylafax/mimetype/ /var/spool/hylafax/mimetype/application/pdf /var/spool/hylafax/mimetype/application/ps /var/spool/hylafax/mimetype/image/tiff The Scripts you will need (make sure to chmod +x them to make them executable): For PDF attachments pdf2ps $1 - For PS attachments echo $1 For TIF attachments /usr/bin/tiff2ps $1 For RTF attachments /usr/bin/unrtf --html $1 | /usr/bin/html2ps
 * 1) !/bin/sh
 * 2) /application/pdf
 * 3) convert PDF attachment to Postscript for HylaFAX to send
 * 1) if you want to send multipage pdf attachments it is better to use "pdftops"
 * 2) use the line below instead of "pdf2ps $1 -"
 * 3) /usr/bin/pdftops $1 -
 * 1) !/bin/sh
 * 2) /application/ps
 * 3) convert PS attachment to Postscript for HylaFAX to send
 * 1) !/bin/sh
 * 2) /image/tif
 * 3) convert TIF attachment to Postscript for HylaFAX to send
 * 1) if you want to send multipage tiff images, than you have to add the option "-a"
 * 2) for example:
 * 3) /usr/bin/tiff2ps -a $1
 * 1) !/bin/sh
 * 2) /application/rtf
 * 3) convert RTF attachment to Postscript for HylaFAX to send
 * 1) NOTE that this script does not work!
 * 2) Although it will work just fine from command line
 * 3) HylaFAX will not accept the Postscript output.
 * 4) any help is welcome...

For HTML attachments and HTML formatted e-mails /usr/bin/html2ps $1
 * 1) !/bin/sh
 * 2) /text/html
 * 3) convert HTML attachment and HTML formatted e-mails to Postscript for HylaFAX to send

For HTML emails that contain inline graphics This is a preprocessor script that will split the email message into parts. These parts include the main html part and any inline graphics. The pyhton module tempfile is used to safely and securely create a temp directory to write all the parts out to. Then html2ps is called to convert the temporary directory to a PS stream that is sent to stdout. Once this work is completed the script removes the temporary directory.
 * 1) !/usr/bin/python

"""Unpack a MIME message into a directory of files."""

import os import sys import email import mimetypes import re import tempfile import shutil

def main:

#Get the message from stdin fp = sys.stdin.read

#Convert this to a message object msg = email.message_from_string(fp)

#Create temp directory to store contents tempcon = tempfile.mkdtemp

#Change to our tempcon os.chdir(tempcon)

patt1 = re.compile('<') patt2 = re.compile('>')

for part in msg.walk: # multipart/* are just containers if part.get_content_maintype == 'multipart': continue # Applications should really sanitize the given filename so that an       # email message can't be used to overwrite important files ext = mimetypes.guess_extension(part.get_content_type) ctype = part.get('content-id')

if ctype: doit = re.sub(patt2, '', re.sub(patt1, 'cid:', ctype)) else: doit = 'main.html' if not ext: # Use a generic bag-of-bits extension ext = '.bin' fp = open(tempcon + '/' + doit, 'wb') fp.write(part.get_payload(decode=True)) fp.close os.system('/usr/local/bin/html2ps main.html') os.chdir('..') shutil.rmtree(tempcon)

if __name__ == '__main__': main You will likely call the above preprocessor from an initial bash script that is first called from your MTA (ie, sendmail, postfix, etc). For example, my sendmail calls this first script. Note that 'multi-write-0.1.py' is the HTML multipart script listed above.
 * 1) !/bin/bash

/usr/bin/sendfax -n -m -R -f "$2" -d "$1"
 * 1) /usr/bin/formail -a 'X-FAX-Headers: clear' | /usr/local/bin/multi-write-0.1.py | \