创建自定义电子邮件提供商
🌐 Creating custom email providers
Page summary:通过导出一个带有
send()函数的 Node.js 模块来构建自定义电子邮件提供程序。可以在项目中本地使用,也可以发布到 npm 与社区共享。
Strapi的电子邮件功能将发送委托给提供者,这是 一个实现标准接口的Node.js模块。当没有官方或社区提供者满足你的需求时,你可以自己创建一个。
🌐 Strapi's Email feature 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
要实现自定义提供者,你必须 create a Node.js module。该模块必须导出一个 init 函数,该函数返回一个 send 函数:
- JavaScript
- TypeScript
module.exports = {
init: (providerOptions = {}, settings = {}) => {
return {
send: async options => {},
};
},
};
export default {
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 以获取参考实现。
发布和使用你的提供者
🌐 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
Publish your provider to npm 使其可供 Strapi 社区使用。一旦发布,就像安装和配置其他提供者一样进行安装和配置(参见 配置提供者)。
使用本地提供商
🌐 Using a local provider
在不将其发布到 npm 的情况下使用自定义提供程序:
🌐 To use a custom provider without publishing it to npm:
- 在你的 Strapi 应用根目录下创建一个
providers文件夹。 - 在其中创建你的提供者(例如,
providers/strapi-provider-email-custom)。 - 更新
package.json以将依赖指向本地路径:
{
"dependencies": {
"strapi-provider-email-custom": "file:providers/strapi-provider-email-custom"
}
}
- 更新
/config/plugins.js|ts以 配置提供程序。 - 运行
yarn或npm install来链接本地包。