How to send Email with Node and SendGrid in 5 Minutes

May 05, 2017

Here’s a little snippet of how to send an email using SendGrid pretty easily

import sendgrid, { mail as helper } from 'sendgrid';

export const sendMail = async payload => {
  const { from_email, to_email, subject, body } = payload;

  const contentType = 'text/html';
  const fromEmail = new helper.Email(from_email);
  const toEmail = new helper.Email(to_email);

  const content = new helper.Content(contentType, body);
  const mail = new helper.Mail(fromEmail, subject, toEmail, content);
  const sg = sendgrid(process.env.SENDGRID_API_KEY);
  const request = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON(),
  return await sg.API(request);

export default sendMail;

👋 I'm Ashik Nesin • Principal Software Engineer @Chargebee 👨‍💻• I write about web development & behinds the scenes of startup/side projects that I'm building

© 2022 Nesin Technologies LLP