Get email notifications on login (api.sendEmail in action)

Hello, we’re using a custom action to get an email when user log into the app through auth0 however it stopped working since old runtime were deprecated apparently. We tried to upgrade this code:

exports.onExecutePostUserRegistration = async (event, api) => {
  const adminEmail = "info@example.com";
  const userEmail = event.user.email;

  try {
    // Use Auth0's built-in email provider to send an email
    await api.sendEmail({
      to: adminEmail,
      template: {
        subject: "New User Registration",
        body: `A new user has registered with the email: ${userEmail}`,
      },
    });
  } catch (error) {
  if (error instanceof Error) {
    console.error("Error sending admin email:", error.message);
  } else {
    console.error("An unknown error occurred.");
  }
}
};

However it now fails, as shown in action real-time logs:

10:36:39: Error sending admin email: api.sendEmail is not a function

We wish to be informed of any login in the app: is there an easy way to do this now? Or is there a new way to replace api.sendEmail with the same features? Help appreciated.

Thanks.

Hi @axel-insightsphere,

The error api.sendEmail is not a function occurs because Auth0 Actions do not have a built-in method to send emails. The api object in Actions is strictly for manipulating the authentication transaction (like denying access or adding claims).

To send an email (like an admin notification), you must use an external email provider (such as SendGrid, AWS SES, or Mailgun) and call their API from within your Action.

Additionally, your code snippet uses onExecutePostUserRegistration, which runs only once when a user first creates an account. Still, you’re stating that you want to be “informed of any login.”, which means you will need to use the Post Login trigger instead.

This other community post provides an example code snippet that can help.

If you have any further questions, please don’t hesitate to reach out.

Have a good one,
Vlad

Hello Vlad. Thank you for your answer. Are there any alternative? For example (a) where we could receive a notification in auth0 dashboard, by using that api object or something else or (b) consult the creation of accounts somewhere? It looks like monitoring > logs only keep for last 24h which is not useful, apparently it’s depending on the plan we’re at (we want to know account creation even if >30 days). Do you have simpler suggestions than external email provider? alerts/notifs in auth0 would be great. Thank you.

You’re correct, I meant only for new registration actually.

Hi @axel-insightsphere,

The Auth0 Dashboard does not have a built-in “Notification Center” or “Inbox” that can be triggered by the api object in Actions. However, you can view user accounts indefinitely (beyond the log retention limit) using the User Management view. For alerts, you can access Monitoring > Log Streams and create a Log Stream. A Webhook (to Slack or Teams) is often the simplest alternative to email.

Have a good one,
Vlad

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.