配置电子邮件通知

Cloud Composer 1 |Cloud Composer 2 |Cloud Composer 3

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

准备工作

如果您的 Cloud Composer 环境 针对专用 IP 进行了配置,请确保它已连接到网络 (例如 https://api.sendgrid.com 或您的首选 SMTP 服务器)。

配置 SendGrid 电子邮件服务

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

注册 SendGrid

如果您尚未注册 SendGrid,请在 Google Cloud 控制台中注册,点击“在提供商处管理”以转到 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。 请务必为 Cloud Storage 存储分区 Secrets 后端。

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

    部分
    email email_conn_id sendgrid_default
    email email_backend airflow.providers.sendgrid.utils.emailer.send_email
  4. 为名为 的 SendGrid 连接创建一个密钥, airflow-connections-sendgrid_default.将该密钥的值设置为 连接 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。在连接中指定 Sendgrid API 密钥 URI例如:

    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

后续步骤