core.utils.fetchers.ExchangeFetcher module

Module with the ExchangeFetcher class.

class core.utils.fetchers.ExchangeFetcher.ExchangeFetcher(account)[source]

Bases: BaseFetcher

Maintains a connection to the Exchange server and fetches data using imaplib.

Opens a connection to the Exchange server on construction and is preferably used in a ‘with’ environment. Allows fetching of mails and mailboxes from an account on an Exchange host.

Parameters:

account (Account)

PROTOCOL = 'EXCHANGE'

Name of the used protocol, refers to MailFetchingProtocols.Exchange.

AVAILABLE_FETCHING_CRITERIA: tuple[str, ...] = (EmailFetchingCriterionChoices.ALL, EmailFetchingCriterionChoices.SEEN, EmailFetchingCriterionChoices.UNSEEN, EmailFetchingCriterionChoices.DRAFT, EmailFetchingCriterionChoices.UNDRAFT, EmailFetchingCriterionChoices.DAILY, EmailFetchingCriterionChoices.WEEKLY, EmailFetchingCriterionChoices.MONTHLY, EmailFetchingCriterionChoices.ANNUALLY, EmailFetchingCriterionChoices.SUBJECT, EmailFetchingCriterionChoices.BODY)

Tuple of all criteria available for fetching. Refers to EmailFetchingCriterionChoices. Constructed analogous to the IMAP4 criteria. Must be immutable!

EMAIL_FETCH_BATCH_SIZE = 20
__init__(account)[source]

Constructor, starts the Exchange connection and logs into the account.

Parameters:

account (Account) – The model of the account to be fetched from.

Return type:

None

connect_to_host()[source]

Opens the connection to the Exchange server using the credentials from account.

Raises:

MailAccountError – If an error occurs accessing the msg_folder_root.

Return type:

None

test(mailbox=None)[source]

Tests the connection to the mailserver and, if a mailbox is provided, whether it can be opened and listed.

Parameters:

mailbox (Mailbox | None) – The mailbox to be tested. Default is None.

Raises:
  • ValueError – If the mailbox does not belong to self.account.

  • MailAccountError – If the account test fails because an error occurs or a bad response is returned.

  • MailboxError – If the mailbox test fails because an error occurs or a bad response is returned testing the mailbox.

Return type:

None

fetch_emails(mailbox, criterion=<core.utils.FetchingCriterion.FetchingCriterion object>)[source]

Fetches and returns maildata from a mailbox based on a given criterion.

Todo

Rewrite this into a generator.

Parameters:
  • mailbox (Mailbox) – Database model of the mailbox to fetch data from.

  • criterion (FetchingCriterion) – Formatted criterion to filter mails in the Exchange server. Defaults to eonvelope.MailFetchingCriteria.ALL.

Yields:

Mails in the mailbox matching the criterion as bytes.

Raises:
Return type:

Generator[bytes]

fetch_mailboxes()[source]

Retrieves and returns the data of the mailboxes in the account.

Todo

Rewrite this into a generator.

Note

Considers only children of the msg_folder_root.

Return type:

list[tuple[str, str]]

Returns:

List of paths of all mailboxes in the account relative to the parent folder of the inbox. Empty if none are found.

Raises:

MailAccountError – If an error occurs or a bad response is returned.

restore(email)[source]

Places an email in its mailbox.

Parameters:

email (Email) – The email to restore.

Raises:
  • ValueError – If the emails mailbox is not in this fetchers account.

  • FileNotFoundError – If the email has no eml file in storage.

  • MailboxError – If uploading the email to the mailserver fails or returns a bad response.

Return type:

None

close()[source]

No cleanup of exchangelib.Account required.

Return type:

None

open_mailbox(mailbox)[source]

Helper method to correctly open a mailbox folder.

Note

This may cause problems with mailboxes that have / in their name (should be very uncommon).

Return type:

Folder

Returns:

The mailbox folder instance.

Parameters:

mailbox (Mailbox)