Skip to content

Instantly share code, notes, and snippets.

@pixline
Created June 28, 2013 04:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixline/5882456 to your computer and use it in GitHub Desktop.
Save pixline/5882456 to your computer and use it in GitHub Desktop.
IMAP to IMAP batch mailbox migration. Requires imapsync (http://imapsync.lamiral.info/)
#!/usr/bin/php
#
# Usage: ./imapsync.sh accounts.csv
#
# CSV account schema:
# old_username;old_password;new_username;new_password
#
<?php
define('HOST_FROM', 'imap.example.com');
define('HOST_TO', 'imap.example.org');
define('DOMAIN', 'example.com');
if(!$argv[1]) die("USAGE: ".$argv[0]." <csv_file>\n\n");
$fh = fopen($argv[1], 'r+');
$contents = fread($fh, filesize($argv[1]));
fclose($fh);
$cn = explode("\n",$contents);
foreach($cn as $c):
$n = explode(";",$c);
if($n[0]!=""):
$cmd = "/usr/bin/imapsync \
--host1 ".HOST_FROM." --user1 ".$n[0]." --password1 ".$n[1]." \
--host2 ".HOST_TO." --user2 ".$n[2]."@".DOMAIN." --password2 ".$n[3]." \
--subscribed --subscribe --useheader Message-ID --idatefromheader --authmech1 LOGIN --authmech2 LOGIN --ssl2 \
--buffersize 614400000 --skipsize";
passthru($cmd);
endif;
endforeach;
?>
@pixline
Copy link
Author

pixline commented Jun 28, 2013

Debian setup:

apt-get install libmail-imapclient-perl libdigest-md5-file-perl \
libterm-readkey-perl libio-socket-ssl-perl libfile-spec-perl libdigest-hmac-perl

perl -MCPAN -e 'install Authen::NTLM'

Perl CPAN setup:

perl -MCPAN -e 'install Mail::IMAPClient' 
perl -MCPAN -e 'install Digest::MD5' 
perl -MCPAN -e 'install Term::ReadKey' 
perl -MCPAN -e 'install IO::Socket::SSL' 
perl -MCPAN -e 'install File::Spec' 
perl -MCPAN -e 'install Digest::HMAC_MD5' 
perl -MCPAN -e 'install Authen::NTLM' 
perl -MCPAN -e 'install Time::HiRes' 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment