MailerHook

Dispatching hook that routes email sending to the SMTP, Graph, or SMTP+OAuth2 implementation.

class airflow.providers.mailer.hooks.mailer.MailerHook(mailer_conn_id: str = 'smtp_default', **kwargs: Any)

Bases: BaseHook

Dispatching hook for sending email.

Resolves mailer_conn_id and routes to the implementation that matches the connection’s type:

  • smtp – Airflow’s standard SMTP connection (default conn id smtp_default). Plain login/password SMTP, no extra setup required.

  • mailer_graph – MailerGraphHook, sends via the Microsoft Graph API.

  • mailer_smtp_oauth – MailerSmtpOAuthHook, sends via SMTP using OAuth2/XOAUTH2.

Any connection type other than mailer_graph/mailer_smtp_oauth is treated as plain SMTP.

get_conn() → Connection

Return the resolved Airflow Connection, caching it on the hook instance.

Returns:

The connection identified by mailer_conn_id.

Return type:

Connection

send_email(to: list[str] | str, subject: str, html_content: str, from_email: str | None = None, cc: list[str] | str | None = None, bcc: list[str] | str | None = None, files: list[str] | None = None, mime_subtype: str = 'mixed', mime_charset: str = 'utf-8') → None

Send an HTML email, delegating to Graph/SMTP-OAuth2 if the connection type matches.

Parameters:
  • to (list[str] | str) – Recipient address(es).

  • subject (str) – Email subject.

  • html_content (str) – HTML body.

  • from_email (str, optional) – Sender override. Defaults to the connection’s from_email extra, then the connection login.

  • cc (list[str] | str, optional) – Carbon-copy address(es).

  • bcc (list[str] | str, optional) – Blind carbon-copy address(es).

  • files (list[str], optional) – Absolute file paths to attach.

  • mime_subtype (str) – MIME multipart subtype. Defaults to "mixed".

  • mime_charset (str) – Character set for the HTML body. Defaults to "utf-8".

Raises:

ValueError – If no sender address can be determined.

Example:

from airflow.providers.mailer.hooks.mailer import MailerHook

MailerHook(mailer_conn_id="smtp_default").send_email(
    to=["ops@example.com"],
    subject="Deploy finished",
    html_content="<p>Deploy finished successfully.</p>",
)