Personal tools
HylaFAX The world's most advanced open source fax server

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

#/etc/hylafax/hyla.conf
# This line tells hylafax(faxmail) to decode attachments using scripts
# located here.
MIMEConverters: /var/spool/hylafax/mimetype
#

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

#!/bin/sh
#/application/pdf
# convert PDF attachment to Postscript for HylaFAX to send
pdf2ps $1 -
# if you want to send multipage pdf attachments it is better to use "pdftops"
# use the line below instead of "pdf2ps $1 -"
# /usr/bin/pdftops $1 -


For PS attachments

#!/bin/sh
#/application/ps
# convert PS attachment to Postscript for HylaFAX to send
echo $1


For TIF attachments

#!/bin/sh
#/image/tif
# convert TIF attachment to Postscript for HylaFAX to send
/usr/bin/tiff2ps $1
# if you want to send multipage tiff images, than you have to add the option "-a"
# for example:
#/usr/bin/tiff2ps -a $1


For RTF attachments

#!/bin/sh
#/application/rtf
# convert RTF attachment to Postscript for HylaFAX to send
/usr/bin/unrtf --html $1 | /usr/bin/html2ps
#NOTE that this script does not work!
#Although it will work just fine from command line
#HylaFAX will not accept the Postscript output.
#any help is welcome...


For HTML attachments and HTML formatted e-mails

#!/bin/sh
#/text/html
# convert HTML attachment and HTML formatted e-mails to Postscript for HylaFAX to send
/usr/bin/html2ps $1


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.

#!/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.

#!/bin/bash

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

This page was last edited on 10 June 2008, at 17:24.

Powered by MediaWiki
Attribution-ShareAlike 2.5

Project hosted by iFAX Solutions