2

I am trying to redirect emails that match a particular pattern to a shell script which will create files containing the texts, with datestamped filenames.

First, here is the routine from .procmailrc that hands the emails off to the script:

:0c:
* Subject: ^Ingest_q.*
| /home/myname/procmail/process

and here is the script 'process':

#!/bin/bash

DATE=`date +%F_%N`
FILE=/home/myname/procmail/${DATE}_email.txt

while read line
        do
            echo "$line" 1>>"$FILE";
        done

I have gotten very frustrated with this because I can pipe text to this script on the command line and it works fine:

mybox-248: echo 'foo' | process
mybox-249: ls
2013-07-31_856743000_email.txt  process

The file contains the word 'foo.'

I have been trying to get an email text to get output as a date-stamped file for hours now, and nothing has worked.

(I've also turned logging on in my .procmailrc and that isn't working either -- I'm not trying to ask a second question by mentioning that, just wondering if that might provide some hint as to what I might be doing wrong ...).

Thanks,

GB

4

1 回答 1

2

引用您的尝试:

:0c:
* Subject: ^Ingest_q.*
| /home/myname/procmail/process

正则表达式是错误的,^只在行首匹配,所以它不能出现在Subject:. 试试这个。

:0c:process.lock
* ^Subject: Ingest_q
| /home/myname/procmail/process

我还指定了一个命名的锁文件;我不相信 Procmail 可以仅从脚本名称推断锁定文件名。由于您可能同时发送多条电子邮件,并且您不希望它们的日志记录混合在日志文件中,因此此处需要使用锁定文件。

最后,正则表达式中的尾随.*是完全多余的,所以我删除了它。

(olde Procmail mini-FAQ也解决了这两个问题。)

我意识到你的食谱可能只是在你开始做更大的事情之前的一个快速测试,但是调用process脚本的整个食谱可以完全替换为类似的东西

MAILDIR=/home/myname/procmail
DATE=`date +%F_%N`
:0c:
${DATE}_email.txt

这将生成 Berkeley mbox 格式,即每条消息都应该From_在真实标题之前有一个伪标题;如果您不确定这是否已经是这种情况,您可能应该使用procmail -Yf-来确保这样做(否则实际上无法判断一条消息在哪里结束,另一条消息在哪里开始;这既适用于您的原始解决方案,也适用于替代品)。

因为 Procmail 看到了您要发送到的文件名,所以它现在可以推断出一个锁定文件名,作为一个小奖励。

使用MAILDIR指定目录是执行此操作的常规方法,但如果您愿意,当然可以指定 mbox 文件的完整路径。

于 2013-08-11T18:45:43.720 回答