[Freeswitch-users] Fax 2 Email issue

Bipin Patel bipin at xbipin.com
Wed Dec 7 17:01:13 MSK 2016


An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20161207/4a704da6/attachment.html 
-------------- next part --------------
#!/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.
#
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/bin/tiff2ps'		# location of tiff2ps
ps2pdfcmd = '/usr/bin/ps2pdf'		# location of ps2pdf
incomingfaxes = '/usr/local/freeswitch/fax/'	# change this to where your .tiffs are saved by mod_fax, trailing slash required
send_from = 'from at gmail.com'	# email address to put in From: header of email
smtp_server = 'smtp.gmail.com'	# change to your SMTP server - authentication/ssl not yet implemented
smtp_port = 587
 
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("sip_from_user")
	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
	error2 = os.system("/bin/cp " + incomingfaxes + "rxfax-" + the_uuid + ".tiff" + " /tmp/")
	consoleLog("info", "/bin/cp " + incomingfaxes + "rxfax-" + the_uuid + ".tiff" + " /tmp/" + "\n")
	error = os.system(tiff2pscmd + " " + tiff2psoptions + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".tiff")
	consoleLog("info","Running: " + tiff2pscmd + " " + tiff2psoptions + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".tiff" + "\n");
	consoleLog("info","Tiff error " + str(error) + "\n")
	#if making the postscript worked, let's try making the pdf
	if error == 0:
		consoleLog("info","Running: "+ps2pdfcmd + " " + ps2pdfoptions + incomingfaxes + "rxfax-" + the_uuid + ".ps " + incomingfaxes + "rxfax-" + the_uuid + ".pdf" + "\n")
		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.starttls()
	s.login(send_from, "password")
	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


Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users mailing list