Sending custom emails from hooks (not using templates)

I am wondering whether is possible to send custom emails through the hooks (we’re using the SMTP provider). I’m wanting to send an email to notify admin when someone signs up. I’m guessing not as I cannot find anything like that, but thought I’d check.

Thanks,
Laura

Hey @auralay, thanks for posting to Auth0 Community.

You can in fact send custom emails from Hooks, Rules extension points.
Do note the implementation will vary according to the email provider you choose.
We have a sample rule to indicate this functionality:

I have tried in past similar code as in the link above on the Post Change Password Hook, and I was able to send an email on successful user signup.

Do try it and let me know how you go!

Hi. I tried to use this in a pre-registration hook and cannot require('request') in the script due to limitations on the node environment that’s made available, I guess. Does that mean that sending a customized e-mail at the pre-registration point cannot be done? It seems like creating this as a rule will not serve my purpose because I specifically need to capture the event of a person attempting to register, not a login. We have users submit enrollment, then someone approves them, then they are admitted. We want to capture that “submit enrollment” event promptly and send an email to someone.
Thanks

Hey @p.reineck, did you try adding the request NPM module in the Hook environment?

If you edit the hook → press the wrench icon → You get an option to Add NPM modules.

Regards,
Sid

1 Like

Thank you, I didn’t know we could do add more modules that way.
I am farther along now (I think). I have some code that works when doing a test run in the hook editor. The problem is that when I turn it on and attempt a new registration at my application I get the error:
Sandbox Error: Extensibility error.
Can you help me out again? Not a node pro by any means.
Thank you

This is what I’ve got:

module.exports = function sendGmail(user, context, callback) {
      const response = {};
      const send = require('gmail-send')({
      user: 'x@y.com',
      pass: context.webtask.secrets.gmail_pwd,
      to: 'y@z.net',
      subject: 'user ' + user.email + ' registration'
    });
     user.app_metadata = user.app_metadata || {};
     console.log(JSON.stringify(user, null, 2));
     // Only send an email when user signs up
     if (user.tenant ) { //will populate later
        send({
      text: user.name + ' has triggered a pre-registration event for tenant ' + user.tenant
    }, (error, result, fullResult) => {
      if (error) console.error(error);
      console.log(result);
    });
      }
      else {
        console.log("else");
      }
      return callback(null, user, context);
    }

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!