电子邮件
电子邮件功能使 Strapi 应用能够从服务器或外部提供商发送电子邮件。
¥The Email feature enables Strapi applications to send emails from a server or an external provider.
配置
¥Configuration
电子邮件功能的大多数配置选项都通过你的 Strapi 项目的代码处理。电子邮件功能无法在管理面板中配置,但是如果管理员已设置电子邮件,用户可以测试电子邮件传递。
¥Most configuration options for the Email feature are handled via your Strapi project's code. The Email feature is not configurable in the admin panel, however users can test email delivery if it has been setup by an administrator.
管理面板设置
¥Admin panel settings
配置功能的 路径: 设置 > 电子邮件功能 > 配置
¥Path to configure the feature: Settings > Email feature > Configuration


在配置界面中,只有 "测试电子邮件传递" 下的电子邮件地址字段可供用户修改。发送测试电子邮件按钮可发送测试电子邮件。
¥In the Configuration interface, only the email address field under "Test email delivery" is modifiable by users. A Send test email button sends a test email.
此页面仅在当前角色启用了 "访问电子邮件设置页面" 权限时可见(有关更多信息,请参阅 RBAC 功能 文档):
¥This page is only visible if the current role has the "Access the Email Settings page" permission enabled (see RBAC feature documentation for more information):


基于代码的配置
¥Code-based configuration
电子邮件功能需要提供程序以及 config/plugins.js|ts
文件中的 提供程序配置。有关详细的安装和配置说明,请参阅 providers。
¥The Email feature requires a provider and a provider configuration in the config/plugins.js|ts
file. See providers for detailed installation and configuration instructions.
发送邮件 是 Strapi 电子邮件功能中的默认电子邮件提供商。它为本地开发环境提供功能,但在默认配置中不适合生产。对于生产阶段应用,你需要进一步配置 Sendmail
或更改提供商。
¥Sendmail is the default email provider in the Strapi Email feature. It provides functionality for the local development environment but is not production-ready in the default configuration. For production stage applications you need to further configure Sendmail
or change providers.
电子邮件配置选项
¥Email configuration options
插件配置在 config/plugins.js
文件或 config/plugins.ts
文件中定义。请参阅 providers,了解特定于提供商的详细安装和配置说明。
¥Plugins configuration are defined in the config/plugins.js
file or config/plugins.ts
file. Please refer to providers for detailed provider-specific installation and configuration instructions.
选项 | 类型 | 描述 | 默认值 | 注意 |
---|---|---|---|---|
provider | string | 要使用的电子邮件提供商。 | sendmail | 必需的 |
providerOptions | object | 电子邮件提供商选项。 | {} | 可选的 |
providerOptions.apiKey | string | 电子邮件提供商的 API 密钥。 | '' | 可选的 |
settings | object | 电子邮件设置。 | {} | 可选的 |
settings.defaultFrom | string | 用作发件人的默认电子邮件地址。 | '' | 可选的 |
settings.defaultReplyTo | string | 用作回复地址的默认电子邮件地址。 | '' | 可选的 |
ratelimit | object | 电子邮件速率限制设置。 | {} | 可选的 |
ratelimit.enabled | boolean | 是否启用速率限制。 | true | 可选的 |
ratelimit.interval | string | 速率限制的间隔(分钟)。 | 5 | 可选的 |
ratelimit.max | number | 间隔内允许的最大请求数。 | 5 | 可选的 |
ratelimit.delayAfter | number | 应用速率限制之前允许的请求数。 | 1 | 可选的 |
ratelimit.timeWait | number | 响应请求之前等 待的时间(以毫秒为单位)。 | 1 | 可选的 |
ratelimit.prefixKey | string | 速率限制键的前缀。 | ${userEmail} | 可选的 |
ratelimit.whitelist | array(string) | 要从速率限制中列入白名单的 IP 地址数组。 | [] | 可选的 |
ratelimit.store | object | 速率限制存储位置,有关更多信息,请参阅 koa2-ratelimit 文档。 | MemoryStore | 可选的 |
提供者
¥Providers
电子邮件功能可以通过安装和配置其他提供程序来扩展。
¥The Email feature can be extended via the installation and configuration of additional providers.
提供商为插件的核心功能添加了扩展,例如,使用 Amazon SES 而不是 Sendmail 来发送电子邮件。
¥Providers add an extension to the core capabilities of the plugin, for example to use Amazon SES for emails instead of Sendmail.
Strapi 维护的官方提供程序(可通过 市场 找到)以及许多社区维护的提供程序(可通过 npm 获取)。
¥There are both official providers maintained by Strapi — discoverable via the Marketplace — and many community maintained providers available via npm.
可以将提供程序配置为 private,以确保对资源 URL 进行签名以实现安全访问。
¥A provider can be configured to be private to ensure asset URLs will be signed for secure access.
安装提供程序
¥Installing providers
可以使用 npm
或 yarn
使用以下格式 @strapi/provider-<plugin>-<provider> --save
安装新的提供程序。
¥New providers can be installed using npm
or yarn
using the following format @strapi/provider-<plugin>-<provider> --save
.
例如,要安装 Sendgrid 提供程序:
¥For example, to install the Sendgrid provider:
- Yarn
- NPM
yarn add @strapi/provider-email-sendgrid
npm install @strapi/provider-email-sendgrid --save
配置提供者
¥Configuring providers
新安装的提供程序在 /config/plugins
文件 中启用并配置。如果该文件不存在,则必须创建它。
¥Newly installed providers are enabled and configured in the /config/plugins
file. If this file does not exist you must create it.
每个提供者都有不同的可用配置设置。查看 市场 或 npm 中该提供商的相应条目以了解更多信息。
¥Each provider will have different configuration settings available. Review the respective entry for that provider in the Marketplace or npm to learn more.
以下是 Sendgrid 提供商的示例配置:
¥The following is an example configuration for the Sendgrid provider:
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
// ...
email: {
config: {
provider: 'sendgrid', // For community providers pass the full package name (e.g. provider: 'strapi-provider-email-mandrill')
providerOptions: {
apiKey: env('SENDGRID_API_KEY'),
},
settings: {
defaultFrom: 'juliasedefdjian@strapi.io',
defaultReplyTo: 'juliasedefdjian@strapi.io',
testAddress: 'juliasedefdjian@strapi.io',
},
},
},
// ...
});
export default ({ env }) => ({
// ...
email: {
config: {
provider: 'sendgrid', // For community providers pass the full package name (e.g. provider: 'strapi-provider-email-mandrill')
providerOptions: {
apiKey: env('SENDGRID_API_KEY'),
},
settings: {
defaultFrom: 'juliasedefdjian@strapi.io',
defaultReplyTo: 'juliasedefdjian@strapi.io',
testAddress: 'juliasedefdjian@strapi.io',
},
},
},
// ...
});
-
当每个环境使用不同的提供程序时,请在
/config/env/${yourEnvironment}/plugins.js|ts
中指定正确的配置(请参阅 环境)。¥When using a different provider per environment, specify the correct configuration in
/config/env/${yourEnvironment}/plugins.js|ts
(See Environments). -
一次只有一个电子邮件提供者处于活动状态。如果 Strapi 未选择电子邮件提供者设置,请验证
plugins.js|ts
文件是否位于正确的文件夹中。¥Only one email provider will be active at a time. If the email provider setting isn't picked up by Strapi, verify the
plugins.js|ts
file is in the correct folder. -
使用在 Strapi 设置期间创建的两个电子邮件模板测试新电子邮件提供者时,模板上的发送者电子邮件默认为
no-reply@strapi.io
,需要根据你的电子邮件提供者进行更新,否则将无法通过测试(请参阅 本地配置模板)。¥When testing the new email provider with those two email templates created during strapi setup, the shipper email on the template defaults to
no-reply@strapi.io
and needs to be updated according to your email provider, otherwise it will fail the test (See Configure templates locally).
每个环境的配置
¥Configuration per environment
配置提供程序时,你可能希望根据 NODE_ENV
环境变量更改配置或使用特定于环境的凭据。
¥When configuring your provider you might want to change the configuration based on the NODE_ENV
environment variable or use environment specific credentials.
你可以在 /config/env/{env}/plugins.js|ts
配置文件中设置特定配置,它将用于覆盖默认配置。
¥You can set a specific configuration in the /config/env/{env}/plugins.js|ts
configuration file and it will be used to overwrite the default configuration.