send email via sendgrid node code example
Example 1: sendgrid nodejs send email template
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
templateId: 'd-f43daeeaef504760851f727007e0b5d0',
dynamic_template_data: {
subject: 'Testing Templates',
name: 'Some One',
city: 'Denver',
},
};
sgMail.send(msg);
Example 2: node js sendgrid
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
};
sgMail.send(msg).catch(err => {
console.log(err);
});
Example 3: node js sendgrid
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const fs = require("fs");
pathToAttachment = `${__dirname}/attachment.pdf`;
attachment = fs.readFileSync(pathToAttachment).toString("base64");
const msg = {
to: [email protected]',
from: '[email protected]',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
attachments: [
{
content: attachment,
filename: "attachment.pdf",
type: "application/pdf",
disposition: "attachment"
}
]
};
sgMail.send(msg).catch(err => {
console.log(err);
});