設定電子郵件通知

Managed Airflow (第 3 代) | Managed Airflow (第 2 代) | Managed Airflow (舊版第 1 代)

本頁面說明如何為 Managed Airflow 環境設定 SMTP 服務。

事前準備

  • 如果 Managed Airflow 環境設為使用私人 IP,請確認該環境可連線至外部電子郵件服務的 API 端點 (例如 https://api.sendgrid.com 或您偏好的 SMTP 伺服器)。

設定 SendGrid 電子郵件服務

如要接收通知,請設定環境變數,透過 SendGrid 電子郵件服務傳送電子郵件。

註冊 SendGrid

如果您尚未註冊,請在 Google Cloud 控制台註冊 SendGrid,然後按一下「Manage On Provider」(在供應商網站上管理) 前往 SendGrid 網域,再按一下「Settings」(設定) 擷取使用者名稱並建立 API 金鑰。 Google Cloud 開發人員每月可免費傳送 12,000 封電子郵件。

前往 SendGrid Email API

設定變數

SendGrid 會使用 API 金鑰和寄件者電子郵件地址傳送郵件。您可以透過下列任一方式提供這項資訊:

  • (建議) 將值儲存在 Secret Manager

  • 在 Airflow 中儲存值

將值儲存在 Secret Manager

如要在 Secret Manager 儲存值,請按照下列步驟操作:

  1. 為環境設定 Secret Manager。請務必設定密鑰後端的權限和 Airflow 設定選項。

  2. 覆寫下列 Airflow 設定選項:

    區段 鍵 值
    email email_conn_id sendgrid_default
    email email_backend airflow.providers.sendgrid.utils.emailer.send_email
  3. 為 SendGrid 連線建立名為 airflow-connections-sendgrid_default 的密鑰。將密鑰的值設為連線 URI。例如:

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

    如要進一步瞭解如何將連線儲存在 Secret Manager,請參閱「管理 Airflow 連線」。

  4. 設定 SendGrid 的電子郵件地址。無法透過 Secret 設定電子郵件地址。請改為覆寫下列 Airflow 設定選項:

    區段 鍵 值
    email from_email 寄件者電子郵件地址,例如 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 的電子郵件地址。無法透過 Secret 設定電子郵件地址。請改為覆寫下列 Airflow 設定選項:

    區段 鍵 值
    email from_email 寄件者電子郵件地址,例如 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 UI 中,前往「Activity」頁面。
  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 密碼

在 Airflow 設定檔中以純文字格式保留 SMTP 密碼,並非良好的安全做法。因此,Managed Airflow 不支援這種方法。您可以改用其他兩種方法設定 SMTP 密碼。

使用指令擷取 SMTP 密碼

無法使用 Managed Airflow (舊版第 1 代) 中的指令擷取 SMTP 密碼。透過指令提供密碼時,在 Airflow UI 中存取「Admin」(管理) >「Configuration」(設定) 會觸發錯誤。

使用儲存在 Secret Manager 中的密鑰擷取 SMTP 密碼

您可以設定 Secret Manager 做為 Airflow 密鑰後端。

為 Composer 環境設定 Secret Manager 後,即可將 SMTP 密碼儲存在 Secret Manager 中:

  1. 建立新的密鑰:

    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 設定選項。Airflow 從 Secret Manager 擷取 airflow-config-smtp-password 密鑰時,會自動在您為 smtp_password_secret 指定的值前面加上 airflow-config- 前置字串。

    區段 鍵 值
    smtp smtp_password_secret smtp-password

後續步驟