HylaFAX The world's most advanced open source fax server

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

[hylafax-users] Hylafax email to fax gateway



 

(I have attached files and sorry this is sloppy I did this quick and dirty).

 

Goal: Setup a fax-server (Using Linux) for email to fax gateway, fully supporting MS office 2007 and lower document types.

 

What software was used:

 

Trixbox CE

Hylafax 6.0

IAXModem

OpenOffice 3.0

Sendmail

python and bash scripts

 

How it was done:

 

I am not going to go step by step on item that already have tutorials. I will point you to the location of a tutorial.

 

First I installed Trixbox CE (http://www.trixbox.org/wiki/trixbox-quick-install-guide) This is a free and open source PBX system with a great community behind it. In that I set up and IAX Modem (http://www.trixbox.org/forums/trixbox-forums/help/hylafax-how ) and made a sip trunk for making calls out.

 

Then I installed Hylafax. http://www.hylafax.org/content/Handbook:Binary_Package_Install

and all the parts it needed.

 

The trouble came when I needed to set up the email to fax gateway. It needed to take an email, parse it, get the send info from the header and convert the parts to a sendable format for sendfax. Hylafax does have faxmail that can work as an MTA and convert email to fax, but getting it to convert MS Office 2007 (DocX) files proved to hard. So I search the internet. I found JodConverter that used Java and open office to convert docs. I set it up and it worked, but that seemed heavy and I am not versed ion Java. That find lead me to its sister project, PyODConverter http://www.artofsolving.com/opensource/pyodconverter

 

This was python using open office to convert the files. This made more sense to me and seemed to be lighter and faster. I had to set up open office to run headless. I made a bashscript called rrunoffice that started office without a head (no gui) and opened a prot for python to talk to. It is basically one long line

 

/usr/lib/openoffice.org3/program/soffice "-accept=socket,host=localhost, port=8100;urp;StarOffice.ServiceManager" -norestore -nologo -headless -nofirststartwizard&

 

This works well and makes it easy when I need to restart it.

 

I made a subdomain with an MX record so I could send email to fax_number@xxxxxxxxxxxxxx

I then configured sendmail to have a catch all for that email domain sent to an alias. The alias was set to feed a program.

 

Sendmail Files I edited:

file /etc/mail/mailertable

 

# number@hyla

hyla.domain.tld local:mail2fax3

 

(mail2fax3 is the alias i set up

file /etc/aliases

 

# Webmin: Pipe to python script for hyla

mail2fax3: |/usr/local/bin/mail.py

 

 

Now I needed something to convert. This meant getting something to decode email into it's parts and put it in a directory for proccessing. I knew Python could do it so I took a script and converted it to read from stdin so I could pipe the mail to it.

 

The script

 

 

#!/usr/bin/env python

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

import os

import sys

import email

import mimetypes

import tempfile

import re

import string

#import logging

#

# Parse email into parts and store them in a tempdir/

# Dave Finnerty 5-25-2010

#

def main():

#Read piped in message from stdin

fp = sys.stdin.read()

#Convert this to a message object

#get all addressing information

msg = email.message_from_string(fp)

toname = msg.get("To")

mailFrm = msg.get("From")

mailSub = msg.get("Subject")

faxnumber = re.search('(\d{10})',toname)

f2num = "".join(faxnumber.groups())

fromemail = str(mailFrm).replace('"', "")

faxdir = tempfile.mkdtemp()

if not faxdir:

sys.exit(1)

counter = 1

for part in msg.walk():

# multipart/* are just containers

if part.get_content_maintype() == 'multipart':

continue

filename = part.get_filename()

if not filename:

ext = mimetypes.guess_extension(part.get_content_type())

if not ext:

# Use a generic bag-of-bits extension

ext = '.bin'

filename = 'part-%03d%s' % (counter, ext)

filename2 = str(filename)

filename2 = string.replace(filename2, ' ', '_')

counter += 1

fp = open(os.path.join(faxdir, filename2), 'wb')

fp.write(part.get_payload(decode=True))

fp.close()

# sendfaxcmd = "sendfax -d "+ f2num + " -r " + mailSub + " *.pdf"

sendfaxcmd = ('sendfax -D -r "%s" -f "%s" -o root -d %s %s/*.ps' % ( mailSub, fromemail, f2num, faxdir))

os.chdir(faxdir)

os.system('docx2pdf *')

os.system('df-pdf2ps *.pdf')

os.system(sendfaxcmd)

if __name__ == '__main__':

main()

 

 

 

Script was inspired by http://docs.python.org/library/email-examples.php

 

This script does this:

1.      Reads Standard input (Piped in email)

2.      Turns the input into a mail message object

3.      Gets the addressing information out of the header.

4.      Creates a temp directory.

5.      Splits message into it separate parts and remove the blank spaces from the name and stores it in the temp directory

6.      Creates the string variable for the sendfax command inserting variables created from the header into the string.

7.      The next step is Python runs some bash scripts.

1.      Chdir to the temp dir

2.      docx2pdf (Bash script to send everything to the python script.

3.      Df-pdf2ps converts all pdf to .ps files

4.      Lastly it runs the sendfax command to submit the fax to Hylafax.

SECURITY

WARNING----I have set a limit to the corporate email servers that we want to allow to send faxes.  If you don’t lock it down any one can send a fax.

 

 

--

Dave Finnerty,  Unix System Administrator

The Casey Group: a business and technology services company

Address: 77 East Halsey Road, Parsippany, NJ 07054

WEB: http://www.caseygrp.com/

Email: DFinnerty@xxxxxxxxxxxxxxxxx

 

 

 

Attachment: mail.py
Description: mail.py

Attachment: df-pdf2ps
Description: df-pdf2ps

Attachment: docx2pdf
Description: docx2pdf

Attachment: runoffice
Description: runoffice




Project hosted by iFAX Solutions