def fetch(self, label = "INBOX"): mbox = ImapMailbox( (self.server, label) ) uids = mbox.get_all_uids() cache = Cache() for uid in uids: cached = cache.get_header(uid) if cached: yield cached else: msg = cache.set_header(uid, mbox.get_fields(uid, 'SUBJECT FROM DATE')) yield msg
#!/usr/bin/env python """ This example shows how to restore a backup created by backup_mailbox.py" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.ImapMessage import ImapMessage from ProcImap.Utils.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: restore_mailbox.py backupmbox imapmailbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[2])) backupsource = mbox(sys.argv[1], factory=ImapMessage) for message in backupsource: if message.has_key("X-ProcImap-Imapflags"): message.flags_from_string(message["X-ProcImap-Imapflags"]) del message["X-ProcImap-Imapflags"] if message.has_key("X-ProcImap-ImapInternalDate"): message. internaldate_from_string(message["X-ProcImap-ImapInternalDate"]) del message["X-ProcImap-ImapInternalDate"] mailbox.add(message) mailbox.close() backupsource.close() sys.exit(0)
print >> sys.stderr, "You must use a backup mailbox" sys.exit(1) if 'imap.gmail.com' in mailbox_server.servername: if options.cachefile is None: print >> sys.stdout, "Gmail Mailboxes require a cache" sys.exit(1) else: cache = GmailCache(mailbox_server) cache.load(options.cachefile) cache.autosave = options.cachefile cache.update() for mailbox_name in args[2:]: mailbox = ImapMailbox((mailbox_server.clone(), mailbox_name)) print("\n\nProcessing mailbox %s" % mailbox.name) encrypted = mailbox.search('UNDELETED HEADER Content-Type encrypted') for uid in encrypted: print(" Decrypting UID %s" % uid) message = mailbox[uid] labels = [mailbox_name] # all the mailboxes the mail appears (for gmail) if options.backup is not None: print(" Backing up the original (encrypted) message") backupbox.add(message) print(" Deleting the original (encrypted) message") if is_gmail_box(mailbox): labels = cache.get_labels("%s.%s" % (mailbox_name, uid)) delete(mailbox, uid) else: mailbox.discard(uid)
#!/usr/bin/env python """ This example shows how to create a backup of an IMAP mailbox into an mbox folders. The IMAP attributes are stored in each message in special header fields" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.Utils.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: backup_mailbox.py imapmailbox backupmbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[1])) backuptarget = mbox(sys.argv[2]) backuptarget.lock() for message in mailbox: message.add_header("X-ProcImap-Imapflags", message.flagstring()) message.add_header("X-ProcImap-ImapInternalDate", message.internaldatestring()) backuptarget.add(message) mailbox.close() backuptarget.close() sys.exit(0)
import mailbox from email.Utils import make_msgid if len(sys.argv) < 3: print "Usage: mbox2imap.py <mboxpath> <imapfolder>" print "To change the imap server, edit the script" sys.exit(2) # Variables toserver = ImapServer("imap.gmail.com", "*****@*****.**", "secret", ssl=True) toboxname = sys.argv[2] # Processing tobox = ImapMailbox((toserver, toboxname)) fromboxname = sys.argv[1] frombox = mailbox.mbox(fromboxname) frombox.lock() tobox.lock() i = 1 print "Processing mbox file %s with %s messages" % (fromboxname, len(frombox)) for message in frombox: print "%s" % i if message['Message-Id'] is None: print " WARNING: message has no message-id (mesage ID will be added)" message.add_header("Message-Id", make_msgid('katamon.mbox2imap') ) if True: try:
if line_match: record[line_match.group('luid')] = line_match.group('filename') else: print "Can't understand line in record file" record_file.close() except IOError: pass record_file = open(options.record, 'a') mailbox = None try: for label in labels: if not label in include: continue if label in exclude: continue if mailbox is None: mailbox = ImapMailbox((server, label)) else: mailbox.switch(label) if options.search == '': uids = mailbox.get_all_uids() else: uids = mailbox.search(options.search) for uid in uids: if not record.has_key("%s.%s" % (label, uid)): descr = "%s.%s " % (label, uid) if options.summary: s = summary(mailbox, uid, printuid=False, printout=False)[0] descr += "\t" + s[3:] print descr except KeyboardInterrupt: print ""
if len(sys.argv) < 3: print "Usage: mbox2imap.py <mboxpath> <imapfolder>" print "To change the imap server, edit the script" sys.exit(2) # Variables toserver = ImapServer("imap.gmail.com", "*****@*****.**", "secret", ssl=True) toboxname = sys.argv[2] # Processing tobox = ImapMailbox((toserver, toboxname)) fromboxname = sys.argv[1] frombox = mailbox.mbox(fromboxname) frombox.lock() tobox.lock() i = 1 print "Processing mbox file %s with %s messages" % (fromboxname, len(frombox)) for message in frombox: print "%s" % i if message['Message-Id'] is None: print " WARNING: message has no message-id (mesage ID will be added)" message.add_header("Message-Id", make_msgid('katamon.mbox2imap')) if True: try: tobox.add(message)
#!/usr/bin/env python """ This example shows how to restore a backup created by backup_mailbox.py" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.ImapMessage import ImapMessage from ProcImap.Utils.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: restore_mailbox.py backupmbox imapmailbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[2])) backupsource = mbox(sys.argv[1], factory=ImapMessage) for message in backupsource: if message.has_key("X-ProcImap-Imapflags"): message.flags_from_string(message["X-ProcImap-Imapflags"]) del message["X-ProcImap-Imapflags"] if message.has_key("X-ProcImap-ImapInternalDate"): message.internaldate_from_string( message["X-ProcImap-ImapInternalDate"]) del message["X-ProcImap-ImapInternalDate"] mailbox.add(message) mailbox.close() backupsource.close() sys.exit(0)
#!/usr/bin/env python """ This example shows how to create a backup of an IMAP mailbox into an mbox folders. The IMAP attributes are stored in each message in special header fields" """ from ProcImap.ImapMailbox import ImapMailbox from ProcImap.MailboxFactory import MailboxFactory from mailbox import mbox import sys # usage: backup_mailbox.py imapmailbox backupmbox mailboxes = MailboxFactory('/home/goerz/.procimap/mailboxes.cfg') server = mailboxes.get_server('Gmail') mailbox = ImapMailbox((server, sys.argv[1])) backuptarget = mbox(sys.argv[2]) backuptarget.lock() for message in mailbox: message.add_header("X-ProcImap-Imapflags", message.flagstring()) message.add_header("X-ProcImap-ImapInternalDate", message.internaldatestring()) backuptarget.add(message) mailbox.close() backuptarget.close() sys.exit(0)
del record[labeluid] if options.verbose: print "Deleted %s (deleted label %s) " % ( labeluid, record_label) + "from record" labeluids = record.keys() labeluids.sort() # delete missing uids label = '' mailbox = None uids = [] for labeluid in labeluids: record_label = re.split("\.\d+$", labeluid)[0] if (label != record_label): label = record_label if mailbox is not None: mailbox.close() mailbox = ImapMailbox((server, label)) uids = mailbox.get_all_uids() uid = int(re.split("^.*\.", labeluid)[1]) if uid not in uids: del record[labeluid] if options.verbose: print "Deleted %s from record" % labeluid if mailbox is not None: mailbox.close() labeluids = record.keys() labeluids.sort() # write out result record_file = open(options.record, 'w') for labeluid in labeluids: print >>record_file, "%s : %s" % (labeluid, record[labeluid]) # delete those files that are not in filenames
if line_match: record[line_match.group('luid')] = line_match.group('filename') else: print "Can't understand line in record file" record_file.close() except IOError: pass record_file = open(options.record, 'a') mailbox = None try: for label in labels: if not label in include: continue if label in exclude: continue if mailbox is None: mailbox = ImapMailbox((server, label)) else: mailbox.switch(label) if options.search == '': uids = mailbox.get_all_uids() else: uids = mailbox.search(options.search) for uid in uids: if record.has_key("%s.%s" % (label, uid)): if options.verbose: print "Skip %s.%s" % (label, uid) continue try: size = mailbox.get_size(uid) message = mailbox.get(uid) except Exception, errormessage: record_file.flush()
print "subject:",header['subject'],"copied to archive" print "" return header def fullprocess(self,message): """Only called if header.fullprocess=True""" return if __name__=="__main__": from ProcImap import imaplib2 from ProcImap.Utils import log import os password = open( os.getenv("HOME")+"/.me").read().strip() while True: try: s = ImapServer("mail.me.com","simsong",password) mbx = ImapMailbox(path=(s,"INBOX")) mbx.trash = 'Trash' # specify a trash box print "calling ImapWatcher" w = ImapWatcher(mbx) w.run() except: log("caught exception") pass