Apache Airflow provider package for IMAP email server integration and attachment processing
npx @tessl/cli install tessl/pypi-apache-airflow-providers-imap@3.9.0Apache Airflow provider package for IMAP (Internet Message Access Protocol) integration, enabling workflows to connect to mail servers, search for emails with specific attachments, and download them for processing within Airflow DAGs.
pip install apache-airflow-providers-imapMain classes and functionality:
from airflow.providers.imap.hooks.imap import ImapHook
from airflow.providers.imap.sensors.imap_attachment import ImapAttachmentSensorPackage version information:
from airflow.providers.imap import __version__from airflow.providers.imap.hooks.imap import ImapHook
from airflow.providers.imap.sensors.imap_attachment import ImapAttachmentSensor
from airflow import DAG
from datetime import datetime
# Using ImapHook to download attachments
def download_email_attachments():
with ImapHook(imap_conn_id="my_imap_conn") as hook:
# Check if attachment exists
has_attachment = hook.has_mail_attachment(
name="report.csv",
mail_folder="INBOX"
)
if has_attachment:
# Download attachments to local directory
hook.download_mail_attachments(
name="report.csv",
local_output_directory="/tmp/downloads"
)
# Using ImapAttachmentSensor to wait for attachments
dag = DAG('email_processing', start_date=datetime(2024, 1, 1))
sensor = ImapAttachmentSensor(
task_id="wait_for_report",
attachment_name="daily_report.xlsx",
check_regex=False,
mail_folder="INBOX",
conn_id="imap_default",
dag=dag
)The IMAP provider follows Airflow's standard provider architecture:
Provides comprehensive IMAP server connectivity with SSL/TLS support, email searching, attachment detection, and secure file downloads with path traversal protection.
class ImapHook:
def __init__(self, imap_conn_id: str = "imap_default") -> None: ...
def get_conn(self) -> ImapHook: ...
def has_mail_attachment(
self,
name: str,
*,
check_regex: bool = False,
mail_folder: str = "INBOX",
mail_filter: str = "All"
) -> bool: ...
def retrieve_mail_attachments(
self,
name: str,
*,
check_regex: bool = False,
latest_only: bool = False,
mail_folder: str = "INBOX",
mail_filter: str = "All",
not_found_mode: str = "raise",
) -> list[tuple]: ...
def download_mail_attachments(
self,
name: str,
local_output_directory: str,
*,
check_regex: bool = False,
latest_only: bool = False,
mail_folder: str = "INBOX",
mail_filter: str = "All",
not_found_mode: str = "raise",
) -> None: ...Sensor operators that monitor email mailboxes for specific attachments, enabling event-driven workflows triggered by incoming emails.
class ImapAttachmentSensor:
def __init__(
self,
*,
attachment_name,
check_regex=False,
mail_folder="INBOX",
mail_filter="All",
conn_id="imap_default",
**kwargs,
) -> None: ...
def poke(self, context) -> bool: ...The IMAP provider uses Airflow connections with the connection type imap:
imap.gmail.com){
"use_ssl": true,
"ssl_context": "default"
}use_ssl (bool): Enable SSL/TLS connection (default: true)ssl_context (str): SSL context setting ("default" or "none")The provider includes comprehensive error handling:
Attachment operations support configurable error handling:
"raise": Raise AirflowException if attachment not found"warn": Log warning message if attachment not found"ignore": Silent operation if attachment not found