MailerSmtpOAuthHook

Send email over SMTP authenticated with OAuth2/XOAUTH2 instead of a static password.

class airflow.providers.mailer.hooks.smtp_oauth.MailerSmtpOAuthHook(mailer_conn_id: str = 'mailer_smtp_oauth_default', **kwargs: Any)

Basisklassen: BaseHook

Send email over SMTP using OAuth2 / XOAUTH2 instead of a static password.

The host/port/login fields are the usual SMTP connection details (e.g. smtp.office365.com on port 587, with login set to the mailbox address used for AUTH XOAUTH2). The access token can come from either:

  • Azure AD client-credentials flow – set extras tenant_id and client_id, plus a Client Secret in the connection password field.

  • A pre-obtained access token – set extra access_token directly (refreshing it is the caller’s responsibility).

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', **kwargs: Any) → None

Send an HTML email over SMTP, authenticating via AUTH XOAUTH2.

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".

  • **kwargs (Any) – Accepted for interface parity with the other mailer hooks; unused here.

Veroorzaakt:

ValueError – If no sender address can be determined.

Example:

from airflow.providers.mailer.hooks.smtp_oauth import MailerSmtpOAuthHook

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