Skip to content

Instantly share code, notes, and snippets.

@BolajiAyodeji
Last active September 29, 2022 15:00
Show Gist options
  • Save BolajiAyodeji/c475dc5e56ae103cfc31131b3bd4adb5 to your computer and use it in GitHub Desktop.
Save BolajiAyodeji/c475dc5e56ae103cfc31131b3bd4adb5 to your computer and use it in GitHub Desktop.
let output = "";
const apiKey = "xxx";
async function sendEmail() {
await fetch("https://api.sendgrid.com/v3/mail/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: {
template_id: "d-cd0b22632dcc427cb2b98e3bdd562e5a",
personalizations: [
{
to: [
{
email: inputData.customerEmail,
name: inputData.customerName
}
],
from: {
email: "bolaji@xxx",
name: "Cake Store Team"
},
reply_to: {
email: "noreply@xxx",
name: "No Reply"
},
subject: `Thank you for your order, ${inputData.customerName}!`,
dynamic_template_data: {
customerName: inputData.customerName,
orderTimeStamp: inputData.orderTimeStamp,
dateFormat: "DD MMMM, YYYY h:mm:ss A",
timezoneOffset: "-0800",
shipmentNumber: `${
inputData.shipmentNumber.match(/#\d{8}\/S\/\d{3}/g)[0]
}`,
orderNumber: inputData.orderNumber,
customerEmail: inputData.customerEmail,
marketName: inputData.marketName,
paymentMethod: inputData.paymentMethod,
shippingAddress: inputData.shippingAddress,
shippingMethod: inputData.shippingMethod,
lineItems: inputData.lineItems,
totalAmount: inputData.totalAmount,
shippingAmount: inputData.shippingAmount,
grandTotalAmount: inputData.grandTotalAmount,
}
}
]
}
})
.then((response) => {
output = response;
console.log(output);
})
.catch((error) => {
console.error(error);
});
}
sendEmail();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment