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

五迷三道 提交于 2019-12-03 06:13:56

问题


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????

回答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.




回答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.



来源:https://stackoverflow.com/questions/557906/want-procmail-to-run-a-custom-python-script-everytime-a-new-mail-shows-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!