# 创建自定义电子邮件提供商

> Source: https://strapi.nodejs.cn/cms/configurations/email-custom-providers

🌐 Creating custom email providers

通过导出一个带有 `send()` 函数的 Node.js 模块来构建自定义电子邮件提供程序。可以在项目中本地使用，也可以发布到 npm 与社区共享。

Strapi的[电子邮件功能](/cms/features/email)将发送委托给提供者，这是一个实现标准接口的Node.js模块。当没有官方或社区提供者满足你的需求时，你可以自己创建一个。

🌐 Strapi's [Email feature](/cms/features/email) delegates sending to a provider, a Node.js module that implements a standard interface. When no official or community provider meets your needs, you can create your own.

## 提供者接口 {#provider-interface}

🌐 Provider interface

要实现自定义提供者，你必须 [create a Node.js module](https://npm.nodejs.cn/creating-node-js-modules)。该模块必须导出一个 `init` 函数，该函数返回一个 `send` 函数：

```js
module.exports = {
  init: (providerOptions = {}, settings = {}) => {
    return {
      send: async options => {},
    };
  },
};
```

```ts

  init: (providerOptions = {}, settings = {}) => {
    return {
      send: async options => {},
    };
  },
};
```

在 `send` 函数内部，你可以访问：

🌐 Inside the `send` function you have access to:

- `providerOptions` — 在 `/config/plugins.js|ts` 中传递给 `providerOptions` 的值。
- `settings` — 在 `/config/plugins.js|ts` 中传递给 `settings` 的值。
- `options` — 从控制器或服务调用 `send()` 时传递的选项。

你可以查看 [Strapi-maintained providers](https://github.com/strapi/strapi/tree/main/packages/providers) 以获取参考实现。

## 发布和使用你的提供者 {#publishing-and-using-your-provider}

🌐 Publishing and using your provider

在创建提供程序后，你可以将其发布到 npm 或在项目中本地使用它。

🌐 After creating your provider you can either publish it to npm or use it locally within your project.

### 发布到 npm {#publishing-to-npm}

🌐 Publishing to npm

[Publish your provider to npm](https://npm.nodejs.cn/creating-and-publishing-unscoped-public-packages) 使其可供 Strapi 社区使用。一旦发布，就像安装和配置其他提供者一样进行安装和配置（参见 [配置提供者](/cms/features/email#configuring-providers)）。

### 使用本地提供商 {#using-a-local-provider}

🌐 Using a local provider

在不将其发布到 npm 的情况下使用自定义提供程序：

🌐 To use a custom provider without publishing it to npm:

1. 在你的 Strapi 应用根目录下创建一个 `providers` 文件夹。
2. 在其中创建你的提供者（例如，`providers/strapi-provider-email-custom`）。
3. 更新 `package.json` 以将依赖指向本地路径：

```json
{
  "dependencies": {
    "strapi-provider-email-custom": "file:providers/strapi-provider-email-custom"
  }
}
```

4. 更新 `/config/plugins.js|ts` 以 [配置提供程序](/cms/features/email#configuring-providers)。
5. 运行 `yarn` 或 `npm install` 来链接本地包。

## 私有供应商 {#private-providers}

🌐 Private providers

提供者可以被标记为**私有**，这意味着每个资源 URL 都将被签名以实现安全访问，而不是以普通 URL 形式暴露。

🌐 A provider can be marked as **private**, meaning every asset URL will be signed for secure access rather than exposed as a plain URL.

要启用此功能，请在你的提供者模块中实现 `isPrivate()` 方法并返回 `true`。

🌐 To enable this, implement the `isPrivate()` method and return `true` in your provider module.

当提供者是私有时，Strapi 使用 `getSignedUrl(file)` 方法——该方法也在提供者中实现——为每个资源生成一个签名 URL。签名 URL 包含一个加密签名，根据你的实现，该签名允许在有限时间内并带有特定限制地访问资源。

🌐 When a provider is private, Strapi uses a `getSignedUrl(file)` method — also implemented in the provider — to generate a signed URL for each asset. The signed URL contains an encrypted signature that grants access for a limited time and with specific restrictions, depending on your implementation.

:::note

出于安全原因，内容 API 不返回签名 URL。使用该 API 的开发者必须使用提供者的签名逻辑自行签署 URL。

🌐 For security reasons, the Content API does not return signed URLs. Developers consuming the API must sign URLs themselves using the provider's signing logic.

:::
