0

I would like to store my incoming e-mails as *.msg files and i try this with the following external delivery method; This is working well but i receive the mail as one large plain text string.

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

My parse_postfix.sh contains the following code;

#!/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};

The output of this method is one large string and i can't read it with a MIME:Parser; (example 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>;

The example mail listed below is the way how i can read it properly with the MIME:Parser; (example 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;

What is the best way to convert the 1st example to the 2nd example? Or to save the e-mail properly like the 2nd example?

tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

0

You are not quoting the variable properly.

echo "$mail"

The sane solution is to not use a variable at all, though.

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

This will still be problematic if there is more than one message arriving during the same second.

I cannot see why you are not using the built-in delivery mechanisms of Postfix itself, anyway. If you want more control over delivery, maybe hook in Procmail, which has a built-in delivery mode for running numbering of messages. Proper Maildir would still be better for most real-world scenarios, as it is more robust.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318