Skip to content

Instantly share code, notes, and snippets.

@blambi
Created September 8, 2013 14:29
Show Gist options
  • Save blambi/6485114 to your computer and use it in GitHub Desktop.
Save blambi/6485114 to your computer and use it in GitHub Desktop.
What I used to restore my mail from claws-mail imap caches after the servers disk died with to old backups.
#!/usr/bin/env python
from email.parser import Parser
import smtplib
import sys
import os
from hashlib import md5
def sendmail(mail_str):
headers = Parser().parsestr(mail_str)
print "Sending: '%s' from %s to %s" %(headers['subject'], headers['from'], headers['to'])
smtp = smtplib.SMTP('localhost')
#smtp.set_debuglevel(1)
smtp.sendmail(headers['to'], ['avraham@murdoch.mythos',], mail_str)
def is_sent(mail_str):
db = open('/home/avraham/mailer_sent.md5s').readlines()
hash = md5(mail_str).hexdigest()
if hash + '\n' in db:
return True
def mark_as_sent(mail_str):
hash = md5(mail_str).hexdigest()
db = open('/home/avraham/mailer_sent.md5s', 'a')
db.write(hash + "\n")
db.close()
new_mail = 0
old_mail = 0
if len(sys.argv) > 1:
for mail in sys.argv[1:]:
if os.path.basename(mail).isdigit():
mail_str = open(mail).read()
if is_sent(mail_str):
old_mail += 1
else:
new_mail += 1
sendmail(mail_str)
mark_as_sent(mail_str)
print "Old: %s\nNew: %s" %(old_mail, new_mail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment