Email Handler

part of the ArsDigita Community System by Henry Minsky
System dependencies: you will be in a world of hurt unless you have Perl DBI/DBD installed (so that a Perl script on your computer can talk to Oracle) and a mailer configured to exec a procedure when mail arrives addressed to a particular alias.

The Big Picture

You can build a gateway for handling incoming email messages for your application using a perl-script called queue-message.pl. queue-message.pl will accept an incoming email message from the mailer and insert its contents into a queue table in the database. A procedure can be scheduled to sweep the queue at some interval to process the messages.

Using the queue-message.pl script

The script takes a list of command-line arguments, which tell it which database to connect to, and a classification tag for the message.

  usage: queue_message.pl db_datasrc db_user db_passwd destaddr

  Inserts the data from stdin into a queue table.

  Assumes the following table and sequence are defined in the db:

    create table incoming_email_queue (
	    id 		integer primary key,
	    destaddr	varchar(256),
	    content		clob,		-- the entire raw message content
					    -- including all headers
	    arrival_time	date
    );

    create sequence incoming_email_queue_sequence;

The destaddr field is a string tag which you can assign to a message, so that the routine which sweeps the queue can distinguish where it came from. You might use this if you had several different mail recipient aliases on your system, which all accept messages and put the into the queue.

To configure your mailer, you must add a mailer alias which invokes the script. For sendmail, this would be done in the aliases file. For qmail, you create a file in the qmail/alias directory with a name .qmail-your-alias-here.

Example: You are setting up an email handler for user feedback messages.

.qmail-ticket-handler:
|/web/yourwebserver/bin/queue-message.pl dbi:Oracle: yourdbuser yourdbpassword user_feedback
The alias above specified that incoming messages will be piped to the perl script, which will connect to the specified database, and will insert the message with the tag "user_feedback".

Some guidelines: Try to sure that the from and reply-to headers on your outgoing message are not the same as your incoming mail handler alias. This will help prevent the possibility of nasty mailer loops, in the case where messages may bounce or be returned for some reason.

Scheduled Procedures and Parsing Mail Messages

The procmail Perl script doesn't do anything except fill the incoming_email_queue Oracle table. So the file /tcl/email-queue.tcl schedules the Tcl procedure process_email_queue to sweep the queue, and will dispatch on each message tag to a procedure which you specify in the email-handler section of ad.ini.
[ns/server/photonet/acs/email-queue]
QueueSweepInterval=300
; what to do with a new message
; format is tag|tcl_proc_to_invoke
DispatchPair=na-support|ticket_process_message
The example above specifies that tickets with the tag "na-support" will be passed to the procedure ticket_process_message. The Tcl procedure invoked by the dispatcher is passed two arguments: a database connection, and the raw message text. It is up to you to parse or handle the message in any way you wish. After the call to your dispatch procedure, the message is deleted from the queue.

Email Handling Utilities

Some routines in /tcl/email-utils.tcl will help you parse the raw mail message contents in the db.
parse_email_message message_body
returns an ns_set mapping each mail header to its value, as well as a key named message_body which contains the message body text.
clean_up_html html_text
Returns the html_text with all HTML escape characters quoted properly.

Tips for Oracle 8i Achievers

Oracle 8i (8.1.5 and later) includes a Java virtual machine. You are thus able to load up a Java email parsing library that will take apart the messages in a queue very nicely (presumably more robustly than the Tcl kludges in email-utils.tcl).
hqm@arsdigita.com