How do I send invitation link to new admin-user by email

Hi All! When we invite a new user by the admin panel we get an invitation link to share with that user. I want to share that link by sending an email on the invite automatically.

I have set up the email plugin and its working. I thought of two approaches to send email, by using hooks or by overriding create action but unfortunately I have not been able to make it work.

I added this code in extensions/admin/content-types/User/lifecycles.js but it does not log anything.

module.exports = {
  async afterCreate(event) {
    console.log("New user created");
  },
};
System Information
  • Strapi Version: 4.0.7
  • Operating System: Windows 11
  • Database: PostgreSQL
  • Node Version: 14.19.0
  • NPM Version:
  • Yarn Version:

2 Likes

Were you able to resolve it ?. I’m also having the same issues

1 Like

I can send emails at other times. I found it strange to see that, when creating a user in the administration panel, despite requiring an email in the form, it ends up not sending any email, just presenting a link on the front-end to copy it manually. So I decided to look for information on the internet and found this post. I’m curious to know how to trigger the email with the link, automatically, in this case. I will continue to follow this topic. Greetings from Brazil :slight_smile:

2 Likes

I also want to send invitation mails, any update for this topic?

All the best
Tobi

Hi all! I think I’ve found a solution. I generated a plugin with npm run strapi generate plugin and in bootstrap.js, I did the following:

  strapi.db.lifecycles.subscribe({
    models: ['admin::user'],
    async afterCreate({ result }) {
      const { registrationToken } = result;
      if (!registrationToken) return;
      
      // Email invite logic goes here
    },
  });

Basically, we can add an afterCreate hook to the admin::user entity and handle the email logic in there. Here’s a more in-depth example in case you’re curious: strapi-plugin-invite-email/bootstrap.js at main · arellaTV/strapi-plugin-invite-email · GitHub

And if it’s helpful at all, I’ve also published an npm package: strapi-plugin-invite-email - npm

You can install it with npm i strapi-plugin-invite-email. It’s pretty bare bones, so I would suggest creating your own local plugin, but it’s there as an option if anyone needs it.

3 Likes