Exim redeliver / reinject mails

 perl redeliver.pl MAILBOX
Where MAILBOX is the mailbox file
my $file = shift || die "need file\n";
my $gto  = shift; # global to.  if present, override other per-email decision
my $msg  = '';
my $to   = '';
my $from = '';

open(I, "<$file") || die "Can't open $file\n";
while (<I>) {
  if (/^From /) {
    if ($msg) {
      if ($to && $from) {
        do_mail($from, $to, $msg);
      } else {
        print STDERR "have a message but no recips\n";
      }
    } else {
      print STDERR "saw From w/ no message\n";
    }
    $msg  = '';
    $from = '';
    $to   = '';
  } elsif (/^Return-path:\s*<(.*)>$/) {
    $from = $1;
  } elsif (/^Envelope-to:\s*(\S+)\s*$/) {
    $to = $1;
  } elsif (/^Delivery-date:\s*/) {
    ; # just ignore
  } else {
    $msg .= $_;
  }
}
close(I);

if ($msg && $to && $from) {
  do_mail($from, $to, $msg);
}

sub do_mail {
  my $f = shift;
  my $t = shift;
  my $m = shift;
  $t = $gto if ($gto);

  print "$f -> $t\n";
  #print "MAIL FROM:<$f>\nRCPT TO:<$t>\nDATA\n$m\n.\n";
  open(P, "|/usr/lib/sendmail -f $f $t") || warn "can't open sendmail: $!\n";
  print P $m, "\n.\n";
  close(P);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>