Sending Email with SendGrid and NodeJS

Rishav Kumar

10/13/20233 min read

In this blog, we will learn how to send an email using SendGrid and Node.js with Express.js. We will also be able to attach attachments to it seamlessly.

Pre-requisite : A domain owned by you.

Thank you for taking the time to read this blog! I appreciate your interest and hope you found it valuable.

I also welcome any questions or discussions you may have, so feel free to reach out to me anytime via email or the form in the footer of this blog. Your feedback and engagement mean a lot to me!

To stay updated with our latest content, please consider subscribing so that you'll be notified whenever new blogs are published.

P.S. You can use your gmail or any other free email here but it is not recommended since DMARC will not be able to prove the authenticity of the email sender. DMARC stands for Domain-based Message Authentication, Reporting and Conformance. To know more about this head to https://docs.sendgrid.com/ui/sending-email/dmarc

Sender Authentication

  • Head over to SendGrid and create a free account if you haven’t already created one.

  • Once your account is created go to Settings->Sender Authentication and then click on the Verify a Single Sender button to add a sender.

Sending Email with SendGrid and NodeJS
Sending Email with SendGrid and NodeJS
  • Follow the steps mentioned to verify a single sender, once completed successfully, you can see them listed here

Sending Email with SendGrid and NodeJS
Sending Email with SendGrid and NodeJS

Getting SendGrid API Key

  • Once we have verified the sender, go to Settings->API Keys and then click on Create API Key button to generate an API Key to allow us to send an email.

Send Email Using NodeJS and SendGrid
Send Email Using NodeJS and SendGrid
  • Add the details

  • API Key Name: Whatever name you use to identify the API Key

  • You can choose the API Key Permission level according to your needs, I will be giving my API Key full access.

Send Email using NodeJS and SendGrid
Send Email using NodeJS and SendGrid
  • After clicking on Create & View, you will be able to see the API key

  • Copy it and keep it somewhere as you will not be able to see it again

  • Click on Done and there you have your API Key.

Send Email using NodeJs and SendGrid
Send Email using NodeJs and SendGrid

Creating a Project

  • Open a terminal at the folder where you want to create the project, then execute these commands:

  • Open index.js that you just created and write this code:

  • Body of Email is a text

  • Body of Email is a HTML

  • Combining text and HTML in your message is recommended. Notifications and email lists display a snippet, so including text is vital. Without text, the snippet will show raw HTML starting with <html><h3>, potentially confusing recipients and possibly appearing as spam.

Attachments

  • Attachments are passed as Base64 strings

  • There are 2 types of dispositions for adding attachments to the email : inline and attachment

Adding Attachments : Inline

  • The attachments that are part of the email body

  • The attachment is referenced to the email body by using unique identifiers cid and content_id

  • Their JSON input has the following things:

  • filename — The file name that will be used to represent the attachment in the email

  • type: MIME Type of the file

  • disposition: “inline” (Fixed)

  • content_id: the unique id to reference it inside the body of email

  • content: Base64 encoded string of the attachment

Adding Attachments : Attachments

  • The attachments that are not part of the email body

  • Their JSON input has the following things:

  • filename — The file name that will be used to represent the attachment in the email

  • type: MIME Type of the file

  • disposition: “attachment” (Fixed)

  • content: Base64 encoded string of the attachment

Let’s take an example to understand this better

Let the request be:

Let the final updated code be:

With that you have everything ready, once you send the request an email should be received by the emails mentioned in either of the to, cc or bcc list.

Related Blog(s)

Connect With Me

Connect with me for a productive discussion, valuable consultation, or exciting collaboration. I am eager to engage with you and explore new ideas, strategies, and opportunities. Reach out to me today and let's embark on a journey of shared success.