queue-message.pl
. That Perl script 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.
queue-message.pl
script
usage: queue_message.pl db_user db_passwd tag 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, tag varchar(256), content clob, -- the entire raw message content -- including all headers arrival_time date ); create sequence incoming_email_queue_sequence;The tag 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 which are addressed to user-feedback@yourhost:
/var/alias/.qmail-user-feedback: |/web/yourwebserver/packages/email-handler/bin/queue-message.pl yourdbuser yourdbpassword user_feedbackThe 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.
incoming_email_queue
Oracle table. A scheduled procedure
, process_email_queue, is launched by the email-handler at server
startup to sweep the email queue table. It will dispatch on each
message tag to call a procedure which you specify in the DispatchTable
parameter of the email-handler package.
The DispatchTable parameter should be set to a list of key-value pairs where the key matches a tag which you used in your call to queue-message.pl and the value is the name of a Tcl procedure to run on the message contents.
When installing the email handler, you must go to the Site Map on your ACS web server and instantiate a copy of the package with the name 'email-handler'. It should be mounted at /email-handler in order to edit it's parameters, although you can probably mount it at another URL and it will still work.
Once it is mounted, you will see the DispatchTable parameter, with a default value as shown:
DispatchTable => user_feedback|email_handler_testThe example above specifies that tickets with the tag "user_feedback" will be passed to the procedure
email_handler_test
.
You can set DispatchTable to a list of space-separated key-value pairs, for example
user_feedback|email_handler_test ticket|handle_ticket_tracker_email bboard|handle_bboard_mail
The Tcl procedure invoked by the dispatcher is passed one argument, a string containing the raw message text including headers. 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.
/tcl/email-utils
will help you
parse the raw mail message contents in the db.
parse_email_message message_body
email-test|email_handler_testTo do this, go to the Site Map page in your ACS admin area, and click Set Parameters on the email-handler mount point. Set the parameter DispatchTable to the above value.
% /web/yourserver/packages/email-handler/bin/queue-message.pl yourdbuser yourdbpasswd email-test this is some test email message stuff ^DThe perl script should insert an entry into the table incoming_email_queue, which should then (within 10 minutes) get handled by the email handler test procedure and stuffed into the email_handler_test table, where it can be viewed by the test web page at /email-handler on your server.
For qmail, put the following into a file named /var/qmail/alias/.qmail-acs-mail-test |/web/yourwebserver/packages/email-handler/bin/queue-message.pl yourdbuser yourdbpassword email-test
There is also a new Tcl standard library available from http://dev.scriptics.com/software/tcllib/ which includes MIME decoding routines. This is not loaded by default with ACS 4, but can be downloaded and installed if needed.
% cd perl % tar -zxvf DBD-Oracle-1.06.tar.gz % tar -zxvf DBI-1.14.tar.gz Install DBI module % cd DBI-1.14 % perl Makefile.PL % make % make test % su # make install # cd .. On Linux (and possibly Solaris), this is needed to allow the DBD Oracle driver to find the libcntlsh.so loadable module from the Oracle installation. # export LD_LIBRARY_PATH=/ora8/m01/app/oracle/product/8.1.6/lib Install Oracle Driver module # cd DBD-Oracle-1.06 # perl Makefile.PL # make # make test # su # make install # cd ..
It may be necessary to change the location of the Perl executable at the start of the test-perl-dbi and queue-message.pl scripts. To find the location of your Perl (> 5.004) executable, you can type
# type perl (if you are using bash) # perl is hashed (/usr/local/bin/perl) or # where perl (if you are using csh)Change the first line of the scripts to this location:
#!
Workaround for Problem I experienced with Linux: In the case that Perl DBI cannot resolve the Oracle shared library location for the driver, you may need to make a shell script to set the environment variable LD_LIBRARY_PATH: I defined a shell script called queue-message.sh which sets the environment for running an Oracle client:
#!/bin/bash . /etc/profile export ORACLE_BASE=/ora8/m01/app/oracle export ORACLE_HOME=$ORACLE_BASE/product/8.1.6 export PATH=$PATH:$ORACLE_HOME/bin export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib export ORACLE_SID=ora8 export ORACLE_TERM=vt100 export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data NLS_LANG=.UTF8 export NLS_LANG NLS_DATE_FORMAT=YYYY-MM-DD export NLS_DATE_FORMAT TZ=GMT export TZ exec `dirname $0`/queue-message.pl $*
For qmail, you need to create an alias file. In a standard installation this would probably be a file named /var/qmail/alias/.qmail-your-alias-address, containing a call to exec the queue-message.pl script:
|/web/yourwebserver/packages/email-handler/bin/queue-message.pl yourdbuser yourdbpassword tagname