【问题标题】:Strapi send email to all administratorsStrapi 向所有管理员发送电子邮件
【发布时间】:2020-11-16 22:10:16
【问题描述】:

假设我们有一个与 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


    【解决方案1】:

    好的,我明白了。 型号名称为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.
            `,
          });
        },
      },
    };
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      相关资源
      最近更新 更多