airflow.contrib.hooks.imap_hook

Module Contents

class airflow.contrib.hooks.imap_hook.ImapHook(imap_conn_id='imap_default')[source]

Bases:airflow.hooks.base_hook.BaseHook

This hook connects to a mail server by using the imap protocol.

Parameters

imap_conn_id (str) – The connection id that contains the information used to authenticate the client.

__enter__(self)[source]
__exit__(self, exc_type, exc_val, exc_tb)[source]
has_mail_attachment(self, name, mail_folder='INBOX', check_regex=False)[source]

Checks the mail folder for mails containing attachments with the given name.

Parameters
  • name (str) – The name of the attachment that will be searched for.

  • mail_folder (str) – The mail folder where to look at.

  • check_regex (bool) – Checks the name for a regular expression.

Returns

True if there is an attachment with the given name and False if not.

Return type

bool

retrieve_mail_attachments(self, name, mail_folder='INBOX', check_regex=False, latest_only=False, not_found_mode='raise')[source]

Retrieves mail’s attachments in the mail folder by its name.

Parameters
  • name (str) – The name of the attachment that will be downloaded.

  • mail_folder (str) – The mail folder where to look at.

  • check_regex (bool) – Checks the name for a regular expression.

  • latest_only (bool) – If set to True it will only retrieve the first matched attachment.

  • not_found_mode (str) – Specify what should happen if no attachment has been found. Supported values are ‘raise’, ‘warn’ and ‘ignore’. If it is set to ‘raise’ it will raise an exception, if set to ‘warn’ it will only print a warning and if set to ‘ignore’ it won’t notify you at all.

Returns

a list of tuple each containing the attachment filename and its payload.

Return type

a list of tuple

download_mail_attachments(self, name, local_output_directory, mail_folder='INBOX', check_regex=False, latest_only=False, not_found_mode='raise')[source]

Downloads mail’s attachments in the mail folder by its name to the local directory.

Parameters
  • name (str) – The name of the attachment that will be downloaded.

  • local_output_directory (str) – The output directory on the local machine where the files will be downloaded to.

  • mail_folder (str) – The mail folder where to look at.

  • check_regex (bool) – Checks the name for a regular expression.

  • latest_only (bool) – If set to True it will only download the first matched attachment.

  • not_found_mode (str) – Specify what should happen if no attachment has been found. Supported values are ‘raise’, ‘warn’ and ‘ignore’. If it is set to ‘raise’ it will raise an exception, if set to ‘warn’ it will only print a warning and if set to ‘ignore’ it won’t notify you at all.

_handle_not_found_mode(self, not_found_mode)[source]
_retrieve_mails_attachments_by_name(self, name, mail_folder, check_regex, latest_only)[source]
_list_mail_ids_desc(self)[source]
_fetch_mail_body(self, mail_id)[source]
_check_mail_body(self, response_mail_body, name, check_regex, latest_only)[source]
_create_files(self, mail_attachments, local_output_directory)[source]
_is_escaping_current_directory(self, name)[source]
_correct_path(self, name, local_output_directory)[source]
_create_file(self, name, payload, local_output_directory)[source]
class airflow.contrib.hooks.imap_hook.Mail(mail_body)[source]

Bases:airflow.LoggingMixin

This class simplifies working with mails returned by the imaplib client.

Parameters

mail_body (str) – The mail body of a mail received from imaplib client.

has_attachments(self)[source]

Checks the mail for a attachments.

Returns

True if it has attachments and False if not.

Return type

bool

get_attachments_by_name(self, name, check_regex, find_first=False)[source]

Gets all attachments by name for the mail.

Parameters
  • name (str) – The name of the attachment to look for.

  • check_regex (bool) – Checks the name for a regular expression.

  • find_first (bool) – If set to True it will only find the first match and then quit.

Returns

a list of tuples each containing name and payload where the attachments name matches the given name.

Return type

list of tuple

class airflow.contrib.hooks.imap_hook.MailPart(part)[source]

This class is a wrapper for a Mail object’s part and gives it more features.

Parameters

part (any) – The mail part in a Mail object.

is_attachment(self)[source]

Checks if the part is a valid mail attachment.

Returns

True if it is an attachment and False if not.

Return type

bool

has_matching_name(self, name)[source]

Checks if the given name matches the part’s name.

Parameters

name (str) – The name to look for.

Returns

True if it matches the name (including regular expression).

Return type

tuple

has_equal_name(self, name)[source]

Checks if the given name is equal to the part’s name.

Parameters

name (str) – The name to look for.

Returns

True if it is equal to the given name.

Return type

bool

get_file(self)[source]

Gets the file including name and payload.

Returns

the part’s name and payload.

Return type

tuple