Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 730
Geekman Avatar asked Apr 30 '10 10:04

Geekman


1 Answers

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

like image 168
Ether Avatar answered Oct 26 '22 22:10

Ether