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

# メール変更スクリプトのテンプレート

> ユーザーのメールアドレスを変更するカスタムデータベースアクションスクリプトについて説明します。

メール変更スクリプトは、ユーザーのメールアドレスやその検証ステータスが変わった際に、定義済みの関数を実行します。この関数の名前を`changeEmail`にすることをお勧めします。

このスクリプトは[レガシー認証シナリオ](/docs/ja-jp/authenticate/database-connections/custom-db/overview-custom-db-connections) でのみ使用され、ユーザーのメールアドレス（もしくはメールアドレス検証ステータス）をAuth0と外部データベースで同時に更新したい場合には必須です。

メール変更スクリプトは、<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-jp/glossary?term=auth0-dashboard" tip="Auth0 Dashboard: サービスを構成するためのAuth0の主製品。" cta="用語集の表示">Auth0 Dashboard</Tooltip>では構成できません。このスクリプトを管理するには、Auth0 <Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-jp/glossary?term=management-api" tip="Management API: 顧客が管理タスクを実行できるようにするための製品。" cta="用語集の表示">Management API</Tooltip>の[接続の作成](https://auth0.com/docs/api/management/v2#!/Connections/post_connections)または[接続の更新](https://auth0.com/docs/api/management/v2#!/Connections/patch_connections_by_id)エンドポイント、または[Auth0 Deploy CLI](/docs/ja-jp/deploy-monitor/deploy-cli-tool)を使用する必要があります。

<Warning>
  Auth0 Dashboardを使ってカスタムデータベース接続のデータベースアクションスクリプトを管理している場合には、保存時にメール変更スクリプトが自動的に削除されます。

  メール変更スクリプトが誤って削除されていないことを確認するには、Auth0 Management APIまたはAuth0 Deploy CLIを使って接続を管理してください。

  カスタムデータベースでユーザーのメールを手動で変更する場合には、ユーザーに個別に適用しなければなりません。Auth0は変更を自動的には検知しません。
</Warning>

## ChangeEmail関数

changeEmail関数は以下を行います。

* 外部データベースでユーザーのメールアドレスを更新します。
* 処理の失敗やエラーが発生した場合にはエラーを返します。

### 定義

`changeEmail`関数は、4つのパラメーターを受け取り、`コールバック`関数を返します。

```js lines theme={null}
changeEmail(email, newEmail, verified, callback): function
```

| パラメーター     | タイプ  | 説明                              |
| ---------- | ---- | ------------------------------- |
| `email`    | 文字列  | ユーザーの現在のメールアドレス。                |
| `newEmail` | 文字列  | 外部データベースに設定するユーザーの新しいメールアドレスの値。 |
| `verified` | ブール値 | 新しいメールアドレスの確認状況。                |
| `callback` | 関数   | エラーデータをパイプライン経由で渡すために使用。        |

### 例

これは疑似JavaScriptを使った例で、どのようにすれば`changeEmail`関数を実装できるかがわかります。

```javascript lines expandable theme={null}
function (email, newEmail, verified, callback) {
  // Prepare the API call
  let options = {
    url: "https://example.com/api/users",
    action: "PATCH",
    body: {
      email: email,
      new_email: newEmail,
      email_verified: verified
    }
  };

  // Call the API
  send(options, err => {
    // Return `false` value in callback if operation failed
    if (err && err.id == "FAIL_CHANGE_EMAIL") {
      return callback(null, false);
    } else if (err) {
      // Return error in callback if unspecified error occurred
      return callback(new Error("My custom error message."));
    }

    // Return `true` value in callback if operation succeeded
    return callback(null, true);
  });
}
```

## コールバック関数

`callback`関数は2つのパラメーターを受け取り、1つの関数を返します。

### 定義

```js lines theme={null}
callback(error, operationResult): function
```

| パラメーター            | タイプ    | 必須 | 説明             |
| ----------------- | ------ | -- | -------------- |
| `error`           | オブジェクト | 必須 | エラーデータを含む。     |
| `operationResult` | ブール値   | 任意 | メール変更操作の結果を示す。 |

### 成功の場合

メールアドレス変更操作が成功した場合、`callback`関数を返し、`error`パラメーターには`null`を、`operationResult`パラメーターには`true`を渡します。

```js lines theme={null}
return callback(null, true);
```

### 失敗の場合

メールアドレス変更操作が失敗した場合、`callback`関数を返し、`error`パラメーターには`null`を、`operationResult`パラメーターには`false`を渡します。

```js lines theme={null}
return callback(null, false);
```

### エラーの場合

エラーが発生した場合は、`callback`関数を返し、`error`パラメーターに関連するエラー情報を渡します。

```js lines theme={null}
return callback(new Error("My custom error message."));
```

## もっと詳しく

* [パスワード変更スクリプトのテンプレート](/docs/ja-jp/authenticate/database-connections/custom-db/templates/change-password)
* [作成スクリプトのテンプレート](/docs/ja-jp/authenticate/database-connections/custom-db/templates/create)
* [スクリプトのテンプレートを削除する](/docs/ja-jp/authenticate/database-connections/custom-db/templates/delete)
* [ユーザー取得スクリプトのテンプレート](/docs/ja-jp/authenticate/database-connections/custom-db/templates/get-user)
* [ログインスクリプトのテンプレート](/docs/ja-jp/authenticate/database-connections/custom-db/templates/login)
* [検証スクリプトのテンプレート](/docs/ja-jp/authenticate/database-connections/custom-db/templates/verify)
