Overriding sendConfirmationEmail method in Strapi V4

Hi everyone,

I’m new to Strapi and I’m currently developing an application with Strapi V4 using TypeScript.
I’m trying to override the sendConfirmationEmail method (in file node_modules/@strapi/plugin-users-permissions/server/services/user.js) which is called after a user registers in the register controller.
This functionality is provided by the Users & Permissions plugin when you enable the email confirmation.
My goal is to send an email defined in SendGrid using their template system instead of the default email of Strapi.
I added a strapi-server.ts file in the extension directory (src/extensions/users-permissions/strapi-server.ts) to override the method.

// src/extensions/users-permissions/strapi-server.ts
export default function (plugin) {
  const registerController = plugin.controllers.auth.register;

  plugin.controllers.auth.register = async (ctx) => {
    // this code is executed
  }

  const sendConfirmationEmail = plugin.services.user.sendConfirmationEmail;

  plugin.services.user.sendConfirmationEmail = async (user) => {
    // this code is not executed
  }

  return plugin;
}

However, this does not seem to be working as expected. My custom method sendConfirmationEmail is not called.
I have already overridden the register method to add additional information to my user, and I call the original method to keep Strapi’s default behavior, this works as desired.

Can anyone help me figure out what I’m missing or doing wrong?

And more generally, what is the procedure for redefining a method of a plugin service? I would also like to “decorate” the send method of the email plugin to add behavior based on my environment.

Thank you!

This topic has been created from a Discord post (1248558673811476490) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

yeah the emailing and plugin/email plugin/users-permissions are kind of lacking in terms of customization

Im running into the same issue, any ideas?

<@1072591948499664996> how do I override sendConfirmationEmail function?

Let me check the knowledge base for you. One moment! :books:


To override the sendConfirmationEmail function in Strapi, you can use the concept of extensions. Here are the steps:

  1. Copy the User.js file from ./node_modules/strapi-plugin-users-permissions/services/ to ./extensions/users-permissions/services/.

  2. In the copied User.js file, remove all the unused functions and modify the sendConfirmationEmail function as per your requirements.

Here is an example of how your User.js file might look after modification:

'use strict';

const crypto = require('crypto');
const { sanitizeEntity, getAbsoluteServerUrl } = require('strapi-utils');

module.exports = {
  async sendConfirmationEmail(user) {
    // Your custom logic here...
  },
};

Please note that you should be careful when using extensions and upgrading your Strapi version, as mentioned in the Strapi documentation (https://strapi.io/documentation/developer-docs/latest/concepts/customization.html#plugin-extensions).

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

So first:

yarn strapi services:list

// check uid
plugin::users-permissions.user                 

then

// src/index.ts
export default {
  ...  
  bootstrap({ strapi }: { strapi: Strapi }) {
    strapi.plugins["users-permissions"].services["user"].sendConfirmationEmail =
      async (ctx) => {
        console.log("my custom send confiramtion email");
      };
  },
};

<@149511171555328000> <@1204255677321576520> <@1238075063199072352>

Hi <@632956853122236457> , thanks for your help, I will try your solution.

not sure why your original override is’t working tho, looks correct to me, i think it’s might be overwritten somewhere else

Will try this out soon aswell