X-GM-RAW search extension. Thanks to John Louis del Rosario for the
patch.
+ENVELOPE FETCH response parsing [NEW, API CHANGE]
+-------------------------------------------------
+ENVELOPE FETCH responses are now returned as Envelope instances. These
+objects are namedtuples providing convenient attribute and positional
+based access to envelope fields. The Date field is also now converted
+to a datetime instance.
+
+As part of this change various date and time related utilities were
+moved to a new module at imapclient.datetime_util.
+
SELECT response bug fix
-----------------------
-Handle square brackets in flags returned in SELECT
-response. Previously these would cause parsing errors. Thanks to
-Benjamin Morrise for the bug report.
+Handle square brackets in flags returned in SELECT response.
+Previously these would cause parsing errors. Thanks to Benjamin
+Morrise for the bug report.
========
0.10.2
These are aliases for the imaplib.IMAP4 exceptions of the same name. Socket
errors may also be raised in the case of network errors.
-IMAPClient Class Reference
---------------------------
+API Reference
+-------------
+
+IMAPClient Class
+~~~~~~~~~~~~~~~~
The primary class used by the imapclient package is the IMAPClient
class. All interaction with a remote IMAP server is performed via an
IMAPClient instance.
.. autoclass:: imapclient.IMAPClient
:members:
+Fetch Response Types
+~~~~~~~~~~~~~~~~~~~~
+Various types may be used in the data structures returned by
+:py:meth:`.IMAPClient.fetch` when certain response types
+are encountered during parsing.
+
+.. automodule:: imapclient.response_types
+ :members:
+
Interactive Sessions
--------------------
When developing program using IMAPClient is it sometimes useful to
A dictionary is returned, indexed by message number. Each item
in this dictionary is also a dictionary, with an entry
- corresponding to each item in *data*.
+ corresponding to each item in *data*. Returned values will be
+ appropriately typed. For example, integer values will be returned as
+ Python integers, timestamps will be returned as datetime
+ instances and ENVELOPE responses will be returned as
+ :py:class:`Envelope <imapclient.response_types.Envelope>` instances.
In addition to an element for each *data* item, the dict
returned for each message also contains a *SEQ* key containing
from datetime import datetime
from email.utils import make_msgid
+from .fixed_offset import FixedOffset
from .imapclient import IMAPClient, DELETED, to_unicode
+from .response_types import Envelope, Address
from .six import binary_type, text_type, PY3
from .test.util import unittest
from .config import parse_config_file, create_client_from_config
self.client.select_folder(self.base_folder)
self.append_msg(msg)
+ self.client.normalise_times = False
fields = ['RFC822', b'FLAGS', 'INTERNALDATE', 'ENVELOPE']
msg_id = self.client.search()[0]
self.assertMultiLineEqual(msginfo['RFC822'], msg)
self.assertIsInstance(msginfo['INTERNALDATE'], datetime)
self.assertIsInstance(msginfo['FLAGS'], tuple)
- self.assertTupleEqual(msginfo['ENVELOPE'],
- ('Tue, 16 Mar 2010 16:45:32 +0000',
- 'A multipart message',
- (('Bob Smith', None, 'bob', 'smith.com'),),
- (('Bob Smith', None, 'bob', 'smith.com'),),
- (('Bob Smith', None, 'bob', 'smith.com'),),
- (('Some One', None, 'some', 'one.com'), (None, None, 'foo', 'foo.com')),
- None, None, None, msg_id_header))
+
+ self.assertSequenceEqual(msginfo['ENVELOPE'],
+ Envelope(
+ datetime(2010, 3, 16, 16, 45, 32, tzinfo=FixedOffset(0)),
+ 'A multipart message',
+ (Address('Bob Smith', None, 'bob', 'smith.com'),),
+ (Address('Bob Smith', None, 'bob', 'smith.com'),),
+ (Address('Bob Smith', None, 'bob', 'smith.com'),),
+ (Address('Some One', None, 'some', 'one.com'),
+ Address(None, None, 'foo', 'foo.com')),
+ None, None, None, msg_id_header))
def test_partial_fetch(self):
self.client.append(self.base_folder, MULTIPART_MESSAGE)
import sys
from collections import defaultdict
from datetime import datetime
-from email.utils import parsedate_tz
from . import six
xrange = six.moves.xrange
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
-"""
-Structured representation for IMAP response.
-Inspired by Ruby Net::IMAP::Envelope and Net::IMAP::Address
-(see http://www.ruby-doc.org/stdlib-2.0.0/libdoc/net/imap/rdoc/Net/IMAP.html).
-"""
-
from collections import namedtuple
from email.utils import formataddr
-# the following hack to add docstrings to namedtuples is courtesy of:
-# http://stackoverflow.com/questions/1606436/adding-docstrings-to-namedtuples-in-python
-
-class Address(namedtuple("Address", "name route mailbox host")):
- """
- Represents electronic mail addresses.
-
- Fields (in-order):
-
- name -- Returns the phrase from [RFC-822] mailbox.
-
- route -- Returns the route from [RFC-822] route-addr.
-
- mailbox -- None indicates end of [RFC-822] group. If not None and host is None,
- returns [RFC-822] group name. Otherwise, returns [RFC-822] local-part.
-
- host -- None indicates [RFC-822] group syntax. Otherwise, returns [RFC-822] domain name.
-
- """
-
- def __str__(self):
- return formataddr((self.name, self.mailbox + '@' + self.host))
class Envelope(namedtuple("Envelope", "date subject from_ sender reply_to to " +
"cc bcc in_reply_to message_id")):
"""
+ Represents envelope structures of messages. Returned when parsing
+ ENVELOPE responses.
+
+ :ivar date: A datetime instance that represents the "Date" header.
+ :ivar subject: A string that contains the "Subject" header.
+ :ivar from\_: A tuple of Address objects that represent on or more
+ addresses from the "From" header, or None if header does not exist.
+ :ivar sender: As for from\_ but represents the "Sender" header.
+ :ivar reply_to: As for from\_ but represents the "Reply-To" header.
+ :ivar to: As for from\_ but represents the "To" header.
+ :ivar cc: As for from\_ but represents the "Cc" header.
+ :ivar bcc: As for from\_ but represents the "Bcc" recipients.
+ :ivar in_reply_to: A string that contains the "In-Reply-To" header.
+ :ivar message_id: A string that contains the "Message-Id" header.
+ """
- Represents envelope structures of messages.
-
- Fields (in-order):
-
- date -- Returns a string that represents the "Date:" header.
-
- subject -- Returns a string that represents the "Subject:" header.
-
- from_ -- Returns a tuple sequence of Address structure that represents "From:" header,
- or None if header does not exist.
-
- sender -- Returns a tuple sequence of Address structure that represents "Sender:" header,
- or None if header does not exist.
- reply_to -- Returns a tuple sequence of Address structure that represents "Reply_To:" header,
- or None if header does not exist.
+class Address(namedtuple("Address", "name route mailbox host")):
+ """
+ Represents electronic mail addresses. Used to store addresses in
+ :py:class:`Envelope`.
- to -- Returns a tuple sequence of Address structure that represents "To:" header,
- or None if header does not exist.
+ :ivar name: The address "personal name".
+ :ivar route: SMTP source route (rarely used).
+ :ivar mailbox: Mailbox name (what comes just before the @ sign).
+ :ivar host: The host/domain name.
- cc -- Returns a tuple sequence of Address structure that represents "Cc:" header,
- or None if header does not exist.
+ As an example, an address header that looks like::
- bcc -- Returns a tuple sequence of Address structure that represents "Bcc:" header,
- or None if header does not exist.
+ Mary Smith <mary@foo.com>
- in_reply_to -- Returns a string that represents the "In-Reply-To:" header.
+ would be represented as::
- message_id -- Returns a string that represents the "Message-Id:" header.
+ Address(name=u'Mary Smith', route=None, mailbox=u'mary', host=u'foo.com')
+ See :rfc:`2822` for more.
"""
+
+ def __str__(self):
+ return formataddr((self.name, self.mailbox + '@' + self.host))
'(("name" NIL "address1" "domain1.com")) '
'((NIL NIL "address2" "domain2.com")) '
'(("name" NIL "address3" "domain3.com")) '
+ 'NIL'
'((NIL NIL "address4" "domain4.com") '
'("person" NIL "address4b" "domain4b.com")) '
- 'NIL NIL "<reply-to-id>" "<msg_id>"))')
+ 'NIL "<reply-to-id>" "<msg_id>"))')
output = parse_fetch_response([envelope_str], normalise_times=False)
(Address("name", None, "address1", "domain1.com"),),
(Address(None, None, "address2", "domain2.com"),),
(Address("name", None, "address3", "domain3.com"),),
+ None,
(Address(None, None, "address4", "domain4.com"),
Address("person", None, "address4b", "domain4b.com")),
- None, None, "<reply-to-id>", "<msg_id>"
+ None, "<reply-to-id>", "<msg_id>"
)
)