Kenneth Qvistgaard Dalbjerg, IT Konsulent i århus
Citrix Certified

Delete mails from a sender or to recipient address

May 7th, 2012 Posted in Postfix

Here is a simple script “pfdel” that can be use to delete a mail from a sender or recipient address.
Save the perl script as pfdel in /usr/local/bin and chmod +x /usr/local/bin/pfdel

Then you can use the perl script as: pfdel 123test@test.com then it will delete all messages where ether 123test@test.com, is in there sender or recipient address.

#!/usr/bin/perl -w
#
# pfdel – deletes message containing specified address from
# Postfix queue. Matches either sender or recipient address.
#
# Usage: pfdel
#

use strict;

# Change these paths if necessary.
my $LISTQ = “/usr/sbin/postqueue -p”;
my $POSTSUPER = “/usr/sbin/postsuper”;

my $email_addr = “”;
my $qid = “”;
my $euid = $>;

if ( @ARGV != 1 ) {
die “Usage: pfdel \n”;
} else {
$email_addr = $ARGV[0];
}

if ( $euid != 0 ) {
die “You must be root to delete queue files.\n”;
}

open(QUEUE, “$LISTQ |”) ||
die “Can’t get pipe to $LISTQ: $!\n”;

my $entry = ; # skip single header line
$/ = “”; # Rest of queue entries print on
# multiple lines.
while ( $entry = ) {
if ( $entry =~ / $email_addr$/m ) {
($qid) = split(/\s+/, $entry, 2);
$qid =~ s/[\*\!]//;
next unless ($qid);

#
# Execute postsuper -d with the queue id.
# postsuper provides feedback when it deletes
# messages. Let its output go through.
#
if ( system($POSTSUPER, “-d”, $qid) != 0 ) {
# If postsuper has a problem, bail.
die “Error executing $POSTSUPER: error ” .
“code ” . ($?/256) . “\n”;
}
}
}
close(QUEUE);

if (! $qid ) {
die “No messages with the address ” .
“found in queue.\n”;
}

exit 0;

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • PDF
  • email
  • RSS
If you like this content, please support me and press the google adsense in the top of this post, thank you



Post a Comment