Skip to main content

电子邮件插件

¥Email plugin

电子邮件插件使应用能够从服务器或外部提供者发送电子邮件。电子邮件插件使用 Strapi 全局 API,这意味着可以从 Strapi 应用内的任何位置调用它。两个最常见的用例是 Strapi 后端和管理面板。以下文档描述了如何在控制器或服务中使用电子邮件插件来实现后端用例,以及如何使用生命周期钩子来实现管理面板用例。

¥The Email plugin enables applications to send emails from a server or an external provider. The Email plugin uses the Strapi global API, meaning it can be called from anywhere inside a Strapi application. Two of the most common use cases are in the Strapi back end and in the admin panel. The following documentation describes how to use the Email plugin in a controller or service for back-end use cases and using a lifecycle hook for admin panel use cases.

☑️ Prerequisites

电子邮件插件需要提供者以及 config/plugins.js 文件或 config/plugins.ts 文件中的提供者配置。有关详细的安装和配置说明,请参阅 提供者 文档。

¥The Email plugin requires a provider and a provider configuration in the config/plugins.js file or config/plugins.ts file. See the Providers documentation for detailed installation and configuration instructions.

✏️ 注意

Sendmail 是 Strapi 电子邮件插件中的默认电子邮件提供者。它为本地开发环境提供功能,但在默认配置中不适合生产。对于生产阶段应用,你需要进一步配置 Sendmail 或更改提供商。提供者 文档包含有关更改提供者、配置提供者和创建新电子邮件提供者的说明。

¥Sendmail is the default email provider in the Strapi Email plugin. 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. The Providers documentation has instructions for changing providers, configuring providers, and creating a new email provider.

使用控制器或服务发送电子邮件

¥Sending emails with a controller or service

电子邮件插件有一个 email service,其中包含 2 个发送电子邮件的函数:

¥The Email plugin has an email service that contains 2 functions to send emails:

  • send() 直接包含电子邮件内容,

    ¥send() directly contains the email contents,

  • sendTemplatedEmail() 使用内容管理器中的数据来填充电子邮件,从而简化程序化电子邮件。

    ¥sendTemplatedEmail() consumes data from the Content Manager to populate emails, streamlining programmatic emails.

使用 send() 功能

¥Using the send() function

要触发电子邮件以响应用户操作,请将 send() 函数添加到 controllerservice。发送函数具有以下属性:

¥To trigger an email in response to a user action add the send() function to a controller or service. The send function has the following properties:

属性类型格式描述
fromstring电子邮件地址如果未指定,则在 plugins.js 中使用 defaultFrom
tostring电子邮件地址必需的
ccstring电子邮件地址可选的
bccstring电子邮件地址可选的
replyTostring电子邮件地址可选的
subjectstring*必需的
textstring*需要 texthtml
htmlstring超文本标记语言需要 texthtml
This code example can be used in a controller or a service path: ./src/api/{api name}/controllers/{api name}.js or ./src/api/{api name}/services/{api name}.js

await strapi.plugins['email'].services.email.send({
to: 'valid email address',
from: 'your verified email address', //e.g. single sender verification in SendGrid
cc: 'valid email address',
bcc: 'valid email address',
replyTo: 'valid email address',
subject: 'The Strapi Email plugin worked successfully',
text: 'Hello world!',
html: 'Hello world!',
}),

使用 sendTemplatedEmail() 功能

¥Using the sendTemplatedEmail() function

sendTemplatedEmail() 函数用于根据模板撰写电子邮件。该函数根据可用属性编译电子邮件,然后发送电子邮件。要使用 sendTemplatedEmail() 函数,请定义 emailTemplate 对象并将该函数添加到控制器或服务。该函数调用 emailTemplate 对象,并且可以选择调用 emailOptionsdata 对象:

¥The sendTemplatedEmail() function is used to compose emails from a template. The function compiles the email from the available properties and then sends the email. To use the sendTemplatedEmail() function, define the emailTemplate object and add the function to a controller or service. The function calls the emailTemplate object, and can optionally call the emailOptions and data objects:

范围描述类型默认
emailOptions
可选
包含电子邮件寻址属性:tofromreplyToccbccobject{ }
emailTemplate包含电子邮件内容属性:使用 Lodash 字符串模板subjecttexthtmlobject{ }
data
可选
包含用于编译模板的数据object{ }
This code example can be used in a controller or a service path: ./src/api/{api name}/controllers/{api name}.js or ./src/api/{api name}/services/{api name}.js




const emailTemplate = {


subject: 'Welcome <%= user.firstname %>',
text: `Welcome to mywebsite.fr!
Your account is now linked with: <%= user.email %>.`,
html: `<h1>Welcome to mywebsite.fr!</h1>
<p>Your account is now linked with: <%= user.email %>.<p>`,
};

await strapi.plugins['email'].services.email.sendTemplatedEmail(
{
to: user.email,
// from: is not specified, the defaultFrom is used.
},
emailTemplate,
{
user: _.pick(user, ['username', 'email', 'firstname', 'lastname']),
}
);

从生命周期钩子发送电子邮件

¥Sending emails from a lifecycle hook

要根据管理面板中的管理员操作触发电子邮件,请使用 生命周期钩子send() 功能。例如,要在内容管理器中每次添加新内容条目时发送电子邮件,请使用 afterCreate 生命周期钩子:

¥To trigger an email based on administrator actions in the admin panel use lifecycle hooks and the send() function. For example, to send an email each time a new content entry is added in the Content Manager use the afterCreate lifecycle hook:

./src/api/{api-name}/content-types/{content-type-name}/lifecycles.js

module.exports = {
async afterCreate(event) { // Connected to "Save" button in admin panel
const { result } = event;

try{
await strapi.plugin('email').service('email').send({ // you could also do: await strapi.service('plugin:email.email').send({
to: 'valid email address',
from: 'your verified email address', // e.g. single sender verification in SendGrid
cc: 'valid email address',
bcc: 'valid email address',
replyTo: 'valid email address',
subject: 'The Strapi Email plugin worked successfully',
text: '${fieldName}', // Replace with a valid field ID
html: 'Hello world!',

})
} catch(err) {
console.log(err);
}
}
}