Procmail recipe to forward email is rejected by recipient

emailprocmail

This is the first time using procmail and I've got the following recipe,

SENDMAIL=/opt/zimbra/postfix/sbin/sendmail
SHELL=/bin/sh
PATH=$HOME/bin:/usr/bin:/bin:.
MAILDIR=/opt/zimbra/procmail/
DEFAULT=$MAILDIR
LOGFILE=.procmaillog
LOCKFILE=.procmaillock
VERBOSE=yes

#Get the recipient's address
TO_=`formail -xTo: \
         | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

#Get the recipient's local-part, e.g. fax number
FAXNO_=`echo "${TO_}" | awk -F@ '{ print $1 }'`


LOCALPART=${FAXNO_}
DOMAIN=faxservice.com

# Forward the email
:0
! ${LOCALPART}@${DOMAIN}

The recipe gets the TO email header, parses it and builds the forwarding email address.

PROBLEM

The problem with the recipe is that the email is rejected by the recipient because the Return-Path header is modified.

TRIED SOLUTIONS

  1. I added the following to the top of the recipe,

    SENDMAILFLAGS="-oi -f \"$SENDER\""

    this makes the Return-Path header blank and the recipient still rejects the email.

  2. I found this serverfault question and modified my recipe as follows,

    # Forward the email
    :0
    * ^Return-Path:[   ]*\/[^  ].+
    { env=$MATCH }
    :0
    ! ${env+-f "$env"}
    ! ${LOCALPART}@${DOMAIN}
    

    procmail's log file shows an error that there is no match,

    procmail: No match on "^Return-Path:[   ]*\/[^  ].+"
    

QUESTIONS

I don't know whether to make a change to the postfix pipe,

   procmail-fax-send unix    -   n   n   -   -   pipe
   flags=    user=zimbra argv=/usr/bin/procmail  /opt/zimbra/procmail/procmailrc

Or how to change my recipe, so that the original sender is not modified after procmail processes the email.

Best Answer

Without attempting to address the Postfix part of the question, here is an attempt at fixing the Procmail problems and doing away with the multiple external processes just to extract the token before the @ sign in the To: address.

# Do you really need to muck with SENDMAIL?
SENDMAIL=/opt/zimbra/postfix/sbin/sendmail
SHELL=/bin/sh
# Don't muck with PATH
#PATH=$HOME/bin:/usr/bin:/bin:.
MAILDIR=/opt/zimbra/procmail/
# Don't muck with DEFAULT
#DEFAULT=$MAILDIR
LOGFILE=.procmaillog
VERBOSE=yes

# Get the local part of the recipient's address
:0  # note: whitespace is [ ^ space tab @ ]
* ^To:.*\<\/[^  <>@]+@
{
  :0
  * MATCH ?? ()\/[^@]+
  { LOCALPART=$MATCH }
}
DOMAIN=faxservice.com

# Forward the email
:0  # note: whitespace is [ space tab ] and [ ^ space tab ]
* ^Return-Path:[    ]*\/[^  ].+
{ env=$MATCH }
:0
! ${env+-f "$env"} ${LOCALPART}@${DOMAIN}

If there is no Return-Path: header, the env assignment should end up empty, and the message should be forwarded with whatever envelope sender your Postfix generates.

(ServerFault's Markdown renderer will replace tabs with spaces, so you can't entirely correctly copy/paste this. The requirement to include the tab in the whitespace is a corner case so for quick testing this is probably unimportant.)