procmail处理后的电子邮件正文格式 [英] E-Mail's body format after procmail processing

查看:181
本文介绍了procmail处理后的电子邮件正文格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下场景:

GNU/Linux计算机上的SMTP服务器正在接受邮件.接受的邮件将发送到procmail进行进一步处理.这是我的 .procmailrc :

SMTP server on the GNU/Linux machine is accepting mails. Accepted mail is being sent to procmail for further processing. Here's my .procmailrc:

VERBOSE=yes
LOGFILE=$HOME/procmail.log
SUBJECT=`formail -xSubject: | tr -d '\n' | sed -e 's/^ //' | /usr/bin/perl -MEncode -ne 'print encode ("utf8",decode ("MIME-Header",$_ )) '`
FROM=`formail -rt -xTo:`
DATE=`formail -xDate:`
BODY=`formail -I ""`
:0fbW
* ^From.*some_special_name@special_server.com
| echo "FROM:$FROM" > $HOME/res.txt; \
  echo "DATE:$DATE" >> $HOME/res.txt; \
  echo "SUB:$SUBJECT" >> $HOME/res.txt; \
  echo "BODY:" >> $HOME/res.txt; \
  echo $BODY >> $HOME/res.txt; process.py

这个小脚本首先创建一个本地文件 $ HOME/res.txt ,然后启动另一个名为 process.py 的脚本.现在,在 $ HOME/res.txt 中填充以下条目:

This little script first creates a local file $HOME/res.txt and then launches another script called process.py. Now, the $HOME/res.txt is populated with the following entries:

FROM:some_special_name@special_server.com
DATE:Mon, 06 Oct 2014 13:14:32 +0200
SUB:Some subject
BODY:
This is a multi-part message in MIME format. --------------030006020609010705060803 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hello, Some kind of long tekst where I cannot see the line feed chars nor any other control chars...

主体包含原始字符串,但没有主体的原始格式.我的意思是将\ n或\ t字符过滤掉. process.py 脚本要求消息的正文部分保持电子邮件的原始格式.

The body contains the raw string without the original format of the body. What I mean by this is that \n or \t characters are filtered out. The process.py script demands the body part of the message to maintain the original format of the email message.

我该如何实现?

推荐答案

像在shell脚本中一样,除非您特别要求shell对值执行令牌拆分和通配符扩展,否则需要正确地双引号引用变量.参见例如此答案以获取详细说明.

As always in shell scripts, you need to properly duoble-quote variables unless you specifically require the shell to perform token splitting and wildcard expansion on the value. See e.g. this answer for a detailed explanation.

  echo "$BODY" >>$HOME/res.txt; \

此外,我不明白您为什么要混合多个这样的动作.在这两行之间,我想你的意思是process.py读取的是res.txt而不是其标准输入.如果它正在读取标准输入,它将收到正确的,无干扰的消息.

Additionally, I don't understand why you mix multiple actions like that. Between the lines, I guess you mean that process.py reads res.txt instead of its standard input; if it were reading standard input, it would receive the correct, unmangled message.

在没有上下文的情况下,我会弯腰,推测f标志也是错误的.除非process.py在标准输出上打印一条新消息,该消息应替代Procmail食谱文件其余部分的传入消息,否则您只需将其取出即可.

Without context, I'll go out on a limb and speculate that the f flag is also wrong. Unless process.py prints a new message on standard output which should replace the incoming message for the remainder of your Procmail recipe file, you should simply take it out.

这里还有一个竞争条件:如果多个消息大致同时到达(如果系统负载沉重,则相同时间"可能是一个相当宽的窗口),它们将覆盖res.txt并践踏彼此的结果无法预测.惯用的解决方案是使用本地锁定文件.但极为优越的解决方案是消除对临时文件的需要,并更改process.py,使其改为读取标准输入.

There is also a race condition here: if multiple messages arrive at roughly the same time (where "same time" can be a rather wide window if your system is under heavy load), they will overwrite res.txt and trample over each others' results in unpredicable ways. The customary solution to that is to use a local lock file; but the vastly superior solution is to remove the need for a temporary file, and change process.py so that it reads standard input instead.

因为无论如何您都在使用Python,所以我会在Python中执行所有标头解析,除了formail -rtzxTo:可能是例外,这对于重新实现而言相当复杂.这将大大简化您的Procmail配方,并可能改善process.py(或创建的包装,如果无法修改它的话).

Since you are using Python anyway, I would do all the header parsing in Python, with the possible exception of formail -rtzxTo:, which is rather complex to reimplement; this would simplify your Procmail recipe significantly, and probably improve process.py (or the wrapper you create, if you cannot modify it).

这篇关于procmail处理后的电子邮件正文格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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