10

I have a pretty usual requirement with procmail but I am unable to get the results somehow. I have procmailrc file with this content:

:0
* ^To.*@myhost
| /usr/bin/python /work/scripts/privilege_emails_forward.py

Wherein my custom python script(privilege_emails_forward.py) will be scanning through the email currently received and do some operations on the mail content. But I am unable to get the script getting executed at the first shot(let alone scanning through the mail content).

  • Is this a correct way of invoking an external program(python) as soon as new mail arrives?
  • And how does my python program(privilege_emails_forward.py) will receive the mail as input? I mean as sys.argv or stdin????
tripleee
  • 175,061
  • 34
  • 275
  • 318
Nanda Kishore
  • 2,789
  • 5
  • 38
  • 61
  • Though **procmail** may work, since **procmail** hasn't had active development since 2001, and [the developer has encouraged people to find other solutions](http://marc.info/?l=openbsd-ports&m=141634350915839&w=2), do any alternatives exists which also will run a script on receipt of a new message? – palswim Nov 01 '18 at 20:19

2 Answers2

11

That is just fine, just put fw after :0 (:0 fw). Your python program will receive the mail on stdin. You have to 'echo' the possibly transformed mail on stdout.

fw means:

  • f Consider the pipe as a filter.
  • w Wait for the filter or program to finish and check its exitcode (normally ignored); if the filter is unsuccessful, then the text will not have been filtered.

My SPAM checker (bogofilter) just works like that. It adds headers and later procmail-rules do something depending on these headers.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • I tried that too. But still it doesn't work I dont know why :( Here is my procmailrc script: :0 fw * ^To.*@myhost | /usr/bin/python /work/scripts/privilege_emails_forward.py – Nanda Kishore Feb 17 '09 at 18:23
  • try VERBOSE=yes and have a look in your procmail log file. Perhaps you can spot the error! – Johannes Weiss Feb 17 '09 at 18:53
  • Johannes! Since this comment text field is not enough to print the procmail log output, I have given the output as an ANSWER. please have a look at it. – Nanda Kishore Feb 18 '09 at 13:23
  • Johannes! I have checked the error log, and one of the things which caught my eye is: Executing "/usr/bin/python,/work/scripts/privilege_emails_forward.py" Why is there a comma between /usr/bin/python and my custom script. First of all is my procmailrc correct? – Nanda Kishore Feb 18 '09 at 14:03
5

The log excerpt clearly states that your script is executed, even if it doesn't show the desired effect. I'd expect procmail to log an error if the execution failed.

Anyway, make sure that the user (uid) that procmail is executed with has the correct permissions to execute your script. Wire the script into procmail only if you succeeded testing with something like this (replace 'procmail' with the correct uid):

# sudo -u procmail /bin/sh -c '/bin/cat /work/scripts/mail.txt | /usr/bin/python /work/scripts/privilege_emails_forward.py'

Depending on your sudo configuration, you'd have to run this as root. Oh, and make sure you use absolute file paths.

Community
  • 1
  • 1
paprika
  • 2,424
  • 26
  • 46
  • Awesome paprika! I tried to run the script you gave with procmail replaced with webmail(current user), My script got executed. But when I receive a mail(be it from anybody),my procmail runs the procmailrc right? So who will be the correct user to run the script? – Nanda Kishore Feb 18 '09 at 14:15
  • Paprika! To explain clearly, my custom python script has got enough permissions(777), still its not getting executed automatically when I receive a mail – Nanda Kishore Feb 18 '09 at 14:17
  • 1
    To find out as which user your procmail script is run, replace the script's line in your procmailrc with this: '| /usr/bin/id -un > /tmp/procmailuser' Don' forget the pipe symbol in front! Afterwards, check 'cat /tmp/procmailuser' should reveal the user. – paprika Feb 19 '09 at 00:25