Imaper

Version: 1.0.0

This package allows you to easily access any IMAP mailbox with just a few lines of code.

Bitbucket Repo: https://bitbucket.org/dhrrgn/imaper

from imaper import Imaper

mailbox = Imaper(
    hostname='imap.foo.com',
    username='foo@foo.com',
    password='password'
)

print "Messages ({0}/{1})".format(mailbox.unread_count(),
    mailbox.message_count())
print "=" * 80

# Imaper.messages() returns a generator, but I want a list
messages = list(mailbox.messages(unread=True))

for msg in messages:
    print "Subject: {0}".format(msg.subject)
    print "Body:\n{0}".format(msg.body['plain'][0])
    print "-" * 80

    # Mark it as read
    msg.mark_read()

# Delete the first message
messages[0].delete()

imaper Package

class imaper.Imaper(hostname, username, password, port=143, ssl=False, connect=True, folder='INBOX')

Connects to the IMAP server with the given information.

Parameters:
  • hostname – The IMAP server to connect to.
  • username – The username to login with.
  • password – The password to use.
  • port – Server port (default=143)
  • ssl – Use SSL? (default=False)
  • connect – Whether to connect immediately (default=True)
  • folder – Which folder to connect to (default=’INBOX’)
connect()

Opens the connection and logs in to the IMAP server.

message_count()

The total number of messages in the selected folder.

Returns:Number of messages.
messages(**criteria)

A Generator that iterates over the messages in the current folder.

Each message is yielded as a Message object.

Parameters:criteria – The search criteria to use (passed through to _build_criteria()).
read_count()

The number of messages in the selected folder that are marked as read.

Returns:Number of read messages.
select_folder(folder)

Selects the given folder on the IMAP connection. This is the folder which all future commands will be ran against.

Parameters:folder – The folder to select.
unread_count()

The number of unread messages in the selected folder.

Returns:Number of unread messages.
class imaper.Message(mb, **properties)

Bases: object

Builds the message.

Parameters:
  • mb – The Imaper instance that this message came from.
  • properites – The message properites.
add_flags(flags)

Adds the given flags to this message

Parameters:flags – Either a string containing a single flag or a list of flags.
delete()

Marks this message as deleted.

keys()

Gets all the property names as a list.

Returns:A list of the message property names.
mark_flagged()

Marks this message as flagged.

mark_read()

Marks this message as read.

mark_unread()

Marks this message as unread.

remove_flags(flags)

Removes the given flags to this message

Parameters:flags – Either a string containing a single flag or a list of flags.
undelete()

unnarks this message as deleted.

unmark_flagged()

Unmarks this message as flagged.

parser Module

This module is a modified version of the parser from Imbox located here: https://github.com/martinrusev/imbox

Modified: Dan Horrigan

The MIT License (MIT)

Copyright (c) 2013 Martin Rusev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

imaper.parser.parse_email(raw_email)