配置电子邮件通知

Cloud Composer 1 | Cloud Composer 2

本页面介绍如何为 Cloud Composer 环境配置 SMTP 服务。

准备工作

如果您的 Cloud Composer 环境配置了专用 IP,请确保该环境已连接到您的外部电子邮件服务(例如 https://api.sendgrid.com 或您的首选 SMTP 服务器)的 API 端点。

配置 SendGrid 电子邮件服务

若想接收通知,请配置相应环境变量,以通过 SendGrid 电子邮件服务发送电子邮件。

使用 SendGrid 注册

如果您尚未在 Google Cloud 控制台中注册 SendGrid,请点击“在提供商处管理”以转到 SendGrid 网域,然后点击“设置”以检索您的用户名并创建 API 密钥。作为 Google Cloud 开发者,您每月可免费发送 12000 封电子邮件。

转到 SendGrid Email API

配置变量

SendGrid 使用 API 密钥和“发件人”电子邮件地址发送邮件。您可以通过以下选项之一提供此信息:

  • (推荐)将值存储在 Secret Manager 中。

  • 将值存储在 Airflow 中。

在 Secret Manager 中存储值

如需将值存储在 Secret Manager 中,请执行以下操作:

  1. 安装 apache-airflow-providers-sendgrid PyPI 软件包。

  2. 为您的环境配置 Secret Manager。请务必为 Secret 后端设置权限和 Airflow 配置选项。

  3. 替换以下 Airflow 配置选项:

    部分
    email email_conn_id sendgrid_default
    email email_backend airflow.providers.sendgrid.utils.emailer.send_email
  4. 为名为 airflow-connections-sendgrid_default 的 SendGrid 连接创建密钥。将密钥的值设置为连接 URI。例如:

    sendgrid://<username>:<sendgrid_api_key>@smtp.sendgrid.net:587
    

    如需详细了解如何在 Secret Manager 中存储连接,请参阅管理 Airflow 连接

  5. 为 SendGrid 配置电子邮件地址:

    • (Airflow 2.2.4 及更高版本)无法通过密钥设置电子邮件地址。请改为替换以下 Airflow 配置选项:

      email from_email 发件人电子邮件地址,例如 noreply@example.com
    • (Airflow 版本 2.1.4 到 2.1.3)为您的环境设置以下环境变量

      名称
      SENDGRID_MAIL_FROM 发件人电子邮件地址,例如 noreply@example.com

在 Airflow 中存储值

  1. 替换以下 Airflow 配置选项:

    部分
    email email_conn_id sendgrid_default
    email email_backend airflow.providers.sendgrid.utils.emailer.send_email
  2. 在 Airflow 中,配置名为 sendgrid_default 的连接。在连接 URI 中指定 Sendgrid API 密钥。例如:

    gcloud composer environments run ENVIRONMENT_NAME \
      --location LOCATION \
      connections add -- \
      --conn-uri "sendgrid://USERNAME:SENDGRID_API_KEY@smtp.sendgrid.net:587" \
      sendgrid_default
    

    替换以下内容:

    • ENVIRONMENT_NAME:您的环境的名称。
    • LOCATION:环境所在的区域。
    • USERNAME:SendGrid 用户名。
    • SENDGRID_API_KEY:SendGrid API 密钥。
  3. 为 SendGrid 配置电子邮件地址:

    • (Airflow 2.2.4 及更高版本)替换以下 Airflow 配置选项:

      email from_email 发件人电子邮件地址,例如 noreply@example.com
    • (Airflow 版本 2.1.4 到 2.1.3)为您的环境设置以下环境变量

      名称
      SENDGRID_MAIL_FROM 发件人电子邮件地址,例如 noreply@example.com

测试您的 SendGrid 配置

如需测试 SendGrid 配置,请执行以下操作:

  1. 创建一个使用 EmailOperator 的测试 DAG。例如:

import datetime

import airflow
from airflow.operators.email import EmailOperator

with airflow.DAG(
    "composer_sample_sendgrid",
    start_date=datetime.datetime(2022, 1, 1),
) as dag:
    task_email = EmailOperator(
        task_id="send-email",
        conn_id="sendgrid_default",
        # You can specify more than one recipient with a list.
        to="user@example.com",
        subject="EmailOperator test for SendGrid",
        html_content="This is a test message sent through SendGrid.",
        dag=dag,
    )
  1. 将 DAG 上传到您的环境,并检查任务是否成功执行。
  2. 使用您的 SendGrid 凭据登录 SendGrid。
  3. 在 SendGrid 界面中,转到“活动”页面。
  4. 在列表中搜索该电子邮件。您应该会看到 SendGrid 已处理并递送该电子邮件。
  5. 如果该电子邮件未被处理和递送,请执行以下操作:

    • 检查您的 SendGrid 配置。
    • 验证您是否启用了 Secret Manager 后端。确保您已授予额外的权限,并为 Airflow 配置选项设置了替换项。
    • 检查您的电子邮件客户端的垃圾邮件过滤器。

配置第三方 SMTP 服务

如需通过第三方 SMTP 服务发送电子邮件,请替换 email_backend Airflow 配置选项并配置其他与 SMTP 相关的参数。

如需配置第三方 SMTP 服务,请替换以下 Airflow 配置选项:

部分
email email_backend airflow.utils.email.send_email_smtp
smtp smtp_host SMTP 服务器的主机名。
smtp smtp_user SMTP 服务器上的用户名。
smtp smtp_port SMTP 服务器的端口。端口 25 不可用。您可以使用其他端口,例如标准 SMTP 端口 465 和 587。
smtp smtp_password 不支持通过 smtp_password 设置密码。要设置 SMTP 密码,请按照配置 SMTP 密码中的说明操作。
smtp smtp_mail_from 发件人电子邮件地址,例如 noreply-composer@
smtp smtp_starttls 为增强安全性,请将其设置为 True
smtp smtp_ssl 为增强安全性,请将其设置为 True

为第三方 SMTP 服务配置 SMTP 密码

将 SMTP 密码以纯文本形式保存在 Airflow 配置文件中是一种不好的安全做法。因此,Cloud Composer 不支持此方法。您可以使用其他两种方法来配置 SMTP 密码。

使用命令检索 SMTP 密码

您可以使用配置替换来指定用于获取 SMTP 密码的命令。与 SMTP 服务通信时,Airflow 会使用此命令获取密码值。指定一个命令,不要使用管道和重定向等 Bash 运算符。

如需使用此方法,请替换以下 Airflow 配置选项:

smtp smtp_password_cmd 指定用于返回 SMTP 密码的命令。

使用存储在 Secret Manager 中的密文来检索 SMTP 密码

您可以配置 Secret Manager 作为 Airflow Secret 后端。

为 Composer 环境配置 Secret Manager 后,您可以将 SMTP 密码存储在 Secret Manager 中:

  1. 创建新 Secret :

    echo -n "SMTP_PASSWORD" | gcloud beta secrets create \
      airflow-config-smtp-password \
      --data-file=- \
      --replication-policy=automatic
    

    SMTP_PASSWORD 替换为您的 SMTP 密码。

  2. 配置 Airflow 以从 Secret Manager 获取 SMTP 密码。为此,请替换以下 Airflow 配置选项:

    部分
    smtp smtp_password_secret smtp-password

后续步骤