#!/usr/local/bin/python # # process-rxfax.py - post process incoming fax from freeswitch (spandsp raw .tiff => pdf, then # emailed) # # the script uses the UUID from freeswitch to make a unique filename on the server while receiving, # then renames the attachment to a friendly name for the emailed user. The script calls tiff2ps # and ps2pdf to create a PDF from the initial TIFF from spandsp. # # by steve steffler - steve@steffler.info on may 14th 2009 # released under the Creative Commons Attribution-Share Alike 2.5 License # http://creativecommons.org/licenses/by-nc-sa/2.5/ca/ # # NOTES: go easy on me, i've never wrote a python script before and IANAP. I am sure there are # far easier ways to accomplish what I am accomplishing with this script, but i am just learning. # please feel free to update this script with any/all improvements! # # if you like this script, donate some money to the FreeSWITCH developers :-) as they made this # functionality possible with thier fantastic API/mod_python module # import sys # import the sys module for argv import os # import the os module import smtplib # for SMTP import mimetypes # for working with MIME from email import Encoders from email.Message import Message from email.MIMEAudio import MIMEAudio from email.MIMEBase import MIMEBase from email.MIMEImage import MIMEImage from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from freeswitch import * ## EDIT THESE VARIABLES TO MATCH YOUR NEEDS: tiff2pscmd = '/usr/local/bin/tiff2ps' # location of tiff2ps ps2pdfcmd = '/usr/local/bin/ps2pdf' # location of ps2pdf incomingfaxes = '/usr/src/wikipbx/fax/' # change this to where your .tiffs are saved by mod_fax, trailing slash required send_from = 'support@thirtytwenty.com' # email address to put in From: header of email smtp_server = 'mail.thirtytwenty.com' # change to your SMTP server - authentication/ssl not yet implemented smtp_port = 25 tiff2psoptions = '-a -O ' # command line options go here ps2pdfoptions = '' # to fine tune output # FreeSWITCH event handlers. TODO: Implement hangup event handler and parse errors from spandsp. # For now the script is dumb and assumes everything just works. def input_callback(session, what, obj): if (what == "dtmf"): consoleLog("info", what + " " + obj.digit + "\n") else: consoleLog("info", what + " " + obj.serialize() + "\n") return "pause" def handler(session, args): #get required variables the_uuid = session.getVariable("uuid") the_recipient = session.getVariable("recipient") the_caller = session.getVariable("caller_id_number") the_dest = session.getVariable("destination_number") consoleLog("info", " rxfax receiving from " + the_caller + " for destination " + the_dest + ", uuid is " + the_uuid + "\n") #answer the phone, save the fax file in .tiff format. session.answer() session.execute("playback", "silence_stream://2000") session.execute("rxfax", incomingfaxes + "rxfax-" + the_uuid + ".tiff") #try to make the tiff file into a postscript file error = os.system(tiff2pscmd + " " + tiff2psoptions + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".tiff") #if making the postscript worked, let's try making the pdf if error == 0: os.system(ps2pdfcmd + " " + ps2pdfoptions + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".pdf") #let's delete the .ps and .tiff now if error == 0: os.system('rm -f ' + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".tiff") consoleLog("info", " rxfax receiving from " + the_caller + " for destination " + the_dest + ", SUCCESSFULLY RECEIVED, processing" + "\n") #assemble full pdf file name with path pdffile = incomingfaxes + "rxfax-" + the_uuid + ".pdf" consoleLog("debug", " rxfax pdf file name is " + pdffile + "\n") the_subject = "New Incoming Fax from " + the_caller + " to " + the_dest #make the email data outer = MIMEMultipart() consoleLog("debug", " rxfax MIMEMultipart() called\n") outer['Subject'] = the_subject consoleLog("debug", " rxfax set subject to " + the_subject + "\n") outer['From'] = send_from consoleLog("debug", " rxfax set FROM to " + send_from + "\n") outer['To'] = the_recipient consoleLog("debug", " rxfax set TO to " + the_recipient + "\n") outer.preamble = 'A new fax has been received and is attached to this email.' consoleLog("debug", " rxfax creating email from=" + the_caller + ", to=" + the_recipient + ", dest=" + the_dest + ", sent from=" + send_from + "\n") # the code attempts to guess the mimetype # ctype, encoding = mimetypes.guess_type(pdffile) consoleLog("debug", " rxfax PDF ctype is " + ctype + "\n") if ctype is None or encoding is not None: # If no guess could be made ctype = 'application/octet-stream' consoleLog("debug", " rxfax executed: ctype = 'application/octet-stream'\n") maintype, subtype = ctype.split('/', 1) consoleLog("debug", " rxfax executed: maintype, subtype = ctype.split('/', 1)'\n") fp = open(pdffile, 'rb') consoleLog("debug", " rxfax opened pdf file\n") msg = MIMEBase(maintype, subtype) consoleLog("debug", " rxfax executed: msg = MIMEBase(maintype, subtype)\n") msg.set_payload(fp.read()) consoleLog("debug", " rxfax executed: msg.set_payload(fp.read())\n") fp.close() consoleLog("debug", " rxfax executed: fp.close()\n") # Encode the payload using Base64 Encoders.encode_base64(msg) consoleLog("debug", " rxfax executed: Encoders.encode_base64(msg)\n") # Set the filename parameter consoleLog("debug", " rxfax setting filename on attachment header (msg.add_header) \n") ## CHANGE LINE BELOW TO CUSTOMIZE FILENAME USER SEES msg.add_header('Content-Disposition', 'attachment', filename="Fax from " + the_caller + ".pdf") consoleLog("debug", " rxfax attempting to attach file to message\n") outer.attach(msg) consoleLog("debug", " rxfax finished creating email from " + the_caller + " for " + the_recipient + "\n") #try to email the pdf composed = outer.as_string() s = smtplib.SMTP(smtp_server, smtp_port) s.sendmail(send_from, the_recipient, composed) s.quit() consoleLog("info", " rxfax email sent to " + the_recipient + " from " + the_caller + "\n") #let's assume that we were able to send. let's now delete the pdf. os.system('rm -f ' + pdffile) #hooray! you should have just been emailed a fax. #return