Airflow Summit 2025 is coming October 07-09. Register now for early bird ticket!

airflow.providers.smtp.hooks.smtp

Search in emails for a specific attachment and also to download it.

It uses the smtplib library that is already integrated in python 3.

Classes

SmtpHook

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

Functions

build_xoauth2_string(username, token)

Local fallback for older Airflow cores (≤2.11).

Module Contents

airflow.providers.smtp.hooks.smtp.build_xoauth2_string(username, token)[source]

Local fallback for older Airflow cores (≤2.11).

class airflow.providers.smtp.hooks.smtp.SmtpHook(smtp_conn_id=default_conn_name, auth_type='basic')[source]

Bases: airflow.providers.smtp.version_compat.BaseHook

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

Note

Please call this Hook as context manager via with to automatically open and close the connection to the mail server.

Parameters:

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

conn_name_attr = 'smtp_conn_id'[source]
default_conn_name = 'smtp_default'[source]
conn_type = 'smtp'[source]
hook_name = 'SMTP'[source]
smtp_conn_id = 'smtp_default'[source]
smtp_connection: airflow.sdk.Connection | None = None[source]
smtp_client: smtplib.SMTP_SSL | smtplib.SMTP | None = None[source]
__enter__()[source]
__exit__(exc_type, exc_val, exc_tb)[source]
get_conn()[source]

Login to the smtp server.

Note

Please call this Hook as context manager via with to automatically open and close the connection to the smtp server.

Returns:

an authorized SmtpHook object.

Return type:

SmtpHook

classmethod get_connection_form_widgets()[source]

Return connection widgets to add to connection form.

test_connection()[source]

Test SMTP connectivity from UI.

send_email_smtp(*, to, subject=None, html_content=None, from_email=None, files=None, dryrun=False, cc=None, bcc=None, mime_subtype='mixed', mime_charset='utf-8', custom_headers=None, **kwargs)[source]

Send an email with html content.

Parameters:
  • to (str | collections.abc.Iterable[str]) – Recipient email address or list of addresses.

  • subject (str | None) – Email subject. If it’s None, the hook will check if there is a path to a subject file provided in the connection, and raises an exception if not.

  • html_content (str | None) – Email body in HTML format. If it’s None, the hook will check if there is a path to a html content file provided in the connection, and raises an exception if not.

  • from_email (str | None) – Sender email address. If it’s None, the hook will check if there is an email provided in the connection, and raises an exception if not.

  • files (list[str] | None) – List of file paths to attach to the email.

  • dryrun (bool) – If True, the email will not be sent, but all other actions will be performed.

  • cc (str | collections.abc.Iterable[str] | None) – Carbon copy recipient email address or list of addresses.

  • bcc (str | collections.abc.Iterable[str] | None) – Blind carbon copy recipient email address or list of addresses.

  • mime_subtype (str) – MIME subtype of the email.

  • mime_charset (str) – MIME charset of the email.

  • custom_headers (dict[str, Any] | None) – Dictionary of custom headers to include in the email.

  • kwargs – Additional keyword arguments.

>>> send_email_smtp(
        'test@example.com', 'foo', '<b>Foo</b> bar', ['/dev/null'], dryrun=True
    )
property conn: airflow.sdk.Connection[source]
property smtp_retry_limit: int[source]
property from_email: str | None[source]
property smtp_user: str[source]
property smtp_password: str[source]
property smtp_starttls: bool[source]
property host: str[source]
property port: int[source]
property timeout: int[source]
property use_ssl: bool[source]
property subject_template: str | None[source]
property html_content_template: str | None[source]
property ssl_context: str | None[source]
property auth_type: str[source]
property access_token: str | None[source]
classmethod get_ui_field_behaviour()[source]

Return custom field behaviour.

Was this entry helpful?