全部博文(1144)
分类: PERL
2014-11-28 20:04:02
#!/usr/bin/perl use strict; use Email::Folder; use Mail::Mailer; use MIME::Parser; use Net::SMTP; my $mbox = $ARGV[0]; my $email = $ARGV[1]; my $server = $ARGV[2]; $server = 'smtp' if(!defined($server)); die "Usage: $0 mbox dest_address [smtp server]" if(!defined($mbox) || !-f $mbox); die "Usage: $0 mbox dest_address [smtp server]" if(!defined($email) || $email !~ m/\@/); my $folder = Email::Folder->new($mbox || die "Usage: $0 mbox dest_address [smtp server] Forward all mail found in mail file mbox to address. "); my $count=0; my @messages=$folder->messages; my $total=@messages; foreach (@messages){ $count++; my $parser = new MIME::Parser; $parser->output_under("/tmp"); $parser->decode_headers(0); $parser->ignore_errors(1); my $entity = $parser->parse_data($_->as_string); my $header = $entity->head; my $sender = $entity->head->get('From'); next if $header->get("subject") =~ m/FOLDER INTERNAL/; $header->replace('To', $email); $header->delete('Received'); $header->delete('MIME-Version'); $header->delete('Return-Path'); $header->delete('User-Agent'); $header->delete('Message-ID'); $header->delete('X-Mailer'); $header->delete('X-Security'); $header->delete('X-Spam-Checker-Version'); $entity->head($header); $entity->sync_headers; print "Message $count / $total\n"; print "Sending message with subject: " . $entity->head->get("subject"); print " to $email via $server\n"; my $smtp = new Net::SMTP($server) or die "No mailserver"; $smtp->mail($sender) or die "unable to set sender"; $smtp->to($email) or die "unable to address message"; $smtp->data() or die "unable to start data send"; $smtp->datasend($entity->as_string()) or die "Message send failed"; $smtp->dataend() or die "Message end failed"; $smtp->quit(); print "Done\n\n"; }