I am trying to use azure to send automatic email, I am using a combination of Azure Communication Services and Azure Function App. I am trying to get users of my website receive an automatic email when they sign up. I am using a HTTP trigger in Azure function to automate the process. When I copy the JS code from ACS to the function app I get a server error and haven’t been able to troubleshoot it yet. This is the code
const { EmailClient } = require("@azure/communication-email");
const connectionString = "endpoint=https://test-email-ktv.uk.communication.azure.com/;accesskey=DZx53W8VWIWSD312Ujt3aUeD63AdwCrJ7ZnYRNxodcUFgISfqIjOJQQJ99AKACULyCpygbUvAAAAAZCSSiCT";
const client = new EmailClient(connectionString);
async function sendEmail(name, email) {
const emailMessage = {
senderAddress: "f6772394-f09c-4dc0-94db-c424bc104f40.azurecomm.net", // Replace <from_domain> with your verified domain
content: {
subject: "Membership Confirmation",
plainText: `Hello ${name},nnThank you for signing up!`,
html: `
<html>
<body>
<h1>Hello ${name},</h1>
<p>Thank you for signing up! We have received your details.</p>
</body>
</html>`,
},
recipients: {
to: [{ address: email }],
},
};
try {
const poller = await client.beginSend(emailMessage);
const result = await poller.pollUntilDone();
console.log("Email sent successfully:", result);
return { success: true, message: "Email sent successfully" };
} catch (error) {
console.error("Error sending email:", error);
return { success: false, message: "Failed to send email" };
}
}
module.exports = async function (context, req) {
const { name, email } = req.body;
if (!name || !email) {
context.res = {
status: 400,
body: { message: "Name and Email are required." },
};
return;
}
const response = await sendEmail(name, email);
context.res = {
status: response.success ? 200 : 500,
body: { message: response.message },
};
};
This is the code on my website javascript thats intended to send the user details over to the azure function:
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("membershipForm");
if (form) {
form.addEventListener("submit", async (event) => {
event.preventDefault();
const name = document.getElementById("Full Name").value;
const gender = document.getElementById("Gender").value;
const email = document.getElementById("Email").value;
const phone = document.getElementById("Phone Number").value;
try {
const response = await fetch("https://membership-form-function.azurewebsites.net/api/signup?", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ name, gender, email, phone })
});
if (response.ok) {
document.getElementById("responseMessage").innerText = "Email sent successfully!";
} else {
document.getElementById("responseMessage").innerText = "An error occurred. Please try again.";
}
} catch (error) {
document.getElementById("responseMessage").innerText = "An error occurred. Please try again.";
console.error("Error:", error);
}
});
} else {
console.error("Form element with id 'membershipForm' not found.");
}
});