def main(): try: search = sys.argv[1] except: search = 'ALL' c = imaplib_connect.open_connection(False, './.pymotw') mailbox_name = '[Gmail]/Chats' try: #num = c.select(mailbox_name, readonly=True) typ, msg_ids = c.select(mailbox_name, readonly=True) print mailbox_name, typ, msg_ids print msg_ids[0] for i in range(int(msg_ids[0])): print i arraynum = list(reversed([num for num in range(int(msg_ids[0]) ) if isinstance(num, int)])) for m in arraynum: try: print m open('mychats/%s.email' %m, 'w').write(get_msg_body(c, m)) except Exception, e: sys.stderr.write(str(e)) except Exception, e: sys.stderr.write(str(e)) print 'trash'
def main(): try: search = sys.argv[1] except: search = 'ALL' c = imaplib_connect.open_connection(False, './.pymotw') mailbox_name = '[Gmail]/Chats' try: #num = c.select(mailbox_name, readonly=True) typ, msg_ids = c.select(mailbox_name, readonly=True) print mailbox_name, typ, msg_ids print msg_ids[0] for i in range(int(msg_ids[0])): print i arraynum = list( reversed([ num for num in range(int(msg_ids[0])) if isinstance(num, int) ])) for m in arraynum: try: print m open('mychats/%s.email' % m, 'w').write(get_msg_body(c, m)) except Exception, e: sys.stderr.write(str(e)) except Exception, e: sys.stderr.write(str(e)) print 'trash'
def fetch_email_colour_counts(self): try: # Open connection to imap server c = imaplib_connect.open_connection() # Select the email account inbox c.select('INBOX', readonly=True) # Search each colour category for number of unread emails red_emails_count = self._fetch_email_count(c, 'red_senders') green_emails_count = self._fetch_email_count(c, 'green_senders') blue_emails_count = self._fetch_email_count(c, 'blue_senders') orange_emails_count = self._fetch_email_count(c, 'orange_senders') pink_emails_count = self._fetch_email_count(c, 'pink_senders') sky_emails_count = self._fetch_email_count(c, 'sky_senders') white_emails_count = self._fetch_email_count(c, 'white_senders') # Close connection to imap server c.close() c.logout() except: pass return (red_emails_count, green_emails_count, blue_emails_count, orange_emails_count, pink_emails_count, sky_emails_count, white_emails_count)
def main(): c = imaplib_connect.open_connection(False,'') c.select('[Gmail]/All Mail', readonly=True) query = '(FROM "*****@*****.**")' fetch_ids_from_query(c, query)
def main(): c = imaplib_connect.open_connection(False, '') c.select('[Gmail]/All Mail', readonly=True) query = '(FROM "*****@*****.**")' fetch_ids_from_query(c, query)
def fetch_thread(i, mailboxfolder, mailbox_name, account): global c try: typ, msg_data = c.fetch(str(i+1), '(RFC822)') file = os.path.join(mailboxfolder, str(i) + ".eml") with open(file, 'wb') as f: f.write(msg_data[0][1]) except Exception as err: c = imaplib_connect.open_connection(False, **account) c.select(mailbox_name, readonly=False) print(err, "curl") return (False, i) return (True, i)
def fetch_process(*p): i = p[0] mailboxfolder = p[3] account = p[1] mailbox_name = p[2] try: c = imaplib_connect.open_connection(False, **account) c.select(mailbox_name) typ, msg_data = c.fetch(str(i + 1), '(RFC822)') print(type(msg_data)) file = os.path.join(mailboxfolder, str(i) + ".eml") with open(file, 'wb') as f: f.write(msg_data[0][1]) except Exception as err: print(err) return (False, i) return (True, i)
#!/usr/bin/env python # -*- coding: utf-8 -*- import imaplib from imaplib_connect import open_connection if __name__ == '__main__': c = open_connection() try: typ, data = c.list(directory='Archive') finally: c.logout() print 'Response code:', typ for line in data: print 'Server response:', line
- INBOX - Deleted Messages - Archive - Example - 2016 """ """After selecting the mailbox, use search() to retrieve the IDs of messages in the mailbox. """ import imaplib import imaplib_connect print() with imaplib_connect.open_connection() as c: typ, mbox_data = c.list() for line in mbox_data: flags, delimiter, mbox_name = parse_list_response(line) c.select('"{}"'.format(mbox_name), readonly=True) typ, msg_ids = c.search(None, 'ALL') print(mbox_name, typ, msg_ids) """ Message IDs are assigned by the server, and are implementation dependent. The IMAP4 protocol makes a distinction between sequential IDs for messages at a given point in time during a transaction and UID identifiers for messages, but not all servers implement both. """ """ EXPECTED OUTPUT: Response code: OK
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ __version__ = "$Id: imaplib_archive_read.py 1882 2009-01-04 15:38:33Z dhellmann $" #end_pymotw_header import imaplib import imaplib_connect c = imaplib_connect.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')
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ __version__ = "$Id$" #end_pymotw_header import imaplib from imaplib_connect import open_connection if __name__ == '__main__': c = open_connection() try: typ, data = c.list(directory='Archive') finally: c.logout() print 'Response code:', typ for line in data: print 'Server response:', line
# Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ #end_pymotw_header import imaplib import re from imaplib_connect 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.decode('utf-8')) flags, delimiter, mailbox_name = match.groups() mailbox_name = mailbox_name.strip('"') return (flags, delimiter, mailbox_name) with open_connection() as c: typ, data = c.list() 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))
msg_from = email.utils.parseaddr(msg.get('from'))[1] msg_to = email.utils.parseaddr(msg.get('to'))[1] msg_date = msgdate.strftime("%F_%H-%M-%S") myfile = 'chats/%s/%s.html' % (msg_from, msg_date) ensure_dir(myfile) print myfile open('chats/%s/%s.html' % (msg_from, msg_date) ,'w').write(response_part[1]) #print type(msgdate), datetime.datetime.fromtuple(msgdate) #msgdate.strftime("%F_%H:%M:%S") for response_part in msg_data: if isinstance(response_part, tuple): return response_part[1] c = imaplib_connect.open_connection(False, '~/.pymotw') mailbox_name = 'wasachat' try: c.select(mailbox_name, readonly=True) typ, msg_ids = c.search(None, search) print mailbox_name, typ, msg_ids for m in reversed([num for num in msg_ids[0].split(' ') if num]): try: print m open('mychats/%s.email' %m, 'w').write(get_msg_body(m)) #print get_msg_body(m) except: print sys.exc_info()[2]
#! /usr/bin/python3 import imaplib import pprint import imaplib_connect with imaplib_connect.open_connection() as c: c.select('INBOX', readonly=True) print('HEADER:') typ, msg_data = c.fetch('1', '(BODY.PEEK[HEADER])') for response_part in msg_data: if isinstance(response_part, tuple): print(response_part[1]) print('\nBODY TEXT:') typ, msg_data = c.fetch('1', '(BODY.PEEK[TEXT])') for response_part in msg_data: if isinstance(response_part, tuple): print(response_part[1]) print('\nFLAGS:') typ, msg_data = c.fetch('1', '(FLAGS)') for response_part in msg_data: print(response_part) print(imaplib.ParseFlags(response_part))
msg_date = msgdate.strftime("%F_%H-%M-%S") myfile = 'chats/%s/%s.html' % (msg_from, msg_date) ensure_dir(myfile) print myfile open('chats/%s/%s.html' % (msg_from, msg_date), 'w').write(response_part[1]) #print type(msgdate), datetime.datetime.fromtuple(msgdate) #msgdate.strftime("%F_%H:%M:%S") for response_part in msg_data: if isinstance(response_part, tuple): return response_part[1] c = imaplib_connect.open_connection(False, '~/.pymotw') mailbox_name = 'wasachat' try: c.select(mailbox_name, readonly=True) typ, msg_ids = c.search(None, search) print mailbox_name, typ, msg_ids for m in reversed([num for num in msg_ids[0].split(' ') if num]): try: print m open('mychats/%s.email' % m, 'w').write(get_msg_body(m)) #print get_msg_body(m) except: print sys.exc_info()[2]
except Exception as err: c = imaplib_connect.open_connection(False, **account) c.select(mailbox_name, readonly=False) print(err, "curl") return (False, i) return (True, i) def done(fn): print(fn.cancelled(), fn.done()) options = imaplib_connect.readconf() for account in options['account']: with imaplib_connect.open_connection(False, **account) as c: path = options['path'] usernamepath = os.path.join(path, account["username"]) if os.path.isdir(usernamepath) == False: os.mkdir(usernamepath) typ, data = c.list() for line in data: flags, delimiter, mailbox_name = parse_list_response(line) # 输入参数是bytes类型,返回str类型 try: mailbox_name_utf8 = imap_utf7.decode( mailbox_name.encode("UTF-7")) except Exception as err: #mailbox_name_utf8 = "INBOX"