Simple perl script to transfer IMAP email from host server to Gmail.

This was carried out on Debian Wheezy.

Grab the latest tarball of imapsync from fedorahosted.org (1.607 at writing of this post):

[html]
wget http://fedorahosted.org/released/imapsync/imapsync-1.607.tgz
[/html]

Decompress tarball:

[html]
tar -xvf imapsync-1.607.tgz
[/html]

Change into directory and then check if you have the correct dependencies for imapsync:

[html]
cd imapsync1607

perl -c imapsync
[/html]

The output we are after before proceeding is:

[html]
imapsync syntax OK
[/html]

*See Troubleshooting below for examples of missing dependencies.

Now install imapsync scripts:

[html]
make install
[/html]

*Need to have make package for this to work – apt-get install make

Now we simply run the script to transfer the mail from your IMAP server to Gmail (or other IMAP mail server):

[html]
imapsync –syncinternaldates –host1 <sourcehost> –port1 993 –ssl1 –user1 <sourceemailaccount> –password1 <sourcepassword> –host2 imap.googlemail.com –port2 993 –ssl2 –user2 <destinationemailaccount> –password2 <destinationpassword> –folder “Source/Folder” –prefix2 ‘[Gmail]/’ –regextrans2 ‘s/Source/Folder$/DestinationFolder/’ –skipsize –noauthmd5 –reconnectretry1 1 –reconnectretry2 1
[/html]

KEY TERMS

– source host (can be IP)
– source email account – e.g. test@test.com
– source email account password
– imap.googlemail.com – destination host
– destination email account
– destination email account
-“Source/Folder” – Folder on source email account to sync
-‘s/Source/Folder$/DestinationFolder/’ – substitute Source/Folder with Destination Folder

Full Example

[html]
imapsync –syncinternaldates –host1 localhost –port1 993 –ssl1 –user1 <source@email.com –password1 sourcepassword –host2 imap.googlemail.com –port2 993 –ssl2 –user2 destination@email.com –password2 destinationpassword –folder “Inbox/Client” –prefix2 ‘[Gmail]/’ –regextrans2 ‘s/Inbox/Client$/Client/’ –skipsize –noauthmd5 –reconnectretry1 1 –reconnectretry2 1
[/html]

The above will transfer the Inbox/Client Folder over to Client on Gmail account.

TROUBLESHOOTING

Can’t locate Mail/IMAPClient.pm in @INC (@INC contains: /etc/perl /usr/local/lib /perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/l ib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at imapsync line 5 74.
BEGIN failed–compilation aborted at imapsync line 574

[html]
apt-get install libmail-imapclient-perl
[/html]

Can’t locate File/Copy/Recursive.pm in @INC (@INC contains: /etc/perl /usr/local /lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /u sr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at imapsync li ne 586.
BEGIN failed–compilation aborted at imapsync line 586.

[html]
apt-get install libfile-copy-recursive-perl
[/html]

Can’t locate IO/Tee.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.1 4.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5 .14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at imapsync line 595.
BEGIN failed–compilation aborted at imapsync line 595.

[html]
apt-get install libio-tee-perl
[/html]

Can’t locate Unicode/String.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at imapsync line 596.
BEGIN failed–compilation aborted at imapsync line 596.

[html]
apt-get install libunicode-string-perl
[/html]

Written by Matt Cooper
Hi, I'm Matt Cooper. I started this blog to pretty much act as a brain dump area for things I learn from day to day. You can contact me at: matt@matthewc424.sg-host.com.