无法正确存储Postfix输出邮件格式

时间:2014-08-27 12:19:37

标签: bash email postfix-mta

我想将传入的电子邮件存储为* .msg文件,我尝试使用以下外部传送方法;这工作正常,但我收到的邮件是一个大的纯文本字符串。

foofoofoo unix -      n       n       -       -       pipe
  flags=F user=www-data argv=/mle/scripts/parse_postfix.sh ${sender} ${recipient}

我的parse_postfix.sh包含以下代码;

#!/bin/bash

mail=$(cat);

ddate=`date +%Y%m%d_%H%M%S`
msg_file=${ddate}.msg
path=/mle/spool/${code}
echo $mail > ${path}/${msg_file};

此方法的输出是一个大字符串,我无法用MIME:Parser读取它; (例1)

From foo@gmail.com Sat Aug 23 19:22:27 2014 Received: from mail-yh0-f49.google.com (mail-yh0-f49.google.com [209.85.213.49]) by foo.testtesttest.com (Postfix) with ESMTPS id D83DC43C8 for <foo@testtesttest.com>; Sat, 23 Aug 2014 19:22:26 +0000 (UTC) Received: by mail-yh0-f49.google.com with SMTP id b6so9924529yha.36 for <foo@testtesttest.com>;

下面列出的示例邮件是我使用MIME正确读取它的方式:Parser; (例2)

Delivered-To: foo@testtesttest.com
Received: by 10.140.108.135 with SMTP id j7csp222526qgf;
        Thu, 3 Jul 2014 06:26:51 -0700 (PDT)
X-Received: by 10.194.82.106 with SMTP id h10mr3033237wjy.115.1404394010626;
        Thu, 03 Jul 2014 06:26:50 -0700 (PDT)
Return-Path: <foo@gmail.com>
Received: from mail-we0-x22c.google.com (mail-we0-x22c.google.com [2a00:1450:400c:c03::22c])
        by mx.google.com with ESMTPS id e6si24028130wix.75.2014.07.03.06.26.50
        for <foo@testtesttest.com>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Thu, 03 Jul 2014 06:26:50 -0700 (PDT)
Received-SPF: pass (google.com: domain of foo@gmail.com designates 2a00:1450:400c:c03::22c as permitted sender) client-ip=2a00:1450:400c:c03::22c;

将第一个示例转换为第二个示例的最佳方法是什么?或者像第二个例子那样正确保存电子邮件?

1 个答案:

答案 0 :(得分:0)

你的变量不是quoting

echo "$mail"

理智的解决方案是完全不使用变量。

ddate=$(date +%Y%m%d_%H%M%S)
msg_file=${ddate}.msg
path=/mle/spool/${code}
cat> ${path}/${msg_file}

如果在同一秒内有多条消息到达,这仍然​​会有问题。

无论如何,我不明白为什么你没有使用Postfix本身的内置传送机制。如果你想要更多地控制交付,可以挂钩Procmail,它有一个内置的交付模式来运行消息编号。对于大多数真实场景,适当的Maildir仍然会更好,因为它更强大。