Sending an email on content published from the admin

I am trying to achieve something that seems pretty standard to me but I can’t find a way to achieve it.

I want to send an email everytime a content is added from the admin panel. I tried to override the content controller, but I noticed that content added from the admin do not trigger controllers.

What is the standard way of doing it?

Thank you

That’s because these controllers are only for the API.

You can use afterCreate lifecycles for your needs.

Thank you! Two questions from there:

  • will afterCreate be triggered on published or at first draft creation?
  • What are use cases where I should use API only controllers instead of general model lifecycles?

Lifecycles are functions triggered on data manipulation (from Admin UI and from API)

Controllers are functions that you attach to your routes (Only from API).

afterCreate will be triggered at the first draft creation.

If you want to trigger a function only when it’s published then you need to use beforeUpdate and compare the previous state of published_at and the current state. If the previous state is null and the current one contains the date, then call your function.

async beforeUpdate(params, data) {
      if (data.published_at != null) {
        const { id } = data;
        const previousData = await{ id });
        const previousPublishedAt = previousData.published_at;
        const currentPublished_at = data.published_at;
        if (currentPublished_at != previousPublishedAt) {
          console.log('Triggered only when published.');

So everytime a controller is run, at least one lifecycle is also run right? That’s why I don’t really understand if I should write my function in a controller or lifecycle function.

Write your custom code as a function and trigger it from the lifecycle.

Yes, in case you use the strapi’s core update controller then that one will also trigger beforeUpdate and afterUpdate lifecycles.