core.utils.fetchers package¶
core.utils.fetchers package containing the email fetcher classes for Eonvelope project.
- class core.utils.fetchers.BaseFetcher(account)[source]¶
Bases:
ABCTemplate class for the mailfetcher classes.
Provides arg-checking for methods.
- Parameters:
account (Account)
- PROTOCOL = ''¶
Name of the used protocol, should be one of
MailFetchingProtocols.
- AVAILABLE_FETCHING_CRITERIA: tuple[str, ...] = ('',)¶
Tuple of all criteria available for fetching. Should refer to
MailFetchingCriteria. Must be immutable!
- DEFAULT_FETCHING_CRITERION = <core.utils.FetchingCriterion.FetchingCriterion object>¶
Default criterion to use for fetching emails with the fetcher class.
- abstractmethod __init__(account)[source]¶
Constructor basis, sets up the instance logger.
- Parameters:
account (
Account) – The model of the account to fetch from.- Return type:
None
- abstractmethod test(mailbox=None)[source]¶
Tests the connection to the mailaccount and, if given, the mailbox.
- Parameters:
mailbox (
Mailbox|None) – The mailbox to be tested. Default is None.- Raises:
ValueError – If the mailbox argument does not belong to
self.account.- Return type:
- abstractmethod fetch_emails(mailbox, criterion=<core.utils.FetchingCriterion.FetchingCriterion object>)[source]¶
Fetches emails based on a criterion from the server.
- Parameters:
mailbox (
Mailbox) – The model of the mailbox to fetch data from.criterion (
FetchingCriterion) – Formatted criterion to filter mails by. Defaults tocore.constants.EmailFetchingCriterionChoices.ALL.
- Yields:
Mails in the mailbox matching the criterion as
bytes.- Raises:
ValueError – If the
fetching_criterionis not available for this fetcher.- Return type:
- abstractmethod restore(email)[source]¶
Restores an email to a mailbox.
- Parameters:
email (
Email) – The email to restore.- Raises:
ValueError – If the emails mailbox is not in this fetchers account.
FileNotFoundError – If the emails file_path is not set.
- Return type:
- __str__()[source]¶
Returns a string representation of the
BaseFetcherinstances.- Return type:
- Returns:
The string representation of the fetcher instance.
- class core.utils.fetchers.ExchangeFetcher(account)[source]¶
Bases:
BaseFetcherMaintains 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:
- 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
mailboxdoes not belong toself.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:
- 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 toeonvelope.MailFetchingCriteria.ALL.
- Yields:
Mails in the mailbox matching the criterion as
bytes.- Raises:
ValueError – If the
mailboxdoes not belong toself.account. Ifcriterionis not inExchangeFetcher.AVAILABLE_FETCHING_CRITERIA.MailboxError – If an error occurs or a bad response is returned during an action on the mailbox..
- Return type:
- 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:
- 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:
- class core.utils.fetchers.IMAP4Fetcher(account)[source]¶
Bases:
BaseFetcher,SafeIMAPMixinMaintains a connection to the IMAP server and fetches data using
imaplib.Opens a connection to the IMAP server on construction and is preferably used in a ‘with’ environment. Allows fetching of mails and mailboxes from an account on an IMAP host.
- Parameters:
account (Account)
- PROTOCOL = 'IMAP'¶
Name of the used protocol, refers to
MailFetchingProtocols.IMAP.
- AVAILABLE_FETCHING_CRITERIA: tuple[str, ...] = (EmailFetchingCriterionChoices.ALL, EmailFetchingCriterionChoices.UNSEEN, EmailFetchingCriterionChoices.SEEN, EmailFetchingCriterionChoices.RECENT, EmailFetchingCriterionChoices.NEW, EmailFetchingCriterionChoices.OLD, EmailFetchingCriterionChoices.FLAGGED, EmailFetchingCriterionChoices.UNFLAGGED, EmailFetchingCriterionChoices.DRAFT, EmailFetchingCriterionChoices.UNDRAFT, EmailFetchingCriterionChoices.ANSWERED, EmailFetchingCriterionChoices.UNANSWERED, EmailFetchingCriterionChoices.DELETED, EmailFetchingCriterionChoices.UNDELETED, EmailFetchingCriterionChoices.DAILY, EmailFetchingCriterionChoices.WEEKLY, EmailFetchingCriterionChoices.MONTHLY, EmailFetchingCriterionChoices.ANNUALLY, EmailFetchingCriterionChoices.SENTSINCE, EmailFetchingCriterionChoices.SUBJECT, EmailFetchingCriterionChoices.BODY, EmailFetchingCriterionChoices.FROM, EmailFetchingCriterionChoices.KEYWORD, EmailFetchingCriterionChoices.UNKEYWORD, EmailFetchingCriterionChoices.LARGER, EmailFetchingCriterionChoices.SMALLER)¶
Tuple of all criteria available for fetching. Refers to
MailFetchingCriteria. Must be immutable! IMAP4 does not accept time lookups, only date based. For a list of all existing IMAP criteria see https://datatracker.ietf.org/doc/html/rfc3501.html#section-6.4.4.
- EMAIL_FETCH_BATCH_SIZE = 100¶
- __init__(account)[source]¶
Constructor, starts the IMAP 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 IMAP server using the credentials from
account.- Raises:
MailAccountError – If an error occurs or a bad response is returned.
- Return type:
- 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
mailboxdoes not belong toself.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:
- fetch_emails(mailbox, criterion=<core.utils.FetchingCriterion.FetchingCriterion object>)[source]¶
Fetches and returns maildata from a mailbox based on a given criterion.
- Parameters:
mailbox (
Mailbox) – Database model of the mailbox to fetch data from.criterion (
FetchingCriterion) – Formatted criterion to filter mails in the IMAP request. Defaults toeonvelope.MailFetchingCriteria.ALL.
- Yields:
Mails in the mailbox matching the criterion as
bytes.- Raises:
ValueError – If the
mailboxdoes not belong toself.account. Ifcriterionis not inIMAP4Fetcher.AVAILABLE_FETCHING_CRITERIA.MailboxError – If an error occurs or a bad response is returned during an action on the mailbox.
- Return type:
- fetch_mailboxes()[source]¶
Retrieves and returns the data of the mailboxes in the account.
Todo
Rewrite this into a generator.
- Return type:
- Returns:
List of data of all mailboxes in the account. 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:
- class core.utils.fetchers.IMAP4_SSL_Fetcher(account)[source]¶
Bases:
IMAP4FetcherSubclass of
core.utils.fetchers.IMAP4Fetcher.Does the same things, just using IMAP4_SSL protocol.
- Parameters:
account (Account)
- PROTOCOL = 'IMAP4_SSL'¶
Name of the used protocol, refers to
constants.MailFetchingProtocols.IMAP4_SSL.
- connect_to_host()[source]¶
Overrides
core.utils.fetchers.IMAP4Fetcher.connect_to_host()to useimaplib.IMAP4_SSL.Important
Using ssl_context is urgently required, see https://www.pentagrid.ch/en/blog/python-mail-libraries-certificate-verification/ .
- Return type:
- class core.utils.fetchers.JMAPFetcher(account)[source]¶
Bases:
BaseFetcherMaintains a connection to the JMAP server and fetches data using
jmapc.Opens a connection to the JMAP server on construction and is preferably used in a ‘with’ environment. Allows fetching of mails and mailboxes from an account on an JMAP host.
- Parameters:
account (Account)
- PROTOCOL = 'JMAP'¶
Name of the used protocol, refers to
MailFetchingProtocols.JMAP.
- AVAILABLE_FETCHING_CRITERIA: tuple[str, ...] = (EmailFetchingCriterionChoices.ALL, EmailFetchingCriterionChoices.SEEN, EmailFetchingCriterionChoices.UNSEEN, EmailFetchingCriterionChoices.DRAFT, EmailFetchingCriterionChoices.UNDRAFT, EmailFetchingCriterionChoices.ANSWERED, EmailFetchingCriterionChoices.UNANSWERED, EmailFetchingCriterionChoices.DAILY, EmailFetchingCriterionChoices.WEEKLY, EmailFetchingCriterionChoices.MONTHLY, EmailFetchingCriterionChoices.ANNUALLY, EmailFetchingCriterionChoices.BODY, EmailFetchingCriterionChoices.FROM, EmailFetchingCriterionChoices.SENTSINCE, EmailFetchingCriterionChoices.LARGER, EmailFetchingCriterionChoices.SMALLER)¶
Tuple of all criteria available for fetching. Refers to
MailFetchingCriteria. Must be immutable!
- __init__(account)[source]¶
Constructor basis, sets up the instance logger.
- Parameters:
account (
Account) – The model of the account to fetch from.- Return type:
None
- test(mailbox=None)[source]¶
Tests the connection to the mailaccount and, if given, the mailbox.
- Parameters:
mailbox (
Mailbox|None) – The mailbox to be tested. Default is None.- Raises:
ValueError – If the mailbox argument does not belong to
self.account.- Return type:
- fetch_emails(mailbox, criterion=<core.utils.FetchingCriterion.FetchingCriterion object>)[source]¶
Fetches emails based on a criterion from the server.
- Parameters:
mailbox (
Mailbox) – The model of the mailbox to fetch data from.criterion (
FetchingCriterion) – Formatted criterion to filter mails by. Defaults tocore.constants.EmailFetchingCriterionChoices.ALL.
- Yields:
Mails in the mailbox matching the criterion as
bytes.- Raises:
ValueError – If the
fetching_criterionis not available for this fetcher.- Return type:
- restore(email)[source]¶
Restores an email to a mailbox.
- Parameters:
email (
Email) – The email to restore.- Raises:
ValueError – If the emails mailbox is not in this fetchers account.
FileNotFoundError – If the emails file_path is not set.
- Return type:
- class core.utils.fetchers.POP3Fetcher(account)[source]¶
Bases:
BaseFetcher,SafePOPMixinMaintains a connection to the POP server and fetches data using
poplib.Opens a connection to the POP server on construction and is preferably used in a ‘with’ environment. Allows fetching of mails and mailboxes from an account on an POP host.
Since POP does not have any mailboxes, none of the methods should raise a MailboxError.
- Parameters:
account (Account)
- PROTOCOL = 'POP3'¶
Name of the used protocol, refers to
MailFetchingProtocols.POP3.
- AVAILABLE_FETCHING_CRITERIA: tuple[str, ...] = (EmailFetchingCriterionChoices.ALL,)¶
Tuple of all criteria available for fetching. Refers to
MailFetchingCriteria. Must be immutable!
- __init__(account)[source]¶
Constructor, starts the POP 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 POP server using the credentials from
account.- Raises:
MailAccountError – If an error occurs or a bad response is returned.
- Return type:
- test(mailbox=None)[source]¶
Tests the connection to the mailserver and, if a mailbox is provided, whether messages can be listed.
- Parameters:
mailbox (
Mailbox|None) – The mailbox to be tested. Default is None.- Raises:
ValueError – If the
mailboxdoes not belong toself.account.MailAccountError – If the test fails because an error occurs or a bad response is returned.
- Return type:
- fetch_emails(mailbox, criterion=<core.utils.FetchingCriterion.FetchingCriterion object>)[source]¶
Fetches and returns all maildata from the server.
- Parameters:
mailbox (
Mailbox) – Database model of the mailbox to fetch data from.criterion (
FetchingCriterion) – POP only supports ALL lookups. Defaults toeonvelope.MailFetchingCriteria.ALL. This arg ensures compatibility with the other fetchers.
- Yields:
Mails in the mailbox.
- Raises:
ValueError – If the
mailboxdoes not belong toself.account. Ifcriterionis noteonvelope.MailFetchingCriteria.ALL.MailAccountError – If an error occurs or a bad response is returned.
- Return type:
- fetch_mailboxes()[source]¶
Returns the data of the mailboxes. For POP3 there is only one mailbox named ‘INBOX’.
Note
This method is built to match the fetcherclasses interface.
- restore(email)[source]¶
Places an email in its mailbox.
Note
POP doesn’t offer an action to upload emails.
- Parameters:
email (
Email) – The email to restore.- Raises:
NotImplementedError – POP can’t restore emails.
- Return type:
- class core.utils.fetchers.POP3_SSL_Fetcher(account)[source]¶
Bases:
POP3FetcherSubclass of
core.utils.fetchers.POP3Fetcher.Does the same things, just using POP3_SSL protocol.
- Parameters:
account (Account)
- PROTOCOL = 'POP3_SSL'¶
Name of the used protocol, refers to
constants.MailFetchingProtocols.POP3_SSL.
- connect_to_host()[source]¶
Overrides
core.utils.fetchers.POP3Fetcher.connect_to_host()to usepoplib.POP3_SSL.Important
Using ssl_context is urgently required, see https://www.pentagrid.ch/en/blog/python-mail-libraries-certificate-verification/ .
- Return type:
Submodules¶
- core.utils.fetchers.BaseFetcher module
- core.utils.fetchers.ExchangeFetcher module
- core.utils.fetchers.IMAP4Fetcher module
- core.utils.fetchers.IMAP4_SSL_Fetcher module
- core.utils.fetchers.JMAPFetcher module
- core.utils.fetchers.POP3Fetcher module
- core.utils.fetchers.POP3_SSL_Fetcher module
- core.utils.fetchers.SafeIMAPMixin module
- core.utils.fetchers.SafePOPMixin module
- core.utils.fetchers.exceptions module