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 • Sr. Frontend Engineer @Chargebee 👨‍💻• I write about modern Javascript & building web apps

© 2021 Nesin Technologies LLP