Simple Sendgrid Example using Node
Sendgrid offers a robust API for sending and managing emails. Let's walk through a step-by-step guide on how to create a simple Sendgrid app using NodeJS. By the end of this tutorial, you'll have a solid foundation for incorporating email functionality into your own NodeJS applications. Let's get started!
Change directory into your node app.
cd /name/of/node/app
Install Sendgrid using Node Package Manager.
npm i sendgrid
Use your Sendgrid username and password to send an email.
var sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
exports.send = function(req, res, next){
if(req.method == "POST"){
var SENDGRID = {
USER: "<<username>>",
KEY: "<<key>>",
TO: "<< Add Custom email>>",
FROM: "noreply@myapp.com"
};
sendgrid.send({
to : SENDGRID.TO,
from : SENDGRID.FROM,
subject : 'Feedback',
text : JSON.stringify(req.body)
},
function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
}else{
console.log("routes/index:", 'feedback');
//res.render('feedback', { title: 'Feedback', app_id: key.APP_ID, js_key: key.JS_KEY });
}
};