> ## Documentation Index
> Fetch the complete documentation index at: https://translations.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# カスタマイズした Action でメールプロバイダーを設定する

> Auth0 Dashboard または Terraform Auth0 provider からメールプロバイダーにメッセージを送信する Action を作成し、ユースケースに応じたカスタムロジックを実装します。

<div id="configure-an-email-provider-action-using-the-auth0-dashboard">
  ## Auth0 Dashboard を使用してメールプロバイダー Action を設定する
</div>

Auth0 Dashboard を使用して、テナントにカスタムメールプロバイダー Action を設定するには、次の手順に従います。

1. [Auth0 Dashboard > Branding > Email Provider](https://manage.auth0.com/#/templates/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](https://auth0.com/docs/api/management/v2/actions/get-actions) を使用して Action とその [versions](/ja/docs/customize/actions/manage-versions) を管理できます。すべての Actions には同じ [limitations](/ja/docs/customize/actions/limitations) が適用されます。

<div id="configure-an-email-provider-action-using-terraform">
  ## Terraform を使用してメールプロバイダー Action を設定する
</div>

[Terraform Auth0 provider](https://registry.terraform.io/providers/auth0/auth0/latest/docs) は、[Auth0 Management API](https://auth0.com/docs/api/management/v2) を使用して Auth0 プラットフォーム上の操作を実行します。

Terraform Auth0 provider を使用すると、カスタムメールプロバイダー Action を作成し、それを使用するようにテナントを設定できます。

<div id="1-unbind-or-delete-conflicting-actions">
  ### 1. 競合する Actions のバインドを解除または削除する
</div>

`custom-email-provider` トリガーにバインドできる Action は 1 つまでです。

テナントですでにカスタムメールプロバイダー Action が設定されている場合は、Terraform で新しいメールプロバイダー Action を作成する前にリセットしてください。

1. [Auth0 Dashboard > Branding > Email Provider](https://manage.auth0.com/#/templates/provider) に移動します。
2. **Email Provider** セクションで、**Custom Provider** をクリックします。
3. Actions コードエディターの下にある **Reset** をクリックします。

Management API を使用すると、`custom-email-provider` トリガーにバインドされている他のデプロイ済み Action があるかどうかを確認できます。

1. [Actions の一覧を取得](https://auth0.com/docs/api/management/v2/actions/get-actions)して、重複している Action を特定します。
2. [トリガーバインディングを更新](https://auth0.com/docs/api/management/v2/actions/patch-bindings)するか、[Action を削除](https://auth0.com/docs/api/management/v2/actions/delete-action)します。

<div id="2-create-a-new-email-provider-action">
  ### 2. 新しいメールプロバイダー Action を作成する
</div>

[Terraform の `auth0_action` リソース](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/action)を使用して、`custom-email-provider` トリガーに対応する Action を作成します。`deploy = true` を設定すると、Action の新しいバージョンがすぐに作成されます。

```terraform lines theme={null}
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 サーバーの詳細については、ご利用のプロバイダーのドキュメントを参照してください。

<div id="3-bind-the-action-to-the-email-provider-trigger">
  ### 3. Action をメールプロバイダートリガーに関連付ける
</div>

[Terraform の `auth0_trigger_action` リソース](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/trigger_action)を使用して、Action を `custom-email-provider` トリガーに関連付けます:

```terraform lines theme={null}
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
  ]
 }
```

<div id="4-configure-your-tenants-email-provider-with-the-action">
  ### 4. Action を使用するようにテナントのメールプロバイダーを設定する
</div>

[Terraform の `auth0_email_provider` リソース](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/email_provider)を使用して、テナントがメールプロバイダー Action を使用するように設定します。

```terraform lines theme={null}
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 のバインドを解除するか削除してください](#1-unbind-or-delete-conflicting-actions)。

<div id="example-email-provider-action">
  ## メールプロバイダー Action の例
</div>

これは、`custom-email-provider` トリガー用の Action の例です。

このコードサンプルでは、関数 `onExecuteCustomEmailProvider` は [`custom-email-provider` event object](/ja/docs/customize/email/configure-a-custom-email-provider/action-triggers-custom-email-provider-event-object) から 2 つの引数を受け取ります。1 つは `event` で、ユーザーに関する情報と通知のコンテキストが含まれます。もう 1 つは `api` で、通知の送信時にカスタム動作を行うためのヘルパーメソッドを提供します。

```javascript lines expandable theme={null}
/**
 * メール通知の送信中に実行されるハンドラー。
 * @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 の仕組み](/ja/docs/customize/actions/actions-overview)をご覧ください。
