Want procmail to run a custom python script, everytime a new mail shows up
Asked Answered
P

2

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????
Polyandry answered 17/2, 2009 at 17:37 Comment(1)
Though procmail may work, since procmail hasn't had active development since 2001, and the developer has encouraged people to find other solutions, do any alternatives exists which also will run a script on receipt of a new message?Sacramental
R
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.

Residuum answered 17/2, 2009 at 17:45 Comment(4)
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.pyPolyandry
try VERBOSE=yes and have a look in your procmail log file. Perhaps you can spot the error!Residuum
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.Polyandry
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?Polyandry
C
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.

Cadence answered 18/2, 2009 at 14:1 Comment(3)
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?Polyandry
Paprika! To explain clearly, my custom python script has got enough permissions(777), still its not getting executed automatically when I receive a mailPolyandry
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.Cadence

© 2022 - 2024 — McMap. All rights reserved.