Custom email provider - sending test email not working

Hi, I wanted to create custom email provider and test it by button named Send test email but I’m getting some error. I want to use Postmark for email sending and I wrote some code like in the docs, but I used Postmark API:

const { request } = require('undici');

/**
* Handler to be executed while sending an email notification
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {CustomEmailProviderAPI} api - Methods and utilities to help change the behavior of sending a email notification.
*/
exports.onExecuteCustomEmailProvider = async (event, api) => {
  
  const sendEmailRequestBody = {
    From: "testsender@mydomain.com",
    To: event.user.email,
    TemplateId: 1234,
    TemplateModel: {
      user: event.user.email
    }
  };

  try 
  {
    const { statusCode, body } = await request('https://api.postmarkapp.com/email/withTemplate', {
      method: 'POST',
      headers: {
        'X-Postmark-Server-Token': `${event.secrets.PostmarkApiKey}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(sendEmailRequestBody),
    });

    if (statusCode === 200) 
    {
      console.log('Email sent successfully');
    } else if (statusCode >= 500) 
    {
      // Retry on server errors
      api.notification.retry(
        `Internal Server Error received from Messaging Proxy. Status code: ${statusCode}.`
      );
      return;
    }

    // Drop the notification if unrecoverable
    api.notification.drop(
      `Something went wrong while sending the email event to Messaging Proxy. Status code: ${statusCode}.`
    );
  } catch(error)
  {
    console.error(`Error sending email: ${error.message}`);
    api.notification.drop(
      `An unexpected error occurred. Error: ${error.message}`
    );
  }

  return;
};

Locally I was able to send email like this with curl, so it looks ok. In Auth0 logs after sending test email I can see error message like this:

“Error executing trigger: ACTION_MALFORMED: Invalid action code”

How to fix it? How to be sure that I configured it correctly?

Hi,

Welcome to the Auth0 Community!

Could you kindly add some console logs to this action and use the Real-time Webtask Logs Extension to see at what point the action is failing?

I don’t see this action in your tenant or any corresponding logs. Are you working on this in a different tenant?

Thanks,

Mary Beth

Hi, I’m working on different tenant, I’m part of some team.

I have installed Real-time Webtask Logs Extension but after running this test action (Branding → Email provider → Send test email (button)) there is no logs in that extension. I have added some more logging but the logs in extension are empty.

I did some additional test. I have set up the node js app locally and I have used code like this and it worked:

const { request } = require('undici');

const sendEmailRequestBody = {
    From: "test@mydomain.com",
    To: "test2@mydomain.com",
    TemplateId: 1234,
    TemplateModel: {
      user: "test user"
    }
  };

SendEmail(sendEmailRequestBody);


async function SendEmail(requestBody) { 
    const { statusCode, body } = await request('https://api.postmarkapp.com/email/withTemplate', {
        method: 'POST',
        headers: {
          'X-Postmark-Server-Token': 'myApiKey',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(requestBody),
      });

      if (statusCode === 200) 
        {
          console.log('Email sent successfully');
        } else if (statusCode >= 500) 
        {
          console.log('Email sent failed');
        }
}

So I have used this code in Auth0 custom email provider script and there it is not working and the result is the same as before: “Error executing trigger: ACTION_MALFORMED: Invalid action code”.

Custom email provider code:

const { request } = require('undici');

/**
* Handler to be executed while sending an email notification
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {CustomEmailProviderAPI} api - Methods and utilities to help change the behavior of sending a email notification.
*/
exports.onExecuteCustomEmailProvider = async (event, api) => {
  const sendEmailRequestBody = {
    From: "mysenderemail@mydomain.com",
    To: "myemail@mydomain.com",
    TemplateId: 1234,
    TemplateModel: {
      user: "testuser"
    }
  };

SendEmail(sendEmailRequestBody);
};

async function SendEmail(requestBody) { 
    const { statusCode, body } = await request('https://api.postmarkapp.com/email/withTemplate', {
        method: 'POST',
        headers: {
          'X-Postmark-Server-Token': 'myapikey',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(requestBody),
      });

      if (statusCode === 200) 
        {
          console.log('Email sent successfully');
        } else if (statusCode >= 500) 
        {
          console.log('Email sent failed');
        }
}

I was thinking that maybe it is the button fault (“Send test email”) so I have added new user to my tenant and I tried to send verification email for him and it also didn’t worked and the result is the same like in the scenario with test button:
“Error executing trigger: ACTION_MALFORMED: Invalid action code”

I think your issue belongs to this line where you try to import ‘undici’. Regarding to the Auth0 extensibility page (Can I require? - Search which node modules you can use in webtask.io) this is not a supported import so you might switch to another package that is supported.

If you simply require ‘requests’, the code seems to be working without the error that you’ve mentioned.

Thanks! That solved the problem. Now I have used ‘axios’ instead of ‘undici’.

FYI: I have used ‘undici’ because of this example in documentation: Configure a Custom Email Provider

@marybeth.hunter As the docs are no longer open source, could you submit a ticket to change the docs for that?

Hi @lucask & @larsf96,

I’m happy to hear that using axios has resolved the problem. I have let the docs team know about this.

Thanks,

Mary Beth