Skip to main content

Effective working with email messages using IMAP protocol.

Project description

Effective working with email using IMAP protocol.

Python version

3.3+

License

MIT

PyPI

https://pypi.python.org/pypi/imap_tools/

IMAP

VERSION 4rev1 - https://tools.ietf.org/html/rfc3501

Features

  • transparent work with letter attributes

  • work with letters in directories (copy, delete, flag, move, seen)

  • work with directories (list, set, get, create, exists, rename, delete, status)

  • no dependencies

Installation

$ pip install imap_tools

Quick guide

Base:

from imap_tools import MailBox

mailbox = MailBox('imap.mail.com')
mailbox.login('test@mail.com', 'password')
subjects = [msg.subject for msg in mailbox.fetch()]
mailbox.logout()
# OR
with MailBox('imap.mail.com').login('test@mail.com', 'password') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]

MailBox.fetch - email message generator, params:

  • search_criteria: message search criteria, examples

  • limit: limit on the number of read emails, useful for actions with a large number of messages, like “move”

  • miss_defect: miss emails with defects

  • miss_no_uid: miss emails without uid

  • mark_seen: mark emails as seen on fetch

Message attributes:

# NOTE: All message properties are cached by functools.lru_cache

for message in mailbox.fetch():
    message.id
    message.uid
    message.subject
    message.from_
    message.to
    message.cc
    message.bcc
    message.date
    message.text
    message.html
    message.flags
    message.from_values
    message.to_values
    message.cc_values
    message.bcc_values
    for filename, payload in message.attachments:
        filename, payload
    # Any message attribute: message.obj['Message-ID'], message.obj['X-Google-Smtp-Source'] ...

Actions with messages in folder:

# NOTE: You can use 2 approaches to perform these operations
# "by one" - Perform operation for each message separately per N commands
# "in bulk" - Perform operation for message set per 1 command

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', initial_folder='INBOX') as mailbox:

    # COPY all messages from current dir to folder1, *by one
    for msg in mailbox.fetch():
        res = mailbox.copy(msg.uid, 'INBOX/folder1')

    # DELETE all messages from current dir, *in bulk
    mailbox.delete([msg.uid for msg in mailbox.fetch()])

    # FLAG unseen messages in current folder as Answered and Flagged, *in bulk.
    flags = (imap_tools.StandardMessageFlags.ANSWERED, imap_tools.StandardMessageFlags.FLAGGED)
    mailbox.flag(mailbox.fetch('(UNSEEN)'), flags, True)

    # MOVE all messages from current dir to folder2, *in bulk
    mailbox.move(mailbox.fetch(), 'INBOX/folder2')

    # SEEN: mark all messages sent at 05.03.2007 in current folder as unseen, *in bulk
    mailbox.seen(mailbox.fetch("SENTON 05-Mar-2007"), False)

Actions with folders:

mailbox.login('test@mail.com', 'pwd')

# LIST
for folder in mailbox.folder.list('INBOX'):
    print(folder['flags'], folder['delim'], folder['name'])
# SET
mailbox.folder.set('INBOX')
# GET
current_folder = mailbox.folder.get()
# CREATE
mailbox.folder.create('folder1')
# EXISTS
is_exists = mailbox.folder.exists('folder1')
# RENAME
mailbox.folder.rename('folder1', 'folder2')
# DELETE
mailbox.folder.delete('folder2')
# STATUS
for status_key, status_val in mailbox.folder.status('some_folder').items():
    print(status_key, status_val)

mailbox.logout()

Reasons

There are many different libraries for working with e-mail via the imap protocol. Including imaplib library. However, these libraries contain various shortcomings, such as:

  • excessive low level

  • returned results are not ready to work with them

  • no convenient tools for working with: directories, letters in directories

Release notes

release_notes.rst

Thanks to

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imap_tools-0.8.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

imap_tools-0.8.0-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file imap_tools-0.8.0.tar.gz.

File metadata

  • Download URL: imap_tools-0.8.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for imap_tools-0.8.0.tar.gz
Algorithm Hash digest
SHA256 d22df4a3b01bf62c46b05c3b3c1b89fa02e4d3fe6bd9c2f37b321e6889aa1978
MD5 c0bbd8732aa7ed932312ed522535aab4
BLAKE2b-256 a3225bd56c166f868e58e296b49e1f8365faae4a94f99af84156f2b1f4a48658

See more details on using hashes here.

File details

Details for the file imap_tools-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: imap_tools-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for imap_tools-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a45519c0a587065bacd0f204e77d35c1bc9fb6ac248d282503b0d81d3b715e62
MD5 2bba700d732e0728da9bf6e798adc916
BLAKE2b-256 161d329c9f756c15a3a1c151f58a3ab8e7bf09cf9ca505ce19e1260ba240df57

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page