http://qs1969.pair.com?node_id=551292


in reply to hashing out lines from mailq...

This is a dumb brute-force parser for the output of mailq. If you just want to get the messages from MAILER-DAEMON you can check $entry->{from} when you are iterating over @entries:

my $header = 1; my @entries; my $entry = {}; while(<>) { if ($header) { $header = 0 if /^--/; next; } if (/^(\S+)\s+(\d+)\s+(.+?)\s+<?(\S+)>?/) { push @entries, $entry if exists $entry->{msg_id}; $entry = {}; $entry->{msg_id} = $1; $entry->{size} = $2; $msg->{date} = $3; $msg->{from} = $4; } elsif (/\s+\((.*)\)/) { $entry->{message} = $1; } elsif ( /\s+<(.*)>?/ ) { $entry->{recipients} = [] unless exists $entry->{recipients}; push @{$entry->{recipients}}, $1; } } push @entries, $entry if exists $entry->{msg_id}; foreach $entry ( @entries ) { print $entry->{msg_id}, join " ", @{$entry->{recipients}},"\n"; }

/J\

Replies are listed 'Best First'.
Re^2: hashing out lines from mailq...
by perlknight (Pilgrim) on May 24, 2006 at 14:54 UTC
    Thanks. How would I go about geting the following for all deferred messages: qid, from, all recipients? Is there a better way of doing this besides parsing the output of mailq with regex? Thanks.

      That code does it, you just have to look for 'Defered' in $entry->{message}. The only other alternative (for sendmail at least) is to parse all of the 'q' files in /var/spool/mqueue, which will almost certainly by more of a pain.

      /J\