Free email with SendGrid, Here’s how to set it up with NodeJS.

Hi, hope you are doing well. It is the time to learn SendGrid configurations with Node JS. In this article, you will learn how to send mail using SendGrid mail alert service.

It is totally free. All you need is, you should have a SendGrid account. Let’s see what are the steps need to be completed.

  1. Create Send Grid account
  2. Configure sender authentication in Send Grid
  3. Create API key
  4. Create mail sending app in Node Js.
  5. Test app.

1. Create SendGrid Account

  1. Go to SendGrid.
    • Click see plans and pricing.
    • Click ‘start for free’ on Free package.
    • Enter mail address and password then click create.
select free package

enter valid credentials to create account
  • Fill ‘Tell Us About Your Self’ form and click Get Started.
fill the required fields

Once you clicked on get started you will be redirected to Send Grid home page. Well, all done for the account creation.

2. Configure sender authentication

  • You will find the settings in the left navigation and click ‘sender authentication’.

select sender authentication
  • Click the ‘get started’ button for single sender authentication .
  • Fill the required fields and click create

create a sender
  • Make sure to enter from email address that you are going to use to send emails.

Well, you have successfully created a sender. Now we have to create our API key and App.

3. Create API Key

  • Click integration guide under Email API.
email API
  • Select Web API
choosing web API
  • Select node JS

select node.js
  • Enter your app name and click create. Then copy the code and keep it with you.
API Key

4. Create mail sending app in Node Js.

  • Create folder called APP and create SendGrid.js file inside APP folder.
  • Install @sendgrid/mail NPM package using ‘npm i @sendgrid/mail’.
  • Copy the sample code from send grid that they have given in their instructions.
  • Paste it into SendGrid.js.
  • Replace ‘process.env.SENDGRID_API_KEY’ this with your API key. You can use environment file for this. Here we are directly using API key.
  • Replace the email addresses with your ones. Make sure to use same email address for ‘from’ property that you have configured in sender authentication.
  • You can customize email body as your preference.

Here is the final code.

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey("API-KEY");
const msg = {
    to: '[email protected]', // Change to your recipient
    from: '[email protected]', // Change to your verified sender
    subject: 'Testing with SendGrid',
    text: 'Easy to do anywhere, even with Node.js',
    html: '<strong>It is send grid</strong>',
}
sgMail.send(msg)
    .then(() => {
        console.log('Email sent')
    })
    .catch((error) => {
        console.error(error)
    });

5. Test app

  • Finally, run the script using ‘node SendGrid.js’.
  • It will send the mail to relevant mail. Check the inbox of you provided mail for ‘to’ property.

Here is my one

test mail alert

If you do not get the mail to your inbox, please check the spam.

Why it is SPAM??

Well, it is not completely configured yet. That is the reason behind this spam alert. Once you configure send grid with your real application domain, this spam alert will disappear. For the testing purpose you can mark it as ‘not spam’ in the mail you received.

Thank you. I will get back to you with something new. 😉

Udara Chinthaka
Author: Udara Chinthaka

Total
1
Shares

Leave a Reply

Total
1
Share