Reputation: 662
Let's say we have a contact form connected with Strapi backend. Every form submit creates a new model entry and everything's fine except we need to notify administrators about new form submission.
So in api/message/model.js
we add a custom lifecycle method:
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: "[email protected]",
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.
`,
});
},
},
};
But I don't understand how to get administrator email addresses.
I've tried to query admins data like
console.log(
strapi.query("user"),
strapi.query("administrator"),
strapi.query("strapi_administrator")
);
But it does not work.
Upvotes: 2
Views: 963
Reputation: 662
Ok, I got it.
The model name is strapi::user
. So the whole lifecycle hook may look like
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: "[email protected]",
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.
`,
});
},
},
};
Upvotes: 2