sendgrid nodejs send email template
// Step 1: Install the SendGrid Node.js library
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('YOUR_SENDGRID_API_KEY');
// Step 2: Create an email template using the SendGrid Template Engine
const templateId = 'YOUR_TEMPLATE_ID';
// Step 3: Prepare the email data
const msg = {
to: '[email protected]',
from: '[email protected]',
templateId: templateId,
dynamic_template_data: {
subject: 'Your Email Subject',
username: 'Recipient Username',
// Add other template variables as needed
},
};
// Step 4: Send the email using SendGrid's send function
sgMail.send(msg)
.then(() => console.log('Email sent successfully'))
.catch((error) => console.error(error.message));
Note: Replace 'YOUR_SENDGRID_API_KEY'
with your actual SendGrid API key and 'YOUR_TEMPLATE_ID'
with the ID of the email template you want to use. Update the email addresses and dynamic_template_data values accordingly. Ensure you have the @sendgrid/mail
library installed in your project.