Is there a better tool than postcat for viewing postfix mail queue files?
Asked Answered
T

1

8

So I got a call early this morning about a client needing to see what email they have waiting to be delivered sitting in our secondary mail server. Their link for the main server had (still is) been down for two days and they needed to see their email.

So I wrote up a quick Perl script to use mailq in combination with postcat to dump each email for their address into separate files, tar'd it up and sent it off. Horrible code, I know, but it was urgent.

My solution works OK in that it at least gives a raw view, but I thought tonight it would be nice if I had a solution where I could provide their email attachments and maybe remove some "garbage" header text as well. Most of the important emails seem to have a PDF or similar attached.

I've been looking around but the only method of viewing queue files I can see is the postcat command, and I really don't want to write my own parser - so I was wondering if any of you have already done so, or know of a better command to use?

Here's the code for my current solution:

#!/usr/bin/perl

$qCmd="mailq | grep -B 2 \"someemailaddress@isp\" | cut -d \" \" -f 1";

@data = split(/\n/, `$qCmd`);
$i = 0;

foreach $line (@data)
{
    $i++;

    $remainder = $i % 2;
    if ($remainder == 0)
    {
            next;
    }

    if ($line =~ /\(/ || $line =~ /\n/ || $line eq "")
    {
        next;
    }
    print "Processing: " . $line . "\n";
    `postcat -q $line > $line.email.txt`;
    $subject=`cat $line.email.txt | grep "Subject:"`;
    #print "SUB" . $subject;
    #`cat $line.email.txt > \"$subject.$line.email.txt\"`;
}

Any advice appreciated.

Ternan answered 30/4, 2010 at 10:5 Comment(2)
Do you care if the solution is Perl? I figure you just want to get the job in any way that you can. :)Include
I don't mind, I actually wrote it in bash initially (hence heavy use of backticks) but found issues with using the modulus operator that I didn't want to deal with (I was in a rush). If I don't already know the language, I'll learn it at least so I can hack something. My only objection may be if it were in assembler perhaps? :PTernan
C
2

You may find the Postfix::Parse::Mailq module of use here, as well as the pfcat script.

Convery answered 30/4, 2010 at 18:5 Comment(1)
I appreciate the suggestions Ether, and I can see how it would greatly tidy up my current script - however they both just seem to be other ways of utilising postcat. Postcat is great for a raw view, but I was hoping I could get something to give a nicer display for customers without me having to parse it out. As a bonus it would be awesome to have something to take the encoded attachments and dump them as the actual files. I haven't found anything like this, I know I could code it, but I'm trying not to as it'd be a fair bit of code (read: time) I imagine.Ternan

© 2022 - 2024 — McMap. All rights reserved.