7.1.1.1.2. isbg.imaputils module

Imap utils module for isbg - IMAP Spam Begone.

isbg.imaputils.mail_content(mail)

Get the email message content.

Parameters:mail (email.message.Message) – The email message.
Returns:The contents, with headers, of the email message. It returns bytes.
Return type:bytes or str
Raises:email.errors.MessageError – if mail is neither bytes nor str.
isbg.imaputils.new_message(body)

Get a email.message from a body email.

Note: If there are problems encoding it, it will replace it to ascii.

Parameters:body (bytes or str) – The content, with or without headers, of a email message.
Returns:The object representing it.
Return type:email.message.Message
Raises:TypeError – If the content is empty.
isbg.imaputils.get_message(imap, uid, append_to=None, logger=None)

Get a message by uid and optionally append it to a list.

Parameters:
  • imap (IsbgImap4) – The imap helper object with the connection.
  • uid (int or str) – A integer value with the uid of a message to fetch from the imap connection.
  • append_to (list of int), optional) – The integer value of uid is appended to this list. Defaults to None.
  • logger (logging.Logger, optional) – When a error is raised fetching the mail a warning is written to this logger. Defaults to None.
Returns:

The message fetched from the imap connection.

Return type:

email.message.Message

isbg.imaputils.imapflags(flaglist)

Transform a list to a string as expected for the IMAP4 standard.

Example

>>> imapflags(['foo', 'boo')]
'(foo,boo)'
Parameters:flaglist (list of str) – The flag list to transform.
Returns:A string with the flag list.
Return type:str
isbg.imaputils.bytes_to_ascii(func)

Decorate a method to return his return value as ascii.

isbg.imaputils.assertok(name)

Decorate with assertok.

class isbg.imaputils.IsbgImap4(host='', port=143, nossl=False, assertok=None)

Bases: object

Proxy class for imaplib.IMAP4 and imaplib.IMAP4_SSL.

It calls to the IMAP4 or IMAP4_SSL methods but before it adds them decorators to log the calls and to try to convert the returns values to str.

The only original method is get_uidvalidity, used to return the current uidvalidity from a mailbox.

__init__(host='', port=143, nossl=False, assertok=None)

Create a imaplib.IMAP4[_SSL] with an assertok method.

append(*args, **kwargs)
capability(*args, **kwargs)
expunge(*args, **kwargs)
list(*args, **kwargs)
login(*args, **kwargs)
logout(*args, **kwargs)
status(*args, **kwargs)
select(*args, **kwargs)
uid(*args, **kwargs)
get_uidvalidity(mailbox)

Validate a mailbox.

Parameters:mailbox (str) – the mailbox to check for its uidvalidity.
Returns:The uidvalidity returned from the imap server. If it cannot be decoded, it returns 0.
Return type:int
isbg.imaputils.login_imap(imapsets, logger=None, assertok=None)

Login to the imap server.

class isbg.imaputils.ImapSettings

Bases: object

Class to store the imap and imap folders settings.

__init__()

Set Imap settings.

host = None

IMAP host name or IP.

port = None

IMAP port to connect.

user = None

IMAP user name.

passwd = None

Password for the IMAP user name.

nossl = None

Not use ssl for IMAP connection.

inbox = None

Inbox folder, default to `INBOX`.

spaminbox = None

Spam folder, default to `INBOX.Spam`.

learnspambox = None

Folder used to learn spam messages.

learnhambox = None

Folder used to learn non-spam messages.

hash

Get the hash property built from the host, user and port.

Type:str
static get_hash(host, user, port)

Get a hash with the host, user and port.

Parameters:
  • host (str) – IMAP host name.
  • user (str) – IMAP user name.
  • port (int) – IMAP connection port.
Returns:

The generated hash.

Return type:

str