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:
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.