在过滤电子邮件时,将procmail配置为与外部电子邮件地址列表匹配 [英] Configure procmail to match an external email address list when filtering emails

查看:131
本文介绍了在过滤电子邮件时,将procmail配置为与外部电子邮件地址列表匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的fetchmail脚本从电子邮件框中检索电子邮件,并将其放入名为 mario 的文件中,然后将其转储到我的/var/mail/文件夹中.我正在尝试设置一个 procmail 脚本来处理 mario ;通过处理,这就是我的意思: procmail 脚本应针对包含已知电子邮件地址列表的外部文本文件( fromlist )进行过滤.一旦匹配mario/fromlist,邮件就会从 mario 中拉出并存储到我的本地nbox/文件夹中.

My fetchmail scripts retrieves emails from an email box and puts them into a file, called mario, and dumps it into my /var/mail/ folder. I am trying to set up a procmail script to process mario; by processing, this is what I mean: the procmail script should filter against an external text file (fromlist) containing a list of known email addresses. Once there is a match mario/fromlist, the message is pulled out from mario and stored into my local nbox/ folder.

在网上,我找到了一段代码,其中包括一个配方,我已将其输入到 procmail 控制文件(.procmailrc)中,但似乎没有用.这是代码:

Online, I found a piece of code, including a recipe, that I have entered into my procmail control file (.procmailrc) but it doesn't seem to be working. This is the code:

FROMFL=$MAIL/fromlist

FROMLS=formail -xFrom: | sed -e 's/*(.*)//;s/>.*//;s/.*[:]*//'`

:0

* ? fgrep -xi $FROMLS $FROMFL

$MAIL/inbox

我认为我已经解决了sed问题(请参见我的问题 Sed命令和在线发现的未知模式),但我仍然尚未解决 formail fgrep 部分.因此,当我运行 procmail 脚本时,获得的日志是:

I think I have addressed the sed (see my question Sed command and unknown patterns found online), but I still haven't been able to address the formail and fgrep parts. So when I run the procmail script, the logs I obtain are:

$ mailstat var/log/procmail.log
/bin/sh: 0: Can't open fgrep
/bin/sh: 1: grep: not found
/bin/sh: 1: sed: not found
/home/user/var/mail/reginbox/
procmail: [6880] Sat Jun 16 16:57:32 2018
procmail: Acquiring kernel-lock
procmail: Assigning "FROMFL=/home/user/var/mail/fromlist"
procmail: Assigning "FROMLS="
procmail: Assigning "LASTFOLDER=/home/user/var/mail/reginbox/msg.XXX"
procmail: Assigning "SHELL=/bin/sh"
procmail: Executing "fgrep,-xi,/home/user/var/mail/fromlist"
procmail: Executing "formail -xFrom: | sed -e `'s/.*<//; s/>.*//'`"
procmail: No match on "fgrep -xi /home/user/var/mail/fromlist"
procmail: Non-zero exitcode (127) from "fgrep"
procmail: Notified comsat: "user@0:/home/user/var/mail/reginbox/msg.XXX"
procmail: Opening "/home/user/var/mail/reginbox/msg.XXX"

formail似乎无法完全提取发件人:"所在的行,这意味着这些行中的电子邮件地址不会被SED命令从其余部分中划出,也不会与带有电子邮件列表(发件人列表),这就是日志显示不匹配"消息的原因.

It looks as if formail cannot quite extract the lines where "From:" is located, which means that the email addresses in those lines are not carved out of the rest by the SED command and are not compared against the text file with the list of emails (fromlist), that's why the log show a "No match" message.

我如何找出这些东西在哪里分解?

How can I find out where these things break down?

推荐答案

运行外部命令的语法为

VARIABLE=`command to run`

您缺少开头的提示,因此您可以高效运行

You are missing the opening backtick, so you are running effectively

FROMLS="formail"
-xFrom: | sed etc is a syntax error

无论如何,提取发件人的方法有点不准确,因为它不能正确应对电子邮件地址格式的各种变化.一个更强大但更难理解的解决方案是

Anyway, the recipe to extract the sender is a bit inexact, because it doesn't cope correctly with various variations in email address formats. A more robust but slightly harder to understand solution is

FROMLS=`formail -rtzxTo:`

使得formail生成答复-rt,然后从生成的答复中 提取To:地址,该地址现在当然指向原始发件人.按照设计,formail仅在生成回复时将输入消息的发件人的实际电子邮件地址放在To:标头中,这就是您要提取的内容.

which makes formail generate a reply -rt, then from the generated reply extract the To: address, which of course now points back to the original sender. By design, formail only puts the actual email address of the sender of the input message in the To: header when it generates the reply, so that's what you will be extracting.

通过这种方式,您的脚本从技术上讲应该可以提取匹配的消息并将其复制到所需的目标文件夹.这是一个快速演示:

With that out of the way, your script should technically work to the point where it can extract the matching messages and copy them to the destination folder you want. Here's a quick demo:

tripleee$ cd /tmp

tripleee$ echo moo@example.com >fromlist

tripleee$ cat one.rc
# temporary hack
SHELL=/bin/sh
MAILDIR=/tmp
MAIL=.
VERBOSE=yes

FROMFL=$MAIL/fromlist

FROMLS=`formail -rtzxTo:`

:0
* ? fgrep -xi "$FROMLS" "$FROMFL"
$MAIL/inbox


tripleee$ procmail -m one.rc <<\:
From: ick@example.com
To: poo@example.org
Subject: no match

hello
:
procmail: [16406] Wed Jun 27 13:41:35 2018
procmail: Assigning "FROMFL=./fromlist"
procmail: Executing "formail,-rtzxTo:"
procmail: Assigning "FROMLS=ick@example.com"
procmail: Executing "fgrep,-xi,ick@example.com,./fromlist"
procmail: Non-zero exitcode (1) from "fgrep"
procmail: No match on "fgrep -xi ick@example.com ./fromlist"
 Subject: no match
  Folder: **Bounced**                                                        61

tripleee$ procmail -m one.rc <<\:
From: moo@example.com
To: poo@example.org
Subject: match

hello
:
procmail: [16410] Wed Jun 27 13:41:37 2018
procmail: Assigning "FROMFL=./fromlist"
procmail: Executing "formail,-rtzxTo:"
procmail: Assigning "FROMLS=moo@example.com"
procmail: Executing "fgrep,-xi,moo@example.com,./fromlist"
procmail: Match on "fgrep -xi moo@example.com ./fromlist"
procmail: Assigning "LASTFOLDER=./inbox"
procmail: Opening "./inbox"
procmail: Acquiring kernel-lock
 Subject: match
  Folder: ./inbox                                                            68    

procmail确实无法从输入文件夹中删除任何内容.如果要这样做,一个常见的解决方案是让Procmail将不匹配的消息写到另一个输出文件夹,然后将其复制回输入文件.最终结果是,原始输入文件夹中的邮件现在被分为两个文件,一个具有匹配的文件,另一个具有不匹配的消息.

There is no way really for procmail to remove anything from the input folder. If you want to do that, a common solution is to have Procmail write the non-matching messages to another output folder, then copy that back over the input file. The net effect is that the messages from the original input folder are now partitioned into two files, one with matching, and one with non-matching messages.

这篇关于在过滤电子邮件时,将procmail配置为与外部电子邮件地址列表匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