希望 procmail 运行自定义 python 脚本,每次出现新邮件时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/557906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want procmail to run a custom python script, everytime a new mail shows up
提问by None-da
I have a pretty usual requirement with procmail but I am unable to get the results somehow. I have procmailrc file with this content:
我对 procmail 有一个非常常见的要求,但我无法以某种方式获得结果。我有包含此内容的 procmailrc 文件:
: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).
其中我的自定义python脚本(privilege_emails_forward.py)将扫描当前收到的电子邮件并对邮件内容进行一些操作。但是我无法在第一次拍摄时执行脚本(更不用说扫描邮件内容了)。
- 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????
- 这是在新邮件到达时立即调用外部程序(python)的正确方法吗?
- 我的python程序(privilege_emails_forward.py)如何接收邮件作为输入?我的意思是 sys.argv 或 stdin ???
回答by Johannes Weiss
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
之后就好了。您的 python 程序将在. 您必须在 上“回显”可能已转换的邮件。:0
:0 fw
stdin
stdout
fw
means:
fw
方法:
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.
f
将管道视为过滤器。w
等待过滤器或程序完成并检查其退出代码(通常被忽略);如果过滤不成功,则文本将不会被过滤。
My SPAM checker (bogofilter) just works like that. It adds headers and later procmail-rules do something depending on these headers.
我的垃圾邮件检查器 (bogofilter) 就是这样工作的。它添加标头,然后 procmail-rules 根据这些标头执行某些操作。
回答by paprika
The log excerptclearly 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.
该日志摘录中明确指出,你的脚本执行,即使它并不显示预期的效果。如果执行失败,我希望 procmail 会记录错误。
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):
无论如何,请确保执行 procmail 的用户 (uid) 具有执行脚本的正确权限。仅当您使用类似的内容成功测试时才将脚本连接到 procmail(将 'procmail' 替换为正确的 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.
根据您的 sudo 配置,您必须以 root 身份运行它。哦,请确保您使用绝对文件路径。