Importing Evolution Contacts into GMail

I may come back and clean up this script at a future point, but I’m putting it out there, warts and all, because some may find it useful.

#!/usr/bin/perl
#
# evol_gmail.pl
#
# Adapted from
# http://timlegge.blogspot.com/2007/07/migrating-evolution-contacts-to-gmail.html
# by Victor Chudnovsky, with a look at
# http://theregoesdave.com/2008/10/17/importing-contacts-into-gmail-guide-to-google-contact-csv-fields/
#
# This code is licensed under a GPL.


use strict;

my $line;
my $islist = "False";

# Common fields
my $name;
my $default_email;
my $notes;

#Fields by Section
my @description = ("Personal","Work");
my @email = ();
my @im = ();
my @phone = ();
my @mobile = ();
my @pager = ();
my @fax = ();
my @company = ();
my @title = ();
my @other = ();
my @address = ();

print "Name,E-mail,Notes,";

for (my $i=1; $i<3; $i++) {
    print "Section $i - Description,";
    print "Section $i - Email,";
    print "Section $i - IM,";
    print "Section $i - Phone,";
    print "Section $i - Mobile,";
    print "Section $i - Pager,";
    print "Section $i - Fax,";
    print "Section $i - Company,";
    print "Section $i - Title,";
    print "Section $i - Other,";
    if ( $i < 2 ) {
    print "Section $i - Address,";
    }
    else {
    print "Section $i - Address";
    }
}
print "\n";

