简体   繁体   English

Procmail将主题替换/添加到空主题字段

[英]Procmail Replace / Add Subject to an Empty Subject Field

I have a recipe that cannot seem to match the address. 我有一个似乎与地址不匹配的食谱。

I tried these: 我试过这些:

* ^From.*address9\@gmail\.com

* ^From.*address9@gmail.com

* ^From.*address\[0-9\]\@gmail\.com

* ^From.*address\d\@gmail\.com

* ^From.*address\d@gmail.com

but none of the above-mentioned works... I am totally lost. 但没有上述作品......我完全失去了。 It looks like regular expressions have its own logic and patterns in procmail. 看起来正则表达式在procmail中有自己的逻辑和模式。

Here is my full recipe. 这是我的完整食谱。 The address to match has this 9 at the end and it's gmail.com When I put any other email address into this * ^From.* field it works OK, but this one doesn't... Here is my full recipe. 匹配的地址最后有9个,它是gmail.com当我把任何其他电子邮件地址放入这个* ^ From。*字段时它工作正常,但这个没有...这是我的完整食谱。 The conditions to match are: to add a tag [New Report] when it comes from address9@gmail.com and the subject field is empty. 要匹配的条件是:当来自address9@gmail.com并且主题字段为空时添加标签[新报告]。 I would really be thankful if somebody could advise please, as I am about to go nuts trying to understand where is the mistake here. 如果有人可以提出建议,我真的很感激,因为我要努力去了解这里的错误。

:0 fhw
* ^From.*address9\@gmail\.com 
* ^Subject:\/.+
| /usr/bin/formail -i "Subject: [New Report]$MATCH"

Would also be grateful for any pointers how to trouble shoot it. 也会感激任何指针如何麻烦拍它。 Many thanks in advance! 提前谢谢了!

The header looks like this: 标题看起来像这样:

From: Name Lastname <address9@gmail.com>
Date: Wed, 12 Jun 2019 20:37:17 +1200
Message-ID<CADxD3vdy5cW55mogOK5+543ngU7iFKjJcpDV3Q4YL772F=LdQ@mail.gmail.com>
Subject: 

Regular expressions are pretty simple actually. 正则表达式实际上非常简单。 Nearly every character simply matches itself. 几乎每个角色都只是匹配自己。 So if you have (beginning of line) F r o m : followed by anything followed by a d d r e s s 9 ... it should match. 所以,如果你有(开始行) F r o m :其次是什么,然后a d d r e s s 9 ...它应该相匹配。

Procmail doesn't support various Perl extensions like \\d or \\t , and of course there is no need to backslash characters which don't have a special meaning in regex like @ . Procmail不支持各种Perl扩展,例如\\d\\t ,当然也不需要反斜杠字符,这些字符在@正则表达式中没有特殊含义。 If you want to match a single digit that's [0-9] without any backslashes (those would change [ and ] back into literal matches). 如果你想匹配一个没有任何反斜杠的[0-9]数字(那些会改变[]回到文字匹配)。

^Subject:\\/.+ checks that there is at least one character in the Subject: header and collects it all into $MATCH . ^Subject:\\/.+检查Subject:标题中至少有一个字符,并将其全部收集到$MATCH This does not check that the subject is empty. 不会检查主题是否为空。 Perhaps you want something like 也许你想要的东西

# \t is not supported, replace with a literal tab character
* ^Subject:[ \t]+$

where the \\t should be replaced with a literal tab. 其中\\t应该用文字标签替换。 Every message should contain a Subject: header so it will always be there, but if its value consists entirely of whitespace, that is considered an empty subject. 每条消息都应该包含一个Subject:标题,因此它始终存在,但如果它的值完全由空格组成,那么它将被视为空主题。 And of course, if the header is empty, there is no need to capture its contents. 当然,如果标题为空,则无需捕获其内容。

Having said that, your expression would match if there is at least a single space after the colon in Subject: so again, it really should match in your test case unless there really is absolutely nothing in the Subject: header. 话虽如此,如果在Subject:冒号后面至少有一个空格,你的表达式将匹配Subject:所以再一次,它确实应该在你的测试用例中匹配,除非在Subject:标题中确实没有任何内容。

So anyway, if that was your problem, the From: recipe might actually have been matching, but the Subject condition was not matching. 所以无论如何,如果这是你的问题, From: recipe可能实际上已匹配,但Subject条件不匹配。 With Procmail VERBOSE logging you would see each regex with "matched" or "no match" in the log file. 使用Procmail VERBOSE日志记录,您将在日志文件中看到每个正则表达式中“匹配”或“不匹配”。

:0 fhw
* ^From.*address9@gmail\.com 
* ^Subject:[    ]*$
| /usr/bin/formail -i "Subject: [New Report]"

Or perhaps you want to do this with any Subject: header? 或者你想用任何Subject:标题来做这个? (This time I'm using a proper literal tab - check that you copy/paste this properly, though! There should be a space and a tab between [ and ] .) (这次我使用了一个正确的文字标签 - 检查你是否正确地复制/粘贴了!在[]之间应该有一个空格和一个标签。)

:0 fhw
* ^From.*address9@gmail\.com 
* ^Subject:[    ]*\/.*$
| /usr/bin/formail -i "Subject: [New Report] $MATCH"

For troubleshooting, perhaps have a look at http://www.iki.fi/era/mail/procmail-debug.html 有关故障排除,请查看http://www.iki.fi/era/mail/procmail-debug.html

The Stack Overflow Minimal Reproducible Example guidance is also useful. Stack Overflow Minimal Reproducible Example指南也很有用。 Briefly, try to reduce the problem to the simplest input message and the simplest recipe which doesn't behave like you expect, then once you can't reduce it any further, take a good sharp look at what you are left with. 简而言之,尝试将问题简化为最简单的输入消息和最简单的配方,这种配方不会像您期望的那样,然后一旦您无法进一步减少它,请仔细看看您剩下的内容。 Common causes of confusion include 混淆的常见原因包括

  • malformed input messages 格式错误的输入消息
    • Empty lines in what's supposed to be the headers? 什么应该是标题的空行?
    • Headers wrapped over multiple physical lines? 包裹在多条物理线上的标题?
    • Pesky control characters where there aren't supposed to be, either in the message or in your Procmail script (don't use Windows editors ^W ) 在消息或Procmail脚本中不应该出现的烦人控制字符(不要使用Windows编辑器^W
  • unwarranted assumptions 无根据的假设
    • Regex doesn't work like you thought? 正则表达式不像你想的那样工作?
    • Procmail's regex dialect is different than eg Perl's? Procmail的正则表达方言与Perl的不同? Online regex testers typically assume a Perl (or occasionally Javascript) regex feature set 在线正则表达式测试人员通常假设Perl(或偶尔使用Javascript)正则表达式功能集
    • Procmail matches against only the headers by default; 默认情况下,Procmail仅匹配标题; some beginners miss this and are surprised when it doesn't find a string which is plainly there, only in the body 一些初学者错过了这一点,并且当它没有找到明显存在的字符串时,只会在身体中感到惊讶
    • MIME content-transfer-encoding obscures the content you thought you knew was there? MIME内容传输编码模糊了您认为您知道的内容?
    • Your regex really has to match a literal piece of text in the message; 你的正则表达式真的必须匹配消息中的文字文本; Procmail does no normalization eg to extract just the sender address in a convenient form for matching. Procmail不进行规范化,例如以便捷的形式提取发件人地址以进行匹配。

... Oh, and everyone's favorite: Make sure you set SHELL=/bin/sh right at the top of your recipe file. ...哦,每个人都喜欢:确保在配方文件的顶部设置SHELL=/bin/sh This has been the source of many otherwise completely mysterious failures over the years. 多年来,这一直是许多其他完全神秘失败的根源。

Here is a quick demo to demonstrate that your test case works for me: 这是一个快速演示,演示您的测试用例适合我:

tripleee@debian$ cat >test.msg
From: Name Lastname <address9@gmail.com>
Date: Wed, 12 Jun 2019 20:37:17 +1200
Message-ID<CADxD3vdy5cW55mogOK5+543ngU7iFKjJcpDV3Q4YL772F=LdQ@mail.gmail.com>
Subject: 

No fooling
^D

tripleee@debian$ cat >test.rc
SHELL=/bin/sh
DEFAULT=/dev/null
VERBOSE=yes

:0 fhw
* ^From.*address9\@gmail\.com 
* ^Subject:\/.+
| /usr/bin/formail -i "Subject: [New Report]$MATCH"
^D

tripleee@debian$ procmail -m test.rc <test.msg 
procmail: [3717] Wed Jun 12 13:38:55 2019
procmail: Match on "^From.*address9\@gmail\.com"
procmail: Assigning "MATCH="
procmail: Matched " "
procmail: Match on "^Subject:\/.+"
procmail: Executing " /usr/bin/formail -i "Subject: [New Report]$MATCH""
procmail: Assigning "LASTFOLDER=/dev/null"
procmail: Opening "/dev/null"
From address9@gmail.com  Wed Jun 12 13:38:55 2019
 Subject: [New Report] 
  Folder: /dev/null                             253

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM