Permalink
Browse files

Document the recommended way of parsing emails

Fixes #340
  • Loading branch information...
NicolasLM committed Aug 30, 2018
1 parent aa9dfaf commit 1a61ffbc904015ef41ff9f0c62c5bde195e0dac9
Showing with 29 additions and 0 deletions.
  1. +12 −0 doc/src/concepts.rst
  2. +17 −0 examples/email_parsing.py
View
@@ -70,6 +70,18 @@ If *folder_encode* is True, all folder names returned by IMAPClient
are always returned as unicode strings. If *folder_encode* is False,
folder names are returned as str (Python 2) or bytes (Python 3).
Working With Fetched Messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The IMAP protocol gives access to a limited amount of information about emails
stored on the server. In depth analysis of a message usually requires
downloading the full message and parsing its content.
The `email <https://docs.python.org/3/library/email.html>`_ package of the
Python standard library provides a reliable way to transform a raw email into
a convenient object.
.. literalinclude:: ../../examples/email_parsing.py
TLS/SSL
~~~~~~~
IMAPClient uses sensible TLS parameter defaults for encrypted
View
@@ -0,0 +1,17 @@
# Download unread emails and parse them into standard EmailMessage objects
import email
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
with IMAPClient(HOST) as server:
server.login(USERNAME, PASSWORD)
server.select_folder('INBOX', readonly=True)
messages = server.search('UNSEEN')
for uid, message_data in server.fetch(messages, 'RFC822').items():
email_message = email.message_from_bytes(message_data[b'RFC822'])
print(uid, email_message.get('From'), email_message.get('Subject'))

0 comments on commit 1a61ffb

Please sign in to comment.