| 1 |
IMAPClient Concepts |
| 2 |
------------------- |
| 3 |
|
| 4 |
Message Identifiers |
| 5 |
~~~~~~~~~~~~~~~~~~~ |
| 6 |
In the IMAP protocol, messages are identified using an integer. These |
| 7 |
message ids are specific to a given folder. |
| 8 |
|
| 9 |
There are two types of message identifiers in the IMAP protocol. |
| 10 |
|
| 11 |
One type is the message sequence number where the messages in a folder |
| 12 |
are numbered from 1 to N where N is the number of messages in the |
| 13 |
folder. These numbers don't persist between sessions and may be |
| 14 |
reassigned after some operations such as an expunge. |
| 15 |
|
| 16 |
A more convenient approach is Unique Identifiers (UIDs). Unique |
| 17 |
Identifiers are integers assigned to each message by the IMAP server |
| 18 |
that will persist across sessions. They do not change when folders are |
| 19 |
expunged. Almost all IMAP servers support UIDs. |
| 20 |
|
| 21 |
Each call to the IMAP server can use either message sequence numbers |
| 22 |
or UIDs in the command arguments and return values. The client |
| 23 |
specifies to the server which type of identifier should be used. You |
| 24 |
can set whether IMAPClient should use UIDs or message sequence number |
| 25 |
via the *use_uid* argument passed when an IMAPClient instance is |
| 26 |
created and the *use_uid* attribute. The *use_uid* attribute can be |
| 27 |
used to change the message id type between calls to the |
| 28 |
server. IMAPClient uses UIDs by default. |
| 29 |
|
| 30 |
Any method that accepts message ids takes either a sequence containing |
| 31 |
message ids (eg. ``[1,2,3]``), or a single message id integer, or a |
| 32 |
string representing sets and ranges of messages as supported by the |
| 33 |
IMAP protocol (e.g. ``'50-65'``, ``'2:*'`` or ``'2,4:7,9,12:*'``). |
| 34 |
|
| 35 |
Message Flags |
| 36 |
~~~~~~~~~~~~~ |
| 37 |
An IMAP server keeps zero or more flags for each message. These |
| 38 |
indicate certain properties of the message or can be used by IMAP |
| 39 |
clients to keep track of data related to a message. |
| 40 |
|
| 41 |
The IMAPClient package has constants for a number of commmonly used flags:: |
| 42 |
|
| 43 |
DELETED = br'\Deleted' |
| 44 |
SEEN = br'\Seen' |
| 45 |
ANSWERED = br'\Answered' |
| 46 |
FLAGGED = br'\Flagged' |
| 47 |
DRAFT = br'\Draft' |
| 48 |
RECENT = br'\Recent' # This flag is read-only |
| 49 |
|
| 50 |
Any method that accepts message flags takes either a sequence |
| 51 |
containing message flags (eg. ``[DELETED, 'foo', 'Bar']``) or a single |
| 52 |
message flag (eg. ``'Foo'``). |
| 53 |
|
| 54 |
Folder Name Encoding |
| 55 |
~~~~~~~~~~~~~~~~~~~~ |
| 56 |
Any method that takes a folder name will accept a standard string or a |
| 57 |
unicode string. Unicode strings will be transparently encoded using |
| 58 |
modified UTF-7 as specified by :rfc:`3501#section-5.1.3`. This allows |
| 59 |
for arbitrary unicode characters (eg. non-English characters) to be |
| 60 |
used in folder names. |
| 61 |
|
| 62 |
The ampersand character ("&") has special meaning in IMAP folder |
| 63 |
names. IMAPClient automatically escapes and unescapes this character |
| 64 |
so that the caller doesn't have to. |
| 65 |
|
| 66 |
Automatic folder name encoding and decoding can be enabled or disabled |
| 67 |
with the *folder_encode* attribute. It defaults to True. |
| 68 |
|
| 69 |
If *folder_encode* is True, all folder names returned by IMAPClient |
| 70 |
are always returned as unicode strings. If *folder_encode* is False, |
| 71 |
folder names are returned as str (Python 2) or bytes (Python 3). |
| 72 |
|
| 73 |
Working With Fetched Messages |
| 74 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 75 |
The IMAP protocol gives access to a limited amount of information about emails |
| 76 |
stored on the server. In depth analysis of a message usually requires |
| 77 |
downloading the full message and parsing its content. |
| 78 |
|
| 79 |
The `email <https://docs.python.org/3/library/email.html>`_ package of the |
| 80 |
Python standard library provides a reliable way to transform a raw email into |
| 81 |
a convenient object. |
| 82 |
|
| 83 |
.. literalinclude:: ../../examples/email_parsing.py |
| 84 |
|
| 85 |
TLS/SSL |
| 86 |
~~~~~~~ |
| 87 |
IMAPClient uses sensible TLS parameter defaults for encrypted |
| 88 |
connections and also allows for a high level of control of TLS |
| 89 |
parameters if required. It uses the built-in `ssl` package, |
| 90 |
provided since Python 2.7.9 and 3.4. |
| 91 |
|
| 92 |
TLS parameters are controlled by passing a ``ssl.SSLContext`` when |
| 93 |
creating an IMAPClient instance (or to the `starttls` method when the |
| 94 |
STARTTLS is used). When ``ssl=True`` is used without passing a |
| 95 |
SSLContext, a default context is used. The default context avoids the |
| 96 |
use of known insecure ciphers and SSL protocol versions, with |
| 97 |
certificate verification and hostname verification turned on. The |
| 98 |
default context will use system installed certificate authority trust |
| 99 |
chains, if available. |
| 100 |
|
| 101 |
When constructing a custom context it is usually best to start with |
| 102 |
the default context, created by the ``ssl`` module, and modify it to |
| 103 |
suit your needs. |
| 104 |
|
| 105 |
The following example shows how to to disable certification |
| 106 |
verification and certificate host name checks if required. |
| 107 |
|
| 108 |
.. literalinclude:: ../../examples/tls_no_checks.py |
| 109 |
|
| 110 |
The next example shows how to create a context that will use custom CA |
| 111 |
certificate. This is required to perform verification of a self-signed |
| 112 |
certificate used by the IMAP server. |
| 113 |
|
| 114 |
.. literalinclude:: ../../examples/tls_cacert.py |
| 115 |
|
| 116 |
If your operating system comes with an outdated list of CA certificates you can |
| 117 |
use the `certifi <https://pypi.python.org/pypi/certifi>`_ package that provides |
| 118 |
an up-to-date set of trusted CAs:: |
| 119 |
|
| 120 |
import certifi |
| 121 |
|
| 122 |
ssl_context = ssl.create_default_context(cafile=certifi.where()) |
| 123 |
|
| 124 |
If the server supports it, you can also authenticate using a client |
| 125 |
certificate:: |
| 126 |
|
| 127 |
import ssl |
| 128 |
|
| 129 |
ssl_context = ssl.create_default_context() |
| 130 |
ssl_context.load_cert_chain("/path/to/client_certificate.crt") |
| 131 |
|
| 132 |
The above examples show some of the most common TLS parameter |
| 133 |
customisations but there are many other tweaks are possible. Consult |
| 134 |
the Python 3 :py:mod:`ssl` package documentation for further options. |
| 135 |
|
| 136 |
Logging |
| 137 |
~~~~~~~ |
| 138 |
IMAPClient logs debug lines using the standard Python :py:mod:`logging` |
| 139 |
module. Its logger prefix is ``imapclient.``. |
| 140 |
|
| 141 |
One way to see debug messages from IMAPClient is to set up logging |
| 142 |
like this:: |
| 143 |
|
| 144 |
import logging |
| 145 |
|
| 146 |
logging.basicConfig( |
| 147 |
format='%(asctime)s - %(levelname)s: %(message)s', |
| 148 |
level=logging.DEBUG |
| 149 |
) |
| 150 |
|
| 151 |
For advanced usage, please refer to the documentation ``logging`` |
| 152 |
module. |