This is a sister library for Red Mail, advanced email sender.

Why Red Box?

Reading emails SHOULD NOT look like this:

import imaplib

# Get message IDs
s = imaplib.IMAP4_SSL('localhost', port=0)
s.select("INBOX")
typ, data = s.search(None, '(ALL (SUBJECT "example 2") (UNSEEN))')
msg_ids = list(data[0].decode("UTF-8").split(" "))

# Get message contents
msgs = []
for msg_id in msg_ids:
    typ, data = s.fetch(str(msg_id), '(RFC822)')
    content = data[0][1].decide("UTF-8")
    msgs.append(content)

It should look like this:

from redbox import EmailBox

box = EmailBox(host="localhost", port=0)
msgs = box['INBOX'].search(subject="example 2", unseen=True)

Red Box has several features:

Furthermore, Red Box has custom message type that makes manipulating the messages easy:

# Get one email
msg = msgs[0]

# String representation of the message
print(msg.content)

# Email contents
print(msg.text_body)
print(msg.html_body)

# Email headers
print(msg.from_)
print(msg.to)
print(msg.date)

Here is a more complete example:

from redbox import EmailBox

box = EmailBox(host="localhost", port=0)
inbox = box['INBOX']

for msg in inbox.search(subject="example 2", unseen=True):

    # Process the message
    print(msg.subject)
    print(msg.text_body)

    # Set the message as read/seen
    msg.read()

Interested?

Install the package:

pip install redbox

and get started.

Indices and tables