Simon Wistow (deflatermouse) wrote,
Simon Wistow

Transferring mail to GMail

Someone on channel was muttering about transferring all their mail from where it currently was to Gmail and that they didn't want to use Mark Lyon's GML which is, shock horror, in Python.

Usually I'm against not using something just because it's not in your language of choice unless you've got a damn good reason. However when I realised that GML just used SMTP isntead of some proprietary GMail transport protocol I whipped this up.

    #!perl -w
    use strict;
    use Email::Folder;
    use Email::Send qw[MySendmail]; # preload mailer(s)
    my $gmail =   shift           || die "You must pass a gmail address\n";
    $gmail    =~  /\@gmail\.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";
        # attempt deliver each mail
        my $success = 0;
        my $fail    = 0;
        foreach my $mail ($folder->messages) {
            my $rv = send MySendmail => $mail, $gmail;
            if ($rv) {
            } else {
        # 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";
        } 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;

Which is hella ugly but for a 5 minute proof of concept it's not bad. As someone pointed out it be smarter about delivering mails - perhaps keeping a failure or success log when things fail to avoid duped or perhaps even checking at start up time what message-ids are already present. Meh. I don't even have a GMail account (which means this isn't even tested) - my Yahoo! brain-implant hasn't been removed yet.

Tags: brain implant, gmail, gml, hasn, hella, lyon, mail, meh, message ids, proof of concept, proprietary, python, realised, shock horror, smtp, transport protocol, ugly, whipped

  • I know what I like and I like what I know

    I actually wrote this about 2 years ago and, for some reason, never posted it but I was having a conversation about it recently and figured I might…

  • All the. Small things.

    Way back, when I was at Yahoo!, there was naturally a lot of discussion about the meteoric rise of Google. At the time we'd dumped Altavista and were…

  • Psychosomatic addict insane

    Specially for burr86 it's time for yet another in the far more than you ever wanted to know series. And today this…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.