#! /usr/bin/env/python # -*- coding:utf-8 -*- import imaplib import imaplib_1 c = imaplib_1.open_connection() try: # Find the "SEEN" messages in INBOX c.select('INBOX') typ, [response] = c.search(None, 'SEEN') if typ != 'OK': raise RuntimeError(response) # Create a new mailbox, "Archive.Today" msg_ids = ','.join(response.split(' ')) typ, create_response = c.create('Archive.Today') print 'CREATED Archive.Today:', create_response # Copy the messages print 'COPYING:', msg_ids c.copy(msg_ids, 'Archive.Today') # Look at the results c.select('Archive.Today') typ, [response] = c.search(None, 'ALL') print 'COPIED:', response finally: c.close() c.logout()
#! /usr/bin/env/python # -*- coding:utf-8 -*- import imaplib import imaplib_1 from imaplib_4 import parse_list_response c = imaplib_1.open_connection() try: c.select('Archive.Today') #What ids are in the mailbox typ,[msg_ids] = c.search(None,'ALL') print 'Starting message:',msg_ids # Find the message(s) typ,response = c.search(None,'(SUBJECT "test")') msg_ids = ','.join(msg_ids.split(' ')) print 'Matching messages:',msg_ids # What are the current flags? typ,response = c.fetch(msg_ids,'(FLAGS)') print 'Flags before:',response # Change the Deleted flag typ, response = c.store(msg_ids, '+FLAGS', r'(\Deleted)') # What are the flags now? typ,response = c.fetch(msg_ids,'(FLAGS)') print 'Flags after:',response # Really delete the message
#! /usr/bin/env/python # -*- coding:utf-8 -*- import imaplib import re from imaplib_1 import open_connection list_response_pattern = re.compile( r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)' ) def parse_list_response(line): match = list_response_pattern.match(line) flags,delimiter,mailbox_name = match.groups() mailbox_name = mailbox_name.strip('"') return (flags,delimiter,mailbox_name) if __name__ == '__main__': c = open_connection() try: typ,data = c.list() finally: c.logout() print 'Response code:',typ for line in data: print 'Server response:',line flags,delimiter,mailbox_name = parse_list_response(line) print 'Parsed response:',(flags,delimiter,mailbox_name)