Skip to content

Instantly share code, notes, and snippets.

@nbeernink
Last active April 3, 2018 12:10
Show Gist options
  • Select an option

  • Save nbeernink/55d48660aabf21d9722cab886c625461 to your computer and use it in GitHub Desktop.

Select an option

Save nbeernink/55d48660aabf21d9722cab886c625461 to your computer and use it in GitHub Desktop.
batch migrate mail data via imapsync read from a csv file
#!/usr/bin/env python3
import argparse
import csv
import os
import subprocess
parser = argparse.ArgumentParser(description='Batch migrate e-mail with imapsync reading from a CSV-file')
parser.add_argument('csvfile',help='The CSV file to parse. The file should have a header like this: host1,user1,password1,host2,user2,password2')
parser.add_argument('--dry',action='store_true',help="Use imapsync's dry mode: don't sync, show what would happen")
args = parser.parse_args()
if os.path.isfile(args.csvfile):
input_file = csv.DictReader(open(args.csvfile))
# TODO check if header matches required columns
for row in input_file:
imapsync_cmd=('imapsync' +\
' --host1 ' + row['host1'] +\
' --user1 ' + row['user1'] +\
' --password1 ' + row['password1'] +\
' --host2 ' + row['host2'] +\
' --user2 ' + row['user2'] +\
' --password2 ' + row['password2'] +\
' --logdir ~/LOG_imapsync'
)
if args.dry:
#TODO try using subprocess here
os.system(imapsync_cmd + ' --dry')
else:
os.system(imapsync_cmd)
else:
print("Can't find the file specified: " + args.csvfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment