如何在procmail转发的电子邮件中添加一些文本? [英] How to put some text into procmail forwarded e-mail?

查看:113
本文介绍了如何在procmail转发的电子邮件中添加一些文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我一直在尝试编写procmail脚本.

For a couple of days, I've been trying to write procmail script.

我想转发邮件,并在邮件内容中插入一些文本.

I want to forward messages, and inject some text into message contents.

我想完成的事情:

  • 有人给我发送电子邮件,主题中带有天气"一词
  • 电子邮件被转发到地址"mymail@somedomain.com"
  • 每封转发的电子邮件都会在内容中添加一些文本

但是到目前为止,还没有成功.

But so far, no success.

.procmail.log中,有一条消息"procmail:缺少操作"

In .procmail.log, there's a message "procmail: Missing action"

SHELL=/bin/bash
VERBOSE=off
LOGFILE=/home/test/.procmail.log
LOGDATE_=`/bin/date +%Y-%m-%d`

:0
* ^Subject:.*weather

:0 bfw
| echo "This is injected text" ; echo "" ; cat

:0 c
! mymail@somedomain.com

当我查看电子邮件源时,我看到文本已被注入. 但是这个地方错了...

When I looked into email source, I saw that text is injected. But the place is wrong ...

看看:

MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="------------148F3F0AD3D65DD3F3498ACA"
Content-Language: pl
Status:   
X-EsetId: 37303A29AA1D9F60667466

This is injected text

This is a multi-part message in MIME format.
--------------148F3F0AD3D65DD3F3498ACA
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

CONTENT CONTENT CONTENT
*********************************************************

应将插入的文本放置在内容所在的位置.现在在上面...

Injected text should be placed, where content is. Now it is above ...

推荐答案

您没有解释您的代码,但看起来您正在尝试在单个条件下使用多个操作. 为此使用括号.

You don't explain your code, but it looks like that you are attempting to use multiple actions under a single condition. Use braces for that.

:0
* ^Subject:.*weather
{
  :0 bfw
  | echo "This is injected text" ; echo "" ; cat

  :0 c
  ! mymail@somedomain.com
}

仅作总结,每个配方都必须具有标题行(:0和可能的标志)和一个动作.条件是可选的,并且可以有多个.进一步的配方块是一种操作形式,它可以满足这些要求(其他操作类型是保存到文件夹,传递到命令或转发到电子邮件地址).

Just to summarize, every recipe must have a header line (the :0 and possible flags) and an action. The conditions are optional, and there can be more than one. A block of further recipes is one form of action so that satisfies these requirements (the other action types are saving to a folder, piping to a command, or forwarding to an email address).

要在多部分消息的第一个MIME正文部分的顶部插入文本,您需要进行一些MIME解析.不幸的是,Procmail没有对MIME的明确支持,但是如果您知道传入的消息将始终具有特定的结构,则可能会遇到一些相当简单的事情.

To inject text at the top of the first MIME body part of a multipart message, you need to do some MIME parsing. Procmail unfortunately has no explicit support for MIME, but if you know that the incoming message will always have a particular structure, you might get away with something fairly simple.

:0
* ^Subject:.*weather
{
  :0fbw
  * ^Mime-version: 1\.0
  * ^Content-type: multipart/
  | awk '/^Content-type: text\/plain;/&&!s {n=s=1} \
      n&&/^$/{n=0; p=1} \
      1; \
      p{ print "This is injected text.\n"; p=0 }'

  :0 c
  ! mymail@somedomain.com
}

正文(包含所有MIME正文部分以及其标头和所有内容)被传递到一个简单的Awk脚本,该脚本在第一个text/plain MIME正文部分(我们乐观地认为是)之后找到第一个空行标头,然后在其中插入文本. (Awk区分大小写,因此可能需要对正则表达式文本进行改编或概括,并且我假设输入消息中的空格是完全规则的.对于生产系统,这些简化的假设是不现实的.)

The body (which contains all the MIME body parts, with their headers and everything) is passed to a simple Awk script, which finds the first empty line after (what we optimistically assume is) the first text/plain MIME body part header, and injects the text there. (Awk is case-sensitive, so the regex text might need to be adapted or generalized, and I have assumed the whitespace in the input message is completely regular. For a production system, these simplifying assumptions are unrealistic.)

如果您需要完全的MIME支持(例如,输入消息可能是也可能不是,或者不是多部分的,或者包含嵌套的多部分),我的建议是使用某种现代的脚本语言用适当的MIME支持库编写注入代码;我仍然会选择Python,尽管它仍然(即使在3.6中的email库更新后)仍然有些笨拙和笨拙.

If you need full MIME support (for example, the input message may or may not be multipart, or contain nested multiparts), my recommendation would be to write the injection code in some modern script language with proper MIME support libraries; Python would be my pick, though it is still (even after the email library update in 3.6) is slightly cumbersome and clumsy.

这篇关于如何在procmail转发的电子邮件中添加一些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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