#!/usr/bin/perl -w

#
# read mbox with error messages from mail server (like 
# Returned mail: Host unknown (Name server...
# and dump e-mails which are returned so that they can be feeded
# back to sendmail using
# $ sendmail -t < mail.0
#

my $b;

my $nr=1;

while(<>) {
	if (m,^Content-Type: message/rfc822,) {
		$foo=<>;
		while ($foo !~ m,^Return-Path,) { $foo=<>; };
		if ($foo !~ m,^Return-Path: <MAILER-DAEMON>,) {
			open(M,"> mail.$nr") || die $!;
			print "$nr: $foo\n\t$b\n";
			print M $foo;
			chomp $b;
			while($foo=<>) {
				last if ($foo=~m,^$b,);
				print M $foo;
			}
			close(M);
			$nr++;
		}
	}
	$b=$_;
}

