views:

266

answers:

1

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.

A: 

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

Ether
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.
Geekman