#!/usr/local/bin/perl -w use strict; use Email::Folder; use Email::Send qw[MySendmail]; # preload mailer(s) =head1 NAME mbox2gmail - transfer mails from local mboxes to Gmail =head1 USAGE mbox2gmail [mailbox[s]] =head1 AUTHOR Simon Wistow =head1 COPYRIGHT Copyright, 2006 - Simon Wistow Destributed under the same terms as Perl itself. =cut my $gmail = shift || die "You must pass a gmail address\n"; $gmail =~ /\@g(oogle?)mail\.com/i || die "$gmail is not a gmail address\n"; @ARGV || die "You must pass at least one mailbox\n"; foreach my $mbox (@ARGV) { # open the folder my $folder = Email::Folder->new($mbox); unless ($folder) { print STDERR "Couldn't open $folder\n"; next; } # attempt deliver each mail my $success = 0; my $fail = 0; foreach my $mail ($folder->messages) { my $rv = send MySendmail => $mail, $gmail; if ($rv) { $success++; } else { $fail++; } } # print some stats if ($success == 0 && $fail == 0) { print STDERR "$folder had no mails!\n"; } else if ($success == 0) { print STDERR "We couldn't transfer any mails to Gmail - aborting :(\n"; last; } else if ($fail == 0) { print STDERR "Transferred all $success mail(s) successfully\n"; } else { print STDERR "Transferred $success mail(s) successfully ". "but failed to transfer $fail mails :(\n"; } } # custom Sendmail package to fake To: address package Email::Send::MySendmail; use strict; use vars qw[$SENDMAIL]; $SENDMAIL ||= q[sendmail]; sub send { my ($message, $address, @args) = @_; open SENDMAIL, "| $SENDMAIL -oi @args $address" or return undef; print SENDMAIL $message->as_string; close SENDMAIL; } 1;