Send transactional emails with SendGrid in Node.js — setup, dynamic templates, email verification, welcome emails, and deliverability best practices.

Abdur Razzak
Full-Stack Web Developer
Sending email from a plain Node.js server using Nodemailer directly is unreliable — your IP will be blacklisted, emails will land in spam, and you will have zero visibility into delivery. Email service providers like SendGrid, Mailgun, and Resend handle deliverability, bounce management, unsubscribes, and analytics. SendGrid is widely used in the Node.js ecosystem with a generous free tier of 100 emails per day.
Install @sendgrid/mail and set your API key: sgMail.setApiKey(process.env.SENDGRID_API_KEY). Create an API key in your SendGrid dashboard with 'Mail Send' permission only — use the principle of least privilege for API keys. Verify your sender email address or domain in SendGrid to improve deliverability. Your from address must be a verified sender.
Send an email with sgMail.send({ to, from, subject, text, html }). Always provide both text and html versions — some email clients don't render HTML. For HTML emails, use inline styles (not external CSS) for maximum compatibility. Keep your email templates simple and tested across email clients — use Litmus or Email on Acid for cross-client testing.
SendGrid Dynamic Templates let you design email templates in SendGrid's visual editor and inject data using Handlebars syntax. Call sgMail.send() with templateId and dynamicTemplateData: { name: user.name, resetLink: url }. This keeps your email HTML in SendGrid's editor (designed by non-developers) and your Node.js code clean of HTML strings.
For email verification: generate a secure random token, store it in your database with an expiry timestamp, and send a verification link containing the token. When the user clicks the link, verify the token, mark the email as verified, and delete the token. For welcome emails, trigger them in a post-registration queue (not synchronously in the registration handler) so a slow email send doesn't delay the registration response.
Set up SPF, DKIM, and DMARC DNS records for your sending domain — SendGrid provides the values in their settings. These records prove to receiving mail servers that your emails are legitimate. Maintain a clean email list: process bounces and unsubscribes using SendGrid's Event Webhooks, and remove invalid addresses from your database immediately. Monitor your SendGrid dashboard for spam reports and act on them quickly.