7.1.1.1.2. isbg.imaputils module

Imap utils module for isbg - IMAP Spam Begone.

isbg.imaputils.mail_content(mail: Email) AnyStr

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: AnyStr) Email

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: IsbgImap4, uid: Uid, append_to: Optional[Uids] = None, logger: Optional[logging.Logger] = None) Email

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: List[str]) str

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

IMAP host name or IP.

port

IMAP port to connect.

user

IMAP user name.

passwd

Password for the IMAP user name.

nossl

Not use ssl for IMAP connection.

inbox

Inbox folder, default to `INBOX`.

spaminbox

Spam folder, default to `INBOX.Spam`.

learnspambox

Folder used to learn spam messages.

learnhambox

Folder used to learn non-spam messages.

property 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