if args.list or args.download: if args.download: if os.path.exists(args.download): raise FileExistsError(args.download) mbox = mailbox.mbox(args.download, create=True) typ, data = M.search(None, 'ALL') for num in data[0].split(): typ, d2 = M.fetch(num, '(RFC822)') for val in d2: if type(val) == tuple: (a, b) = val num = a.decode('utf-8').split()[0] msg = BytesParser().parsebytes(b) print(msg['from'], msg['subject'], msg['date']) if args.download: mbox.add(msg) elif args.upload: mbox = mailbox.mbox(args.upload, create=False) for msg in mbox: M.append('Inbox', '', '', msg.as_bytes()) try: M.expunge() M.close() M.logout() except imaplib.IMAP4.abort as e: #print("IMAP abort") pass