3

I have a very simple procmail process that takes an inbound email and redirects it to a script:

LOGFILE=/home/foo/procmail-log VERBOSE=yes MAILDIR=/var/spool/mail/foo DEFAULT=/var/spool/mail/foo SHELL=/bin/sh :0 ! `/home/foo/scripts/blah/blah.sh` 

The shell script has a condition that greps a file for a sender's email address and if it finds it, it exits:

grep i "$SENDER" /home/foo/scripts/blah/blah2.txt if [[ $? -eq 0 ]] ; then exit 1 fi 

When this happens, I want procmail to just give up / discard the message.

Interestingly enough though, if this triggers, procmail taking the value I just grepped and trying to send an email to it.

In the procmail log, I see:

procmail: Notified comsat: "foo@:/usr/sbin/sendmail -oi [email protected] [email protected]" From [email protected] Thu Feb 6 22:31:47 2014 Subject: Test Folder: /usr/sbin/sendmail -oi [email protected] sender 3165 procmail: Executing "/usr/sbin/sendmail,-oi,[email protected],[email protected]" 

Why is it doing this? Is there any way I can configure procmail to not do this? I just want it to give up and drop the prior email it was processing. I've tried different exit codes (0, 1, 77) but procmail always does the same thing. Any ideas?

1
  • It is extremely unclear what you are actually trying to accomplish. Could you explain what action Procmail should be taking, in your opinion? Commented Feb 10, 2014 at 17:22

2 Answers 2

3

Procmail makes great efforts to assure that mail is not lost even if delivery fails. According to man procmail, email will be bounced back to sender as a last resort:

 ORGMAIL Usually the system mailbox (ORiGinal MAIL‐ box). If, for some obscure reason (like `filesystem full') the mail could not be delivered, then this mailbox will be the last resort. If procmail fails to save the mail in here (deep, deep trouble :-), then the mail will bounce back to the sender 

There is, however, an environment variable that can be set to allow mail to be discarded rather than bounced:

 DELIVERED If set to `yes' procmail will pretend (to the mail agent) the mail has been deliv‐ ered. If mail cannot be delivered after having met this assignment (set to `yes'), the mail will be lost (i.e., it will not bounce). 
0

Let's examine your action:

:0 ! `script` 

The meaning of the backticks is "use the output of script as a string here". So if your script outputs [email protected] then the recipe will effectively be equivalent to

:0 ! [email protected] 

which means to forward the message to [email protected]. The behavior you describe should thus not be surprising at all; Procmail is doing precisely what you are telling it to do.

If you just want to discard the message altogether if your script returns success, you can try this trick:

:0W | script 

This is basically the same as the formail -D example for suppressing duplicates; if the script returns a success exit status, the message is considered delivered; but since the script doesn't actually deliver it anywhere, it is actually lost. (You will obviously need to change the logic of your script so that its exit code is the inverse of what it is now.)

In case you want the syntax sorted out for you, here is a more general attempt at doing what you seem to think you were doing:

:0 ? script ! somewhere 

which will forward to somewhere if the exit code of script is zero (as in success); or possibly

# Capture the output from script in WHERE WHERE=`script` # Also capture its exit code ERR=$? # Forward to $WHERE if $ERR is zero :0 * ERR ?? ^^0^^ ! $WHERE 

(Several arcane details of uncommon Procmail syntax are present here. The ?? operand examines the value of the named variable; the ^^ anchor matches beginning / end of input string.)

If your script is no more complex than that, you might as well inline it. (Notice the syntax fix with a dash before the i option, and the inversion of the exit status with !):

:0 * ! ? grep -i "$SENDER" /home/foo/scripts/blah/blah2.txt ! somewhere 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.