#!/usr/bin/env python3 """Fetch body of email sequence number 2 from shonuff inbox.""" import imaplib, email HOST = "mail.germainebrown.com" PORT = 993 USER = "shonuff@germainebrown.com" PW = "Catches.bullets1985" conn = imaplib.IMAP4_SSL(HOST, PORT) conn.login(USER, PW) conn.select("INBOX") status, data = conn.fetch("2", "(RFC822)") if status == "OK": msg = email.message_from_bytes(data[0][1]) body = "" if msg.is_multipart(): for part in msg.walk(): ct = part.get_content_type() if ct == "text/plain": payload = part.get_payload(decode=True) if payload: body += payload.decode("utf-8", errors="replace") elif ct == "text/html" and not body: payload = part.get_payload(decode=True) if payload: body += "[HTML content omitted]\n" + payload.decode("utf-8", errors="replace")[:500] else: payload = msg.get_payload(decode=True) if payload: body = payload.decode("utf-8", errors="replace") print(body[:3000]) else: print(f"Failed: {status}") conn.logout()