core.utils.mail_parsing module

Provides functions for parsing features from the maildata.

Global variables:

logger (logging.Logger): The logger for this module.

core.utils.mail_parsing.decode_header(header)[source]

Decodes an email header field.

Parameters:

header (Header | str) – The mail header to decode.

Return type:

str

Returns:

The decoded mail header.

core.utils.mail_parsing.get_header(email_message, header_name, joining_string=',')[source]

Shorthand to safely get a header from a email.message.EmailMessage.

Strips trailing whitespace from the header.

Parameters:
  • email_message (EmailMessage) – The message to get the header from.

  • header_name (str) – The name of the header field.

  • joining_string (str) – The string to join multiple headers with. Default to ‘,’ which is safe for CharFields.

Return type:

str

Returns:

The decoded header field as a string if found else “”.

core.utils.mail_parsing.parse_datetime_header(date_header)[source]

Parses the date header into a datetime object.

If an error occurs uses the current time as fallback.

Parameters:

date_header (str | None) – The datetime header to parse.

Return type:

datetime

Returns:

The datetime version of the header. The current time in case of an invalid date header.

core.utils.mail_parsing.get_bodytexts(email_message)[source]

Parses the various bodytexts from a email.message.EmailMessage.

Parameters:

email_message (EmailMessage) – The message to parse the bodytexts from.

Return type:

dict[str, str]

Returns:

A dict containing the bodytexts with their contenttypes as keys.

core.utils.mail_parsing.parse_IMAP_mailbox_data(mailbox_data)[source]

Parses the mailbox name as received in core.utils.fetchers.IMAP4Fetcher.

Note

Uses imap_tools.imap_utf7.utf7_decode() to decode IMAPs modified utf7 encoding. The result must not be changed afterwards, otherwise opening the mailbox via this name is not possible!

Parameters:

mailbox_data (bytes | str) – The mailbox data in bytes or as str as received from the mail server.

Return type:

tuple[str, str]

Returns:

The serverside name of the mailbox.

core.utils.mail_parsing.parse_mailbox_type(mailbox_type_name)[source]

Maps the mailbox type names from the various protocols to the choice enum.

Return type:

str

Parameters:

mailbox_type_name (str)

core.utils.mail_parsing.find_best_href_in_header(header)[source]

Finds the best href in a header of hrefs.

The href is selected via the logic: https > http > …

Return type:

str

Returns:

The best hyperref. The empty string if header is empty.

Parameters:

header (str)

core.utils.mail_parsing.is_x_spam(x_spam_header)[source]

Evaluates a x_spam header spam marker.

Parameters:

x_spam_header (str | None) – The X-Spam-Flag header to evaluate.

Return type:

bool | None

Returns:

Whether the header marks its mail as spam. None if there is no header and thus no statement about the spam property.

core.utils.mail_parsing.make_icalendar_readout(icalendar_file)[source]

Parses the main features of a icalendar file into a list.

References

https://www.rfc-editor.org/rfc/rfc5545.html#page-52

Parameters:

icalendar_file (File | TextIO) – The icalendar file to parse.

Return type:

list[tuple[datetime, datetime, str, str]]

Returns:

A list with the calendar events main features in tuples.

core.utils.mail_parsing.make_vcard_readout(vcard_file)[source]

Parses the main features of a vcard file into a list.

References

https://www.rfc-editor.org/rfc/rfc5545.html#page-52

Parameters:

vcard_file (File | TextIO) – The vcard file to parse.

Return type:

list[tuple[str, str, str, str, str]]

Returns:

A list with the cards main features in tuples.