from markdownify import markdownify import imaplib import email from email.header import decode_header import os import re username = "" password = "" imapserver = "" N = 500 if not os.path.isdir('notes'): os.mkdir('notes') imap = imaplib.IMAP4_SSL(imapserver) imap.login(username, password) status, messages = imap.select("Notes", True) messages = int(messages[0]) for i in range(messages, max(0, messages-N), -1): res, msg = imap.fetch(str(i), "(RFC822)") for response in msg: if isinstance(response, tuple): msg = email.message_from_bytes(response[1]) subject, encoding = decode_header(msg["Subject"])[0] if isinstance(subject, bytes): subject = subject.decode(encoding) From, encoding = decode_header(msg.get("From"))[0] if isinstance(From, bytes): From = From.decode(encoding) print(str(i) + ' - ' + subject) filepath = subject + '.md' filepath = os.path.join('notes', filepath) if msg.is_multipart(): for part in msg.walk(): content_type = part.get_content_type() content_disposition = str(part.get("Content-Disposition")) try: body = part.get_payload(decode=True).decode() except: pass if content_type == "text/plain" and "attachment" not in content_disposition: open(filepath, "wb").write(body.replace('\r\n', '\n').encode('utf8')) elif "attachment" in content_disposition: print('/!\\ attachment') else: content_type = msg.get_content_type() body = msg.get_payload(decode=True).decode() if content_type == "text/plain": open(filepath, "wb").write(body.replace('\r\n', '\n').encode('utf8')) if content_type == "text/html": print('>> html') md = markdownify(body) if md[:4] == 'html': md = md[4:] # fastmail specific: replace internal links md = re.sub(r'\[(.*)\]\(https://app.fastmail.com.*\)', '[[\\1]]', md) open(filepath, "wb").write(md.replace('\r\n', '\n').encode('utf8')) imap.close() imap.logout()