Skip to content

Instantly share code, notes, and snippets.

@rishavk1102
Last active October 1, 2023 11:58
Show Gist options
  • Save rishavk1102/4eb5f1c4a477e1613761dac1b4fd8c1e to your computer and use it in GitHub Desktop.
Save rishavk1102/4eb5f1c4a477e1613761dac1b4fd8c1e to your computer and use it in GitHub Desktop.
Code to send emails with HTML Body using NodeJS and SendGrid
const express = require("express");
const mail = require("@sendgrid/mail");
const app = express();
// It is highly recommended to store the API Keys in a seperate file and DO NOT check it in Git
mail.setApiKey("__sendgrid_api_key__");
app.post('/email/send', (req, res) => {
// Any 1 of to, cc or bcc are required to successfully send an email.
const message = {
to: ["[email protected]__", "[email protected]__"],
cc: ["[email protected]__", "[email protected]__"],
bcc: ["[email protected]__", "[email protected]__"],
from: {
name: 'Rishav Kumar',
email: '[email protected]'
},
replyTo: '[email protected]',
subject: "__Subject of the email__",
html: ```__Your multiline html content__```,
attachments: [{...}, {...}],
};
mail.send(message).then(() => {
res.status(200).send({
message: 'Email sent successfully!'
});
}).catch((err) => {
res.status(500).send({
message: 'Email not sent!',
error: err.message
});
});
})
let port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("Webserver running on 3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment