Strapi 向所有管理员发送电子邮件

Strapi send email to all administrators

假设我们有一个与 Strapi 后端连接的联系表。每个表单提交都会创建一个新的模型条目,一切都很好,除了我们需要通知管理员有关新表单提交的信息。 所以在api/message/model.js中我们添加了一个自定义的生命周期方法:

module.exports = {
  lifecycles: {
    async afterCreate(result, data) {
      await strapi.plugins["email"].services.email.send({
        to: [/* Here a list of administrator email addresses should be. How to get it? */],
        from: "robot@strapi.io",
        subject: "New message from contact form",
        text: `
          Yay! We've got a new message.
          User's name: ${result.name}.
          Phone: ${result.phone}.
          Email: ${result.email}.
          Message: ${result.text}.

          You can also check it out in Messages section of admin area.
        `,
      });
    },
  },
};

但我不明白如何获取管理员电子邮件地址。

我试过查询管理员数据,例如

console.log(
  strapi.query("user"),
  strapi.query("administrator"),
  strapi.query("strapi_administrator")
);

但是不行。

好的,我明白了。 型号名称是 strapi::user。所以整个生命周期钩子可能看起来像

module.exports = {
  lifecycles: {
    async afterCreate(result, data) {
      const administrators = await strapi.query("strapi::user").find({
        isActive: true,
        blocked: false,
        "roles.code": "strapi-super-admin",
      });
      const emails = administrators.map((a) => a.email);
      await strapi.plugins["email"].services.email.send({
        to: emails,
        from: "robot@strapi.io",
        subject: "New message from contact form",
        text: `
          Yay! We've got a new message.
          User's name: ${result.name}.
          Phone: ${result.phone}.
          Email: ${result.email}.
          Message: ${result.text}.

          You can also check it out in Messages section of admin area.
        `,
      });
    },
  },
};