メインコンテンツへスキップ

Auth0 Dashboard を使用してメールプロバイダー Action を設定する

Auth0 Dashboard を使用して、テナントにカスタムメールプロバイダー Action を設定するには、次の手順に従います。
  1. Auth0 Dashboard > Branding > Email Provider に移動します。
  2. Use my own email provider トグルを有効にします。
  3. Email Provider セクションで、Custom Provider を選択します。
  4. From フィールドに、メールの送信元となるデフォルトのメールアドレスを入力します。
  5. コードエディターで、メールプロバイダー経由でメッセージを送信するための Action コードを記述します。API または SMTP 接続の詳細については、ご利用のプロバイダーのドキュメントを参照してください。 コードエディターの左側メニューでは、鍵アイコンをクリックしてシークレット (たとえば API 認証に使用するもの) を追加でき、ボックスアイコンをクリックして依存関係を追加できます。
  6. Action の記述が完了したら、Save をクリックしてデプロイします。
設定をテストするには、Send Test Email をクリックします。 他の Actions と同様に、Management API を使用して Action とその versions を管理できます。すべての Actions には同じ limitations が適用されます。

Terraform を使用してメールプロバイダー Action を設定する

Terraform Auth0 provider は、Auth0 Management API を使用して Auth0 プラットフォーム上の操作を実行します。 Terraform Auth0 provider を使用すると、カスタムメールプロバイダー Action を作成し、それを使用するようにテナントを設定できます。

1. 競合する Actions のバインドを解除または削除する

custom-email-provider トリガーにバインドできる Action は 1 つまでです。 テナントですでにカスタムメールプロバイダー Action が設定されている場合は、Terraform で新しいメールプロバイダー Action を作成する前にリセットしてください。
  1. Auth0 Dashboard > Branding > Email Provider に移動します。
  2. Email Provider セクションで、Custom Provider をクリックします。
  3. Actions コードエディターの下にある Reset をクリックします。
Management API を使用すると、custom-email-provider トリガーにバインドされている他のデプロイ済み Action があるかどうかを確認できます。
  1. Actions の一覧を取得して、重複している Action を特定します。
  2. トリガーバインディングを更新するか、Action を削除します。

2. 新しいメールプロバイダー Action を作成する

Terraform の auth0_action リソースを使用して、custom-email-provider トリガーに対応する Action を作成します。deploy = true を設定すると、Action の新しいバージョンがすぐに作成されます。
resource "auth0_action" "custom_email_provider" {
  name    = "Custom Email Provider"
  runtime = "node20"
  deploy  = true
  code    = <<-EOT
    /**
    * Handler to be executed while sending an email notification
    * @param {Event} event - Details about the user and the context in which they are logging in.
    * @param {CustomEmailProviderAPI} api - Methods and utilities to help change the behavior of sending a email notification.
    */
    exports.onExecuteCustomEmailProvider = async (event, api) => {
      // ここにコードを記述します
      return;
    };
  EOT
  supported_triggers {
    id      = "custom-email-provider"
    version = "v1"
  }
}
関数スタブに、メールプロバイダーにメッセージを送信するための Action コードを記述します。API または SMTP サーバーの詳細については、ご利用のプロバイダーのドキュメントを参照してください。

3. Action をメールプロバイダートリガーに関連付ける

Terraform の auth0_trigger_action リソースを使用して、Action を custom-email-provider トリガーに関連付けます:
resource "auth0_trigger_action" "custom_email_provider" {
  trigger = "custom-email-provider"
  actions {
    id           = auth0_action.custom_email_provider.id
    display_name = auth0_action.custom_email_provider.name
  }
   depends_on = [
    auth0_action.custom_email_provider
  ]
 }

4. Action を使用するようにテナントのメールプロバイダーを設定する

Terraform の auth0_email_provider リソースを使用して、テナントがメールプロバイダー Action を使用するように設定します。
resource "auth0_email_provider" "custom_email_provider" {
  name                 = "custom"
  enabled              = true
  default_from_address = "accounts@example.com"
  credentials {}
  depends_on = [
    auth0_trigger_actions.custom_email_provider
  ]
Terraform で新しいメールプロバイダー Action を作成しても反映されない場合は、custom-email-provider トリガーにバインドされたデプロイ済みの Action がすでに存在している可能性があります。この問題の切り分けと解決のため、この記事の最初の手順に従って、競合する Action のバインドを解除するか削除してください

メールプロバイダー Action の例

これは、custom-email-provider トリガー用の Action の例です。 このコードサンプルでは、関数 onExecuteCustomEmailProvidercustom-email-provider event object から 2 つの引数を受け取ります。1 つは event で、ユーザーに関する情報と通知のコンテキストが含まれます。もう 1 つは api で、通知の送信時にカスタム動作を行うためのヘルパーメソッドを提供します。
/**
 * メール通知の送信中に実行されるハンドラー。
 * @param {Event} event - ユーザーおよびログインコンテキストに関する詳細情報。
 * @param {CustomEmailProviderAPI} api - メール通知の送信動作を変更するためのメソッドとユーティリティ。
 */
exports.onExecuteCustomEmailProvider = async (event, api) => {
  // メールペイロードを定義する
  const emailPayload = {
    from: {
      name: "Test Sender",
      email: "sender@example.com"
    },
    to: [{ email: event.user.email }],
    subject: event.notification.message_type,
    html: event.notification.html,
    text: event.notification.text,
  };
  try {
    // メール送信のAPI呼び出しを実行する
    const response = await fetch('https://api.example.com/send-email', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${event.secrets.api_key}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(emailPayload),
    });
    if (response.ok) {
      console.log('Email sent successfully');
    } else if (response.status >= 500) {
      api.notification.retry(
        `Internal Server Error received from Messaging Proxy. Status code: ${response.status}.`
      );
      return;
    }
  } catch (error) {
    console.error(`Error sending email: ${error.message}`);
    api.notification.drop(`An unexpected error occurred. Error: ${error.message}`);
  }
  return;
};
Actions の詳細については、Auth0 Actions の仕組みをご覧ください。