#!/local/bin/perl use strict; use lib qw(/local/lib/perl); use feature qw(:5.10); use IO::All; use IO::Socket::SSL; use Mail::IMAPTalk; use Mail::Internet; use Date::Format; my $dir = '/home/km/misc/km-mail'; # Maildir where messages are stored my @files = io("$dir/cur")->all; # messages are expected to be in ./cur my $gmuser = ''; # Gmail username my $gmpass = ''; # Gmail password my $my_addr = qr/(km|kirill) @ krot.org/ix; # Regex for matching sent messages # I don't care about the dot! my $gmall = '[Gmail]/All e-post'; # Name of All mail my $gmsent = '[Gmail]/Sendt e-post';# Name for Sent mail my $gmsock = IO::Socket::SSL->new('imap.gmail.com:993') or die "Could not connect to Gmail IMAP server\n"; $gmsock->autoflush(1); my $gm = Mail::IMAPTalk->new( Socket => $gmsock, Username => $gmuser, Password => $gmpass, Uid => 1, UseBlocking => 1, RootFolder => '', Separator => '/', ) or die "Could not login to Gmail IMAP server\n"; my $count = 0; for my $io (sort { $a->mtime <=> $b->mtime } @files) { $count++; # next if $count < 1234; # this can be used to resume uploading open IN, "< $io" or next; # *highly* unlikely, but anyway my $mail = Mail::Internet->new(\*IN, Modify => 0); close IN; $mail->delete($_) for qw(content-length lines); # keep Mutt happy $mail->delete($_) for qw(received-spf authentication-results dkim-signature domainkey-signature); # save space my $from_me = $mail->get('from') =~ $my_addr ? 1 : 0; my $folder = $from_me ? $gmsent : $gmall; my $idate = time2str('%e-%b-%Y %T %z', $io->mtime); my $flags = '(\Seen)'; say "$count: Appending message @{[$io->filename]} to '$folder'"; $gm->append($folder, $flags, $idate, $mail->as_string()) or die "$count: Could not append message @{[$io->filename]} to '$folder': $@"; } $gm->logout();