Want procmail to run a custom python script, everytime a new mail shows up
Solution 1
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.
Solution 2
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.
Comments
-
Nanda Kishore almost 2 years
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????
-
Nanda Kishore over 15 yearsI 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
-
Johannes Weiss over 15 yearstry VERBOSE=yes and have a look in your procmail log file. Perhaps you can spot the error!
-
Nanda Kishore over 15 yearsJohannes! 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 over 15 yearsJohannes! 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 over 15 yearsAwesome 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 over 15 yearsPaprika! To explain clearly, my custom python script has got enough permissions(777), still its not getting executed automatically when I receive a mail
-
paprika over 15 yearsTo 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.