while ( $line =  ) {
    chomp ($line);
    chop ($line) ; # trailing ^M
    
    if ( $line =~ /^BEGIN:VCARD*/ ) {
#   print "VCARD BEGIN\n";
    # Initialize the variables upon hitting a BEGIN:VCARD
    $islist = "False";
    $name = "";
    $default_email = "";
    $notes = "";
    @email = ();
    @im = ();
    @phone = ();
    @mobile = ();
    @pager = ();
    @fax = ();
    @company = ();
    @title = ();
    @other = ();
    @address = ();
    }
    
    if ( $line =~ /^X-EVOLUTION-LIST:TRUE.*/ ) {
#   print "Unable to process Email Lists\n";
    $islist = "True";
    }
    
    if ( $line =~ /^FN:*/ ) {
#   print "Full Name\n";
    $line =~ s/^FN://g;
    $line =~ s/,/;/g;
    $name = $line;
    }
    
    if ( $line =~ /^EMAIL;.*TYPE*/ ) {
#   print "Email Found - ";
#
    my $tmpemail = $line;
    
    $tmpemail =~ s/EMAIL;.*TYPE=.*://g;
    $tmpemail =~ s/,/;/g;
    $tmpemail =~ s/ //g;
#   print "Email found:" . $tmpemail;
    
    if ($line =~ /TYPE=HOME/ ) {
#   print "Home\n"; 
        @email[0] = $tmpemail;
    }
    elsif ($line =~ /TYPE=WORK/ ) { 
#       print "Work\n"; 
        @email[1] = $tmpemail;
    }
    elsif ($line =~ /TYPE=OTHER/ ) {
#       print "Other\n";
        @email[2] = $tmpemail;
    }
    elsif ($line =~ /TYPE=INTERNET/ ) {
#       print "Internet\n";
        @email[3] = $tmpemail;
    }
    } # end email
    
    if ( $line =~ /^NOTE:*/ ) {
#   print "Note\n";
    $line =~ s/^NOTE://g;
    $line =~ s/,/;/g;
    $notes = $line;
    } # end note
    
    if ($line =~ /^X-MSN:*/) {
    my $tmpim = $line;
    $tmpim =~ s/X-.*://g;
    $tmpim =~ s/ //g;
    $tmpim = "MSN:" . $tmpim;
    if ($line =~ /TYPE=HOME/ ) {
#       print "Home\n"; 
        @im[0] = $tmpim;
    }
    elsif ($line =~ /TYPE=WORK/ ) { 
#       print "Work\n"; 
        @im[1] = $tmpim;
    }
    elsif ($line =~ /TYPE=OTHER/ ) {
#       print "Other\n";
        @im[2] = $tmpim;
    }
    elsif ($line =~ /TYPE=INTERNET/ ) {
#       print "Internet\n";
        @im[3] = $tmpim;        
    }   
    } #end MSN
    
    if ( $line  =~ /^X-JABBER:*/ ) {
    my $tmpim = $line;
    $tmpim =~ s/X-.*://g;
    $tmpim =~ s/ //g;
    if ($line =~ /TYPE=HOME/ ) {
#       print "Home\n"; 
        @im[0] = $tmpim;
    }
    elsif ($line =~ /TYPE=WORK/ ) { 
#       print "Work\n"; 
        @im[1] = $tmpim;
    }
    elsif ($line =~ /TYPE=OTHER/ ) {
#       print "Other\n";
        @im[2] = $tmpim;
    }
    elsif ($line =~ /TYPE=INTERNET/ ) {
#       print "Internet\n";
        @im[3] = $tmpim;        
    }   
    } #end JABBER
    
    if ( $line  =~ /^X-YAHOO:*/ ) {
    my $tmpim = $line;
    $tmpim =~ s/X-.*://g;
    $tmpim =~ s/ //g;
    $tmpim = "Yahoo:" . $tmpim;
#   print "Found IM:" . $tmpim;
    if ($line =~ /TYPE=HOME/ ) {
#       print "Home\n"; 
        @im[0] = $tmpim;
    }
    elsif ($line =~ /TYPE=WORK/ ) { 
#       print "Work\n"; 
        @im[1] = $tmpim;
    }
    elsif ($line =~ /TYPE=OTHER/ ) {
#       print "Other\n";
        @im[2] = $tmpim;
    }
    elsif ($line =~ /TYPE=INTERNET/ ) {
#       print "Internet\n";
        @im[3] = $tmpim;        
    }   
    } #end YAHOO
    
    
    if ( $line =~ /^TEL;.*:*/) {
#   print "Telephone found\n";
    my $tmpphone = $line;
    $tmpphone =~ s/TEL;.*://g;
    $tmpphone =~ s/,/;/g;
    
    if ($line =~ /TYPE=HOME/ ) {
#       print "Home\n";
        if ( $line =~ /TYPE=VOICE/ ) {
        @phone[0] = $tmpphone;
        }
        elsif ($line =~ /TYPE=FAX/ ) {
        @fax[0] = $tmpphone;
        }
    }
    elsif ($line =~ /TYPE=WORK*/ ) { 
#       print "Work\n"; 
        if ( $line =~ /TYPE=VOICE/ ) {
        @phone[1] = $tmpphone;
        }
        elsif ($line =~ /TYPE=FAX/ ) {
        @fax[1] = $tmpphone;
        }
    }
    elsif ($line =~ /TYPE=CELL/ ) {
#       print "Cell\n";
        $tmpphone =~ s/TEL;TYPE=CELL.*://g;
        @mobile[0] = $tmpphone;
        }
    elsif ($line =~ /TYPE=PAGER/ ) {
#       print "Pager\n";
        $tmpphone =~ s/TEL;TYPE=PAGER.*://g;
        @pager[0] = $tmpphone;
    }
    } # end PHONE
    
    if ( $line =~ /^ORG:/ ) {
#   print "Company\n";
    $line =~ s/^ORG://g;
    $line =~ s/;;$//g;
    $line =~ s/,/;/g;
    @company[1] = $line;
    } # end company
    
    if ( $line =~ /^TITLE:/ ) {
#   print "Title\n";
    $line =~ s/^TITLE://g;
    $line =~ s/;;$//g;
    $line =~ s/,/;/g;
    @title[1] = $line;
    } # end title
    
    if ( $line  =~ /^OTHER:/ ) {}   # No field in Evolution
    
    if ( $line =~ /^ADR;TYPE*/ ) {
#   print "Address Found - ";
#
    my $tmpaddr = $line;
    $tmpaddr =~ s/ADR;TYPE=....*;*://g;
    
    # find the address parts
    my @tmpaddress = split /;/, $tmpaddr; 
    # put the address in prefered order (pobox, line1, line2, city, prov, postal code, country
    $tmpaddr = "\"";
    if ( @tmpaddress[0] ne "" )
    { $tmpaddr = $tmpaddr . @tmpaddress[0] . chr(13) };
    if ( @tmpaddress[2] ne "" )
    { $tmpaddr = $tmpaddr . @tmpaddress[2] . chr(13) };
    if ( @tmpaddress[1] ne "" )
    { $tmpaddr = $tmpaddr . @tmpaddress[1] . chr(13) };
    if ( @tmpaddress[3] ne "" or @tmpaddress[4] ne "" or @tmpaddress[5] ne "" )
    { $tmpaddr = $tmpaddr . @tmpaddress[3] . ", " . @tmpaddress[4] . "  " . @tmpaddress[5]. chr(13) };
    if ( @tmpaddress[6] ne "" )
    { $tmpaddr = $tmpaddr . @tmpaddress[6] };
    $tmpaddr = $tmpaddr . "\"";
    
#   print $tmpaddr;
    
    if ($line =~ /TYPE=HOME/ ) {
#       print "Home\n"; 
        @address[0] = $tmpaddr;
    }
    elsif ($line =~ /TYPE=WORK/ ) { 
#       print "Work\n"; 
        @address[1] = $tmpaddr;
    }
    elsif ($line =~ /TYPE=OTHER/ ) {
#       print "Other\n";
        @address[2] = $tmpaddr;
    }
    } # end address



# Hit END:VCARD, output the data
    if  ( $line =~ /^END:VCARD*/ ) {
#   print "VCARD END\n";
    if ( $islist eq "False" ) {
        if ( $name eq "" )
        { print ","; }
        else
        { print $name.","; }
        
        for (my $i=0; $i < 4; $i++ ) {
        if ($email[$i] ne "") 
        { $default_email = $email[$i]; }
        }
        
        if ( $default_email eq "" )
        { print ","; }
        else
        { print $default_email.","; }
        
        if ( $notes eq "" )
        { print ","; }
        else
        { print $notes.","; }
        
        
        for (my $i=0; $i<2; $i++) {
        print $description[$i].",";
        #try alternate addresses if home email is missing
        if ( $i eq 0 ) {
            if ( $email[0] eq "" ) {
            for ( my $k = 2; $k<4; $k++) {
                if ( $email[$k] ne "" ) {
                $email[$i] = $email[$k]; 
                }
            }
            }
            print $email[0].",";
        }
        else 
        { print $email[$i].",";}
        print $im[$i].",";
        print $phone[$i].",";
        print $mobile[$i].",";
        print $pager[$i].",";
        print $fax[$i].",";
        print $company[$i].",";
        print $title[$i].",";
        print $other[$i].",";
        if ( $i < 1 ) {
            print $address[$i].",";
        }
        else {
            print $address[$i];
        }
        
        }
        
        print "\n";
    } # end if $islist = False
    } # end END:VCARD
    
} # end of while loop

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>