Browse DevX
Sign up for e-mail newsletters from DevX


Cook Up Powerful Mail-Processing Scripts with Jython Procmail Recipes : Page 2

Learn how to create Jython Procmail recipes, powerful mail-processing scripts that insert selected values into a database. Their ability to easily hook into existing Java APIs also makes recipes simple to implement.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Parse and Store Data of Interest

Jython handles standard in as a file-like object. The main loop of the Jython script is as follows:

RCPT=RE("RCPT To:.*") #pattern to match in message body lines=sys.stdin.readlines( ) # this is the body of the email message #Python and Jython have a simple, clean syntax that relies on indentations
rather than braces to demarcate blocks of code for line in lines:
#start looping through message body if(RCPT.match(line)==1): rParts=line.split('<') rcptUser=rParts[1][:-1] f.write("RCPT: " + rcptUser + "\n") #debug log statement

Within this main loop, patterns in the message body are identified using the Jakarta Regexp Java API. Again, since Jython is implemented in Java and seamlessly interfaces with Java APIs, you need to ensure only that the requisite .jar file is on the Jython classpath in order to access third-party Java libraries. The initial imports for this script include both the Jakarta Regexp libraries and the required JDBC classes from the standard Oracle classes12.jar file:

from oracle.jdbc.driver import OracleDriver from java.sql import DriverManager from java.sql import Connection from java.sql import Statement from java.sql import ResultSet from java.sql import SQLException from java.lang import Class from org.apache.regexp import RE import sys

Finally, after parsing out the data of interest using regular expressions, the script assembles an insert statement string. Be careful when concatenating the text of such a statement. Oracle requires single quotes around varchar type data, and if you're going to include variables defined elsewhere in the script, you have to be sure that they aren't enclosed in quotes. Here's a simple example where recipient and bounceDate are variables assigned values earlier in the script:

ddl="insert into table_email_bounce (email_address, bounce_date)
values(lower('" + str(recipient) + "'), to_date
('" + str(bounceDate) + "', 'Dy, DD Mon YYYY HH24:MI:SS'))" f.write(ddl) st.executeQuery(ddl) con.close()

Customize Your Messaging Environment

Procmail is a versatile, easy-to-use, email message-processing utility. When configured with an MTA, Procmail provides email-processing capabilities such as mail forwarding and also offers a way to pass messages to the standard in of a custom processing script. Jython's ability to access Java libraries coupled with Procmail's local delivery services enables you to tailor your messaging environment to meet your needs.

Doug Tillman is a veteran Java and Python developer turned Scrum Master.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date