Sorting e-mail into queues automatically using procmail

This is under the assumption that all queues are correctly setup and e-mail enabled through the web interface, and the reply address has format @rt.mydomain.com and comment address has format -comment@rt.mydomain.com.

I’m trying to get incoming e-mail to automatically go directly into whichever queue/ticket they are related to or create a new ticket if no matching ones exist.

I will have too many queues to have two line items within mailgate or the aliases file per queue.

I’m able to send and receive e-mail (via PostFix) to the default rt user and this user successfully accepts all e-mail for the rt.mydomain.com domain.

Since enabling procmail and the following scripts, I have no idea where the e-mail goes - it’s successfully delivered, but it does not update existing tickets (with a Subject line match) and it does not create any new. It’s not even in rt’s mailbox.

Here’s an example of my procmail.log:

— Logging /home/rt/procmail_log.log

for rt,

procmail: [8153] Mon Aug 23 21:30:56 2010

procmail: Assigning "MAILDOMAIN=rt.mydomain.com

"

procmail: Assigning "RT_MAILGATE=/opt/rt3/bin/rt-mailgate

"

procmail: Assigning "RT_URL=http://rt.mydomain.com/

"

procmail: Assigning "LOGABSTRACT=all

"

procmail: Skipped "

"

procmail: Skipped "

"

procmail: Assigning "LASTFOLDER={

"

procmail: Opening "{

"

procmail: Acquiring kernel-lock

procmail: Notified comsat: "rt@35475:./{

"

From me@mydomain.com Mon Aug 23 21:30:56 2010

Folder: {

The contents of my ./procmailrc:

#Preliminaries

            SHELL=/bin/sh               #Use the Bourne shell (check your path!)

            #MAILDIR=${HOME}        #First check what your mail directory is!

            MAILDIR="/var/mail/rt/"

            #LOGFILE=${MAILDIR}/procmail.log

            LOGFILE="/home/rt/procmail_log.log"

            LOG="--- Logging ${LOGFILE} for ${LOGNAME}, "

            VERBOSE=yes

            MAILDOMAIN="rt.telebarbados.com"

            RT_MAILGATE="/opt/rt3/bin/rt-mailgate"

            #RT_MAILGATE="/usr/local/bin/rt-mailgate"

            RT_URL="http://rt.telebarbados.com/"



            LOGABSTRACT=all





            :0

            {

            # the following line extracts the recipient from Received-headers.

            # Simply using the To: does not work, as tickets are often created

            # by sending a CC/BCC to RT

            TO=`formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'`

            QUEUE=`echo $TO| $HOME/get_queue.pl`

            ACTION=`echo $TO| $HOME/get_action.pl`

            :0 h b w 

            |/usr/bin/perl $RT_MAILGATE --queue $QUEUE --action $ACTION --url $RT_URL

            }

I have tested get_queue.pl and get_action.pl scripts successfully but let me know if including them helps.

Any help and/or guidance you can give would be greatly appreciated.

Kind regards,
Nicôle

This is under the assumption that all queues are correctly setup and e-mail enabled through
the web interface, and the reply address has format @rt.mydomain.com and comment
address has format -comment@rt.mydomain.com.

I’m trying to get incoming e-mail to automatically go directly into whichever queue/ticket
they are related to or create a new ticket if no matching ones exist.

I will have too many queues to have two line items within mailgate or the aliases file per
queue.

I’m able to send and receive e-mail (via PostFix) to the default rt user and this user
successfully accepts all e-mail for the rt.mydomain.com domain.

Since enabling procmail and the following scripts, I have no idea where the e-mail goes - it’s
successfully delivered, but it does not update existing tickets (with a Subject line match)
and it does not create any new. It’s not even in rt’s mailbox.

Your mail is full of extra newlines, so it is really hard to tell what
is going on, but do you have a { file in your user’s homedir
containing the mail content?

-kevin

Update (still not working):

I believe main error to be this, although the mailgate file is there and contains the default content.

Can’t open perl script "/opt/rt3/bin/rt-mailgate
": No such file or directory
procmail: Error while writing to “/usr/bin/perl”
procmail: Program failure (2) of “/usr/bin/perl”
procmail: Assigning "LASTFOLDER=/usr/bin/perl /opt/rt3/bin/rt-mailgate
–queue general
–action correspond
–url http://rt.mydomain.com/

Thanks for your suggestion Stucki.

Here’s what the file currently looks like, not seeing any funny characters or spaces from cmd line (all the # are from me testing different portions, will clean up once I get it working).

more /home/rt/.procmailrc
::::::::::::::
/home/rt/.procmailrc
::::::::::::::
#Preliminaries
SHELL=/bin/sh #Use the Bourne shell (check your path!)
#MAILDIR=${HOME} #First check what your mail directory is!
MAILDIR=“/var/mail/rt/”
#LOGFILE=${MAILDIR}/procmail.log
LOGFILE=“/home/rt/procmail_log.log”
LOG=“— Logging ${LOGFILE} for ${LOGNAME}, "
VERBOSE=yes
MAILDOMAIN=“rt.mydomain.com
RT_MAILGATE=”/opt/rt3/bin/rt-mailgate"
#RT_MAILGATE=“/usr/local/bin/rt-mailgate”
RT_URL=“http://rt.mydomain.com/

LOGABSTRACT=all
#QUEUE=`echo $TO| $HOME/get_queue.pl`
#ACTION=`echo $TO| $HOME/get_action.pl`

#:0
#{
# the following line extracts the recipient from Received-headers.
# Simply using the To: does not work, as tickets are often created
# by sending a CC/BCC to RT
#TO=`formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'`
TO=`formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'`
QUEUE=`echo $TO| $HOME/get_queue.pl`
ACTION=`echo $TO| $HOME/get_action.pl`
:0 h b w 
|/usr/bin/perl $RT_MAILGATE --queue $QUEUE --action $ACTION --url $RT_URL
#}

I made some other changes since my original e-mail and here is an output of the logs:

— Logging /home/rt/proc.log
for rt,
procmail: [18543] Thu Aug 26 11:08:41 2010
procmail: Assigning "MAILDOMAIN=rt.mydomain.com
"
procmail: Assigning "RT_MAILGATE=/opt/rt3/bin/rt-mailgate
"
procmail: Assigning "RT_URL=http://rt.mydomain.com/
"
procmail: Assigning "LOGABSTRACT=all
"
procmail: Executing “formail -c -xReceived: |grep $MAILDOMAIN |sed -e ‘s/.for <(.)>* ;.$/\1/’”
procmail: Assigning "TO=
"
procmail: Executing “echo $TO| $HOME/get_queue.pl”
procmail: Assigning "QUEUE=general
"
procmail: Executing “echo $TO| $HOME/get_action.pl”
procmail: Assigning "ACTION=correspond
"
procmail: Skipped "
"
procmail: Executing "/usr/bin/perl,/opt/rt3/bin/rt-mailgate
,–queue,general
,–action,correspond
,–url,http://rt.mydomain.com/

"
Can’t open perl script "/opt/rt3/bin/rt-mailgate
": No such file or directory
procmail: Error while writing to “/usr/bin/perl”
procmail: Program failure (2) of “/usr/bin/perl”
procmail: Assigning "LASTFOLDER=/usr/bin/perl /opt/rt3/bin/rt-mailgate
–queue general
–action correspond
–url http://rt.mydomain.com/

"
procmail: Locking “/var/mail/rt.lock”
procmail: Assigning “LASTFOLDER=/var/mail/rt”
procmail: Opening “/var/mail/rt”
procmail: Acquiring kernel-lock
procmail: Unlocking “/var/mail/rt.lock”
procmail: Notified comsat: “rt@85426:/var/mail/rt”>From me@mydomain.com Thu Aug 26 11:08:41 2010
Subject: Testing to internal
Folder: /var/mail/rt 6147

Syslog:
Aug 26 11:08:41 RT postfix/smtpd[18537]: connect from [local Exchange server]
Aug 26 11:08:41 RT postfix/smtpd[18537]: 599F948096: client=[local Exchange server]

Aug 26 11:08:41 RT postfix/qmgr[5063]: 599F948096: from=nlayne@mydomain.com, size=6163, nrcpt=1 (queue active)
Aug 26 11:08:41 RT postfix/smtpd[18537]: disconnect from [local Exchange server]
Aug 26 11:08:41 RT postfix/local[18542]: 599F948096: to=rt@RT.mydomain.com, orig_to=internal@rt.mydomain.com, relay=local, delay=0.29, delays=0.1/0.03/0/0.16, dsn=2.0.0, status=sent (delivered to command: /usr/bin/procmail)

Aug 26 11:08:41 RT postfix/bounce[18541]: 599F948096: sender delivery status notification: 984B748098
Aug 26 11:08:41 RT postfix/qmgr[5063]: 599F948096: removed
Aug 26 11:08:41 RT postfix/qmgr[5063]: 984B748098: from=<>, size=2642, nrcpt=1 (queue active)
Aug 26 11:08:50 RT postfix/smtp[18558]: 984B748098: to=nlayne@mydomain.com, relay=10.0.0.44[10.0.0.44]:25, delay=8.9, delays=0.02/0.04/0.02/8.8, dsn=2.6.0, status=sent (250 2.6.0 20100826150841.984B748098@RT.mydomain.com Queued mail for delivery)
Aug 26 11:08:50 RT postfix/qmgr[5063]: 984B748098: removed

Kind regards,
Nicôle

From: Chr. von Stuckrad [mailto:stucki@mi.fu-berlin.de]
Sent: Thursday, August 26, 2010 5:12 AM
To: Nicôle Layne
Subject: Re: [rt-users] Sorting e-mail into queues automatically usingprocmail

Hi!

Seeing the strange “somethimg” pattern in the log lines
and seeing the definitley broken single ‘{’ for ‘LASTFOLDER’
reminds me of the time, when one of our Users edited his .procmailrc
with an WINDOWS-Editor inserting ‘’ where
procmail only understands ‘’ for the line-endings.

Lots of strange things then happened, because listings do not
show the difference, but the ‘character’ was used for Filenames
and such…

So you may look into your procmailrc with a tool showing ‘really
everything’ (UNIX: e.g.‘xxd’) to check this.
Or load the file into an editor which can explicitely write it
with ‘only newline’ == UNIX-line-end.

May be this helps … ?

Stucki

— Logging /home/rt/procmail_log.log
… … …
procmail: Assigning "LASTFOLDER={

"

procmail: Opening "{

"

Just letting everyone know that I got it to work.

Here are my config and script files in case this can help anyone.

/home/rt/.procmailrc

#Preliminaries
SHELL=/bin/sh #Use the Bourne shell (check your path!)
MAILDIR=“/var/mail/rt/”
LOGFILE=“/home/rt/proc.log”
LOG=“— Logging ${LOGFILE} for ${LOGNAME}, "
VERBOSE=yes
MAILDOMAIN=“rt.mydomain.com
#Made no changes to the default rt-mailgate file
RT_MAILGATE=”/opt/rt3/bin/rt-mailgate"
RT_URL=“http://rt.mydomain.com/
LOGABSTRACT=all
# the following line extracts the recipient from Received-headers.
# Simply using the To: does not work, as tickets are often created
# by sending a CC/BCC to RT
TO=formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'
QUEUE=echo $TO| $HOME/get_queue.pl
ACTION=echo $TO| $HOME/get_action.pl
:0 h b w
|/usr/bin/perl $RT_MAILGATE --queue $QUEUE --action $ACTION --url $RT_URL

/root/get_action.pl

#!/usr/bin/env perl

@arr = <STDIN>;
$action = "correspond";
foreach (@arr) {
	if (/\s*.*<([^@]+)-comment@.*>/g) {
		$action = "comment";
	} else { 
		if (/\s*([^@]+)-comment@.*/g) {
			$action = "comment";
		} 
	}
}
print "$action";

/root/get_queue.pl

#!/usr/bin/env perl

@arr = <STDIN>;
$queue = 'general';
foreach (@arr) {
	if (/\s*.*<([^@]+)@.*>/g) {
		$queue = $1;
		} else { 
			if (/\s*([^@]+)@.*/g) {
				$queue= $1;
			} 
		}
	}
if ($queue =~ /(.*)-comment/)
{
	$queue = $1;
}

print “$queue”;

/opt/rt3/etc

Set($rtname , “RT.mydomain.com”);
Set($Organization , “rt.mydomain.com”);
Set($Timezone , ‘US/Eastern’);
Set($DatabaseType , ‘mysql’);
Set($DatabaseHost , ‘localhost’);
Set($DatabaseRTHost , ‘localhost’);
Set($DatabasePort , ‘’);
Set($DatabaseUser , ‘rt’);
Set($DatabasePassword , ‘whateveryoulike’);
Set($OwnerEmail , ‘root’);
Set($RTAddressRegexp , ‘^rt@rt.mydomain.com$’);
Set($ValidateUserEmailAddresses, 1);
Set($CorrespondAddress , ‘no-reply@rt.mydomain.com’);
Set($CommentAddress , ‘no-reply@rt.mydomain.com’);
Set( $WebDomain, ‘rt.mydomain.com’ );
Set($WebPath, “”);
Set($CanonicalizeRedirectURLs, 1);
Set($LogToSyslog , ‘info’);
Set($UnsafeEmailCommands,1);
Set($LogToSyslog, “debug”);
Set(@Plugins,qw(RT::FM));

1;

Kind regards,
NicôleFrom: Nicôle Layne
Sent: Thursday, August 26, 2010 11:19 AM
To: rt-users@lists.bestpractical.com
Subject: RE: [rt-users] Sorting e-mail into queues automatically using procmail

Update (still not working):

I believe main error to be this, although the mailgate file is there and contains the default content.

Can’t open perl script "/opt/rt3/bin/rt-mailgate
": No such file or directory
procmail: Error while writing to “/usr/bin/perl”
procmail: Program failure (2) of “/usr/bin/perl”
procmail: Assigning "LASTFOLDER=/usr/bin/perl /opt/rt3/bin/rt-mailgate
–queue general
–action correspond
–url http://rt.mydomain.com/

Thanks for your suggestion Stucki.

Here’s what the file currently looks like, not seeing any funny characters or spaces from cmd line (all the # are from me testing different portions, will clean up once I get it working).

more /home/rt/.procmailrc
::::::::::::::
/home/rt/.procmailrc
::::::::::::::
#Preliminaries
SHELL=/bin/sh #Use the Bourne shell (check your path!)
#MAILDIR=${HOME} #First check what your mail directory is!
MAILDIR=“/var/mail/rt/”
#LOGFILE=${MAILDIR}/procmail.log
LOGFILE=“/home/rt/procmail_log.log”
LOG=“— Logging ${LOGFILE} for ${LOGNAME}, "
VERBOSE=yes
MAILDOMAIN=“rt.mydomain.com
RT_MAILGATE=”/opt/rt3/bin/rt-mailgate"
#RT_MAILGATE=“/usr/local/bin/rt-mailgate”
RT_URL=“http://rt.mydomain.com/

LOGABSTRACT=all
#QUEUE=`echo $TO| $HOME/get_queue.pl`
#ACTION=`echo $TO| $HOME/get_action.pl`

#:0
#{
# the following line extracts the recipient from Received-headers.
# Simply using the To: does not work, as tickets are often created
# by sending a CC/BCC to RT
#TO=`formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'`
TO=`formail -c -xReceived: |grep $MAILDOMAIN |sed -e 's/.*for *<*\(.*\)>* *;.*$/\1/'`
QUEUE=`echo $TO| $HOME/get_queue.pl`
ACTION=`echo $TO| $HOME/get_action.pl`
:0 h b w 
|/usr/bin/perl $RT_MAILGATE --queue $QUEUE --action $ACTION --url $RT_URL
#}

I made some other changes since my original e-mail and here is an output of the logs:

— Logging /home/rt/proc.log
for rt,
procmail: [18543] Thu Aug 26 11:08:41 2010
procmail: Assigning "MAILDOMAIN=rt.mydomain.com
"
procmail: Assigning "RT_MAILGATE=/opt/rt3/bin/rt-mailgate
"
procmail: Assigning "RT_URL=http://rt.mydomain.com/
"
procmail: Assigning "LOGABSTRACT=all
"
procmail: Executing “formail -c -xReceived: |grep $MAILDOMAIN |sed -e ‘s/.for <(.)>* ;.$/\1/’”
procmail: Assigning "TO=
"
procmail: Executing “echo $TO| $HOME/get_queue.pl”
procmail: Assigning "QUEUE=general
"
procmail: Executing “echo $TO| $HOME/get_action.pl”
procmail: Assigning "ACTION=correspond
"
procmail: Skipped "
"
procmail: Executing "/usr/bin/perl,/opt/rt3/bin/rt-mailgate
,–queue,general
,–action,correspond
,–url,http://rt.mydomain.com/

"
Can’t open perl script "/opt/rt3/bin/rt-mailgate
": No such file or directory
procmail: Error while writing to “/usr/bin/perl”
procmail: Program failure (2) of “/usr/bin/perl”
procmail: Assigning "LASTFOLDER=/usr/bin/perl /opt/rt3/bin/rt-mailgate
–queue general
–action correspond
–url http://rt.mydomain.com/

"
procmail: Locking “/var/mail/rt.lock”
procmail: Assigning “LASTFOLDER=/var/mail/rt”
procmail: Opening “/var/mail/rt”
procmail: Acquiring kernel-lock
procmail: Unlocking “/var/mail/rt.lock”
procmail: Notified comsat: “rt@85426:/var/mail/rt”

From me@mydomain.com Thu Aug 26 11:08:41 2010
Subject: Testing to internal
Folder: /var/mail/rt 6147

Syslog:
Aug 26 11:08:41 RT postfix/smtpd[18537]: connect from [local Exchange server]
Aug 26 11:08:41 RT postfix/smtpd[18537]: 599F948096: client=[local Exchange server]

Aug 26 11:08:41 RT postfix/qmgr[5063]: 599F948096: from=nlayne@mydomain.com, size=6163, nrcpt=1 (queue active)
Aug 26 11:08:41 RT postfix/smtpd[18537]: disconnect from [local Exchange server]
Aug 26 11:08:41 RT postfix/local[18542]: 599F948096: to=rt@RT.mydomain.com, orig_to=internal@rt.mydomain.com, relay=local, delay=0.29, delays=0.1/0.03/0/0.16, dsn=2.0.0, status=sent (delivered to command: /usr/bin/procmail)

Aug 26 11:08:41 RT postfix/bounce[18541]: 599F948096: sender delivery status notification: 984B748098
Aug 26 11:08:41 RT postfix/qmgr[5063]: 599F948096: removed
Aug 26 11:08:41 RT postfix/qmgr[5063]: 984B748098: from=<>, size=2642, nrcpt=1 (queue active)
Aug 26 11:08:50 RT postfix/smtp[18558]: 984B748098: to=nlayne@mydomain.com, relay=10.0.0.44[10.0.0.44]:25, delay=8.9, delays=0.02/0.04/0.02/8.8, dsn=2.6.0, status=sent (250 2.6.0 20100826150841.984B748098@RT.mydomain.com Queued mail for delivery)
Aug 26 11:08:50 RT postfix/qmgr[5063]: 984B748098: removed

Kind regards,
Nicôle

From: Chr. von Stuckrad [mailto:stucki@mi.fu-berlin.de]
Sent: Thursday, August 26, 2010 5:12 AM
To: Nicôle Layne
Subject: Re: [rt-users] Sorting e-mail into queues automatically usingprocmail

Hi!

Seeing the strange “somethimg” pattern in the log lines
and seeing the definitley broken single ‘{’ for ‘LASTFOLDER’
reminds me of the time, when one of our Users edited his .procmailrc
with an WINDOWS-Editor inserting ‘’ where
procmail only understands ‘’ for the line-endings.

Lots of strange things then happened, because listings do not
show the difference, but the ‘character’ was used for Filenames
and such…

So you may look into your procmailrc with a tool showing ‘really
everything’ (UNIX: e.g.‘xxd’) to check this.
Or load the file into an editor which can explicitely write it
with ‘only newline’ == UNIX-line-end.

May be this helps … ?

Stucki

— Logging /home/rt/procmail_log.log
… … …
procmail: Assigning "LASTFOLDER={

"

procmail: Opening "{

"