E-mail Template change

System Information
  • Strapi Version: 3.5.3
  • Operating System: Win64x
  • Database: SQLLite
  • Node Version: 14.11.0
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.5

Hi everyone

i would like to customize the “password reset” text for the admin area. this topic was discussed here, but is not working. the static text is defined in forgot-password.js https://github.com/strapi/strapi/blob/master/packages/strapi-admin/config/email-templates/forgot-password.js.

where do i need to copy the changed file? under /config/email-templates/forgot-password or /admin/config/email-templates/forgot-password, both doesn’t work.

So, no Strapi people can offer an explanation for this. Version 4 is starting to become a real pain.

For anyone trying to amend the reset password and email confirmation email template in version 4.

in the extensions folder (if it wasn’t automatically generated just create it in the app route- src/extensions)
then add users-permissions/content-types/user/schema.json, and users-permissions/strapi-server.js.
add the code from below.
this is from the docs -

module.exports = (plugin) => {
  plugin.controllers.auth.forgotPassword = (ctx) => {
};

module.exports = (plugin) => {
  plugin.controllers.auth.register = (ctx) => {
};

  return plugin;
};

the above code will override the node modules, You can copy the code from the node modules do not change anything except the settings.message which is the template.

this is my code

const forgot = require("./email-template/forgot_password");
const reset = require("./email-template/reset_password");

const crypto = require("crypto");
const _ = require("lodash");
const utils = require("@strapi/utils");

const { sanitize } = utils;
const { ApplicationError, ValidationError } = utils.errors;

const emailRegExp =
  /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;


const sanitizeUser = (user, ctx) => {
  const { auth } = ctx.state;
  const userSchema = strapi.getModel("plugin::users-permissions.user");

  return sanitize.contentAPI.output(user, userSchema, { auth });
};

const getService = (name) => {
  return strapi.plugin("users-permissions").service(name);
};
module.exports = (plugin) => {
  plugin.controllers.user.find = (ctx) => {
    console.log(ctx, "I am the find ctx");
  };

  plugin.controllers.auth.forgotPassword = async (ctx) => {
    let { email } = ctx.request.body;

    // Check if the provided email is valid or not.
    const isEmail = emailRegExp.test(email);

    if (isEmail) {
      email = email.toLowerCase();
    } else {
      throw new ValidationError("Please provide a valid email address");
    }

    const pluginStore = await strapi.store({
      type: "plugin",
      name: "users-permissions",
    });

    // Find the user by email.
    const user = await strapi
      .query("plugin::users-permissions.user")
      .findOne({ where: { email: email.toLowerCase() } });

    // User not found.
    if (!user) {
      throw new ApplicationError("This email does not exist");
    }

    // User blocked
    if (user.blocked) {
      throw new ApplicationError("This user is disabled");
    }

    // Generate random token.
    const resetPasswordToken = crypto.randomBytes(64).toString("hex");

    const settings = await pluginStore
      .get({ key: "email" })
      .then((storeEmail) => {
        try {
          return storeEmail["reset_password"].options;
        } catch (error) {
          return {};
        }
      });

    const advanced = await pluginStore.get({
      key: "advanced",
    });

    const userInfo = await sanitizeUser(user, ctx);

    

    settings.message = reset.html

    settings.message = await getService("users-permissions").template(
      settings.message,
      {
        URL: advanced.email_reset_password,
        USER: userInfo,
        TOKEN: resetPasswordToken,
      }
    );
    
    console.log(settings.message);

    settings.object = await getService("users-permissions").template(
      settings.object,
      {
        USER: userInfo,
      }
    );

    try {
      // Send an email to the user.
      await strapi
        .plugin("email")
        .service("email")
        .send({
          to: user.email,
          from:
            settings.from.email || settings.from.name
              ? `${settings.from.name} <${settings.from.email}>`
              : undefined,
          replyTo: settings.response_email,
          subject: settings.object,
          text: settings.message,
          html: settings.message,
        });
    } catch (err) {
      throw new ApplicationError(err.message);
    }

    // Update the user.
    await strapi
      .query("plugin::users-permissions.user")
      .update({ where: { id: user.id }, data: { resetPasswordToken } });

    ctx.send({ ok: true });
    console.log(email, "I am the forgot password ctx");
  };

  return plugin;
};

the node modules path is node_modules/@strapi/plugins-users-permissions/server/controllers/auth.js

I have not done it myself but I believe you could create a controllers folder in extensions folder and just register the controllers in strapi-server.js for better abstraction.

i hope this helps I have been struggling for while until I worked it out.