def printMailboxContent(conn) : msgList = pop_helpers.listMessages(conn) numMessages = len(msgList) print "There are %i messages (via POP)." % (numMessages,) for (sid, emailObj) in pop_helpers.iterMessages(conn, msgList) : #emailObj = email.message_from_string(rawMail) for headerType, headerTrunc in mail_helpers.iterEmailHeaders(emailObj, truncateAt = 70) : if mail_helpers.IsBaseHeader(headerType) : headerDisplay = mail_helpers.RemoveLineBreaks(headerTrunc) print " %-30s %s" % (headerType, headerDisplay,) print
def printPopMailboxContent(conn) : """ Reads the latest mail via POP. """ msgList = pop_helpers.listMessages(conn) numMessages = len(msgList) if 0 : print("There are %i messages (via POP)." % (numMessages,)) newestMailObj = None for (sid, emailObj) in pop_helpers.iterMessages(conn, msgList, newestFirst = True) : if 0 : print("ID = %r" % (sid,)) for headerType, headerTrunc in mail_helpers.iterEmailHeaders(emailObj, truncateAt = 70) : if mail_helpers.IsBaseHeader(headerType) : headerDisplay = mail_helpers.RemoveLineBreaks(headerTrunc) print(" %-30s %s" % (headerType, headerDisplay,)) print() # empty line newestMailObj = emailObj break # show informations of the newest mail if newestMailObj is None : print("Could not access POP server.", file=sys.stderr) sys.exit(2) return None # print all parts of a multipart mail if 0 : for (i, msg) in enumerate(newestMailObj.get_payload()) : print("PART", i) print(" charset =", msg.get_charset()) print(" contentType =", msg.get_content_type()) print(" contentMainType =", msg.get_content_maintype()) print(" contentSubType =", msg.get_content_subtype()) print(" defaultType =", msg.get_default_type()) print(" filename =", msg.get_filename()) print(" boundary =", msg.get_boundary()) print(" contentCharset =", msg.get_content_charset()) print(" charsets =", msg.get_charsets()) print() print(" <CONTENT>") print(msg.as_string()) print(" </CONTENT>") print() pass return newestMailObj
def printMailboxContent(conn, mailbox, **keywords) : """ @keyword uniqueIdentifier: If True (default) use UID instead of sequentialID @type uniqueIdentifier: bool @keyword newestFirst: If True sort from newest to oldest. Default is False @type newestFirst: bool """ (okSelect, msgCountList) = conn.select(mailbox, readonly = True) mailboxPretty = decodeMailboxName(mailbox) print("%(mailboxPretty)s" % locals()) prettyMailbox = decodeMailboxName(mailbox) for id, emailObj in iterMailboxContent(conn, mailbox, **keywords) : print("ID = %(id)s" % locals()) #emailTo = emailObj['To'] #emailFrom = email.utils.parseaddr(emailObj['From']) for headerType, headerTrunc in mail_helpers.iterEmailHeaders(emailObj, truncateAt = 70) : if mail_helpers.IsBaseHeader(headerType) : headerDisplay = mail_helpers.RemoveLineBreaks(headerTrunc) print(" %-30s %s" % (headerType, headerDisplay,)) pass continue if 0 : # note that if you want to get text content (body) and the email contains # multiple payloads (plaintext/ html), you must parse each message separately. # use something like the following: (taken from a stackoverflow post) def get_first_text_block(self, email_message_instance): maintype = email_message_instance.get_content_maintype() if maintype == 'multipart': for part in email_message_instance.get_payload(): if part.get_content_maintype() == 'text': return part.get_payload() elif maintype == 'text': return email_message_instance.get_payload() print()