Open In App

Sending bulk emails in Node.js using SendGrid API

Last Updated : 13 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

What is SendGrid API? 
SendGrid is a platform for sending transactional and marketing emails to the customers. It provides scalability, reliability, and deliverability which is an important issue for an organization.
Benefits of using SendGrid API:

  • If you are using Nodemailer with Gmail then you can send only certain amount of emails per day.
  • Also, there is no need to set up your own SMTP server.
  • SMTP doesn’t provide the deliverability, i.e. email may or may not be sent.

Steps to send emails using SendGrid API:

  1. Setting up an API key: 
    • Go to sendgrid dashboard and click on create api key button.
       
    • Name the API-key as you want, for this tutorial we’ll name it node-mail
    • Copy the API-key as for security reasons you may not be able to see it again.

  2. Setting up a node.js app:
    • Create an empty NPM package using the command. (The –y flag which is passed is used to use the defaults in the generator instead of asking questions)
       
      npm init -y
    • Create a file named index.js and add a boiler plate code.

      index.js




      // Importing http library
      const http = require("http");
        
      const PORT = 3000; // Defining PORT
        
      http.createServer((req, res) => {
        
          // Output Hello World on HTML page
          res.write("<h1>Hello World!</h1>");
          res.end();
      })
        
      // Initializing server
      .listen(PORT,() => console.log(`Server running on PORT : ${PORT}`));

      
      

    • Now run the code by using node index command and go to 127.0.0.1:3000 link. You will see the output.
    • And in console 
       
  3. Installing SendGrid library: Install the SendGrid library by running the below command
    npm i @sendgrid/mail
  4. Sending emails by using library:

    javascript




    const http = require("http");
      
    const PORT = 3000;
      
    http.createServer((req, res) => {
      
        // Initializing sendgrid object
        const mailer = require("@sendgrid/mail");
      
        // Insert your API key here
        mailer.setApiKey("<your-api-key>");
          
        // Setting configurations
        const msg = {
          to: ["youremail@gmail.com", "your.second.email@gmail.com"],
          from: "noreply@example.com",
          subject: "Message sent for demo purpose",
          html:
            "<h1>New message from Geeksforgeeks</h1> 
               
    <p>Some demo text from geeksforgeeks.</p>
    "
        };
      
        // Sending mail
        mailer.send(msg, function(err, json) {
          if (err) {
            console.log(err);
      
            // Writing error message
            res.write("Can't send message sent");
          } else {
      
            // Writing success message
            res.write("Message sent");
          }
        });
      
        res.end();
      })
    .listen(PORT, () => console.log(`Server running on PORT : ${PORT}`));

    
    

Now run the app again by using node index and go to 127.0.0.1:3000 in the browser and check both of your emails, you’ll see an output like below.



Similar Reads

Sending bulk SMS in Node.js using Twilio
SMS is a common method of sending short messages between cell phones, but these SMS can be sent to multiple users at a time using Twilio notify service. Sending bulk SMS to users at a time is possible using Twilio. Introduction: It's easy to get started and easy to use. It is widely used for sending bulk SMS. User can send SMS to desired mobile num
2 min read
Sending Emails Using API in Flask-Mail
Python, being a powerful language don’t need any external library to import and offers a native library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. This article revolves around how we can send bulk customised emails to a group
3 min read
Sending SMS using NEXMO API in Node.js
Introduction: SMS is a common method of sending short messages between cell phones, but these SMS can be sent using API in Node.js. Now there are lots of API in the market for sending SMS like Twilio, Exotel, etc to the user but the popular among them is Nexmo. Features of NEXMO: Integrating this module in your code is very simple and efficient. Us
2 min read
Why are HTTP cookies used by Node.js for sending and receiving HTTP cookies?
The HTTP protocol is one of the most important protocols of the Application layer of the OSI model. The main use of this protocol is for sending hypertext information to the client to the server and server to the client to communicate on the World Wide Web. But, the HTTP protocol is a stateless protocol which means that this protocol cannot maintai
5 min read
How to get emails using PHP and IMAP ?
Reading emails from the Gmail account using PHP will be an enriching task for web developers for its simplicity of code through IMAP (Internet Message Access Protocol). Sometimes there can be a requirement in web projects or from a client that needs the complete management of inbox emails or access to email contents from his Gmail account. This fea
5 min read
PHP | Sending mails using mail() function
PHP is a server side scripting language that is enriched with various utilities required. Mailing is one of the server side utilities that is required in most of the web servers today. Mailing is used for advertisement, account recovery, subscription etc. In order to send mails in PHP, one can use the mail() method. Syntax: bool mail(to , subject ,
2 min read
Sending HTTP Request Using cURL Set-1
Whenever we are dealing with HTTP requests, cURL simplifies our tasks to a great extent and is the easiest tool to get our hands dirty on. cURL: It stands for "client URL" and is used in command line or scripts to transfer data. It is a great tool for dealing with HTTP requests like GET, POST, PUT, DELETE, etc. Although it provides us with the supp
3 min read
Sending Push Notifications : Progressive Web Apps
We need permissions to send push notifications. Key Points : Notifications must be timely. Should be precise (contains all the information a user needs). Should be relevant to the user. Basic Example : An airline app must send push notifications such as "Your flight will be boarding from..." Browser that supports push notifications are : Chrome 44
3 min read
Explain the common methods of sending request to a server in jQuery
In this article, we will see the common methods of sending requests to a server by using the jQuery library. jQuery is an open-source JavaScript library jQuery simplifies HTML document traversing and manipulation, browser event handling, DOM animation, Ajax interactions cross-browser JavaScript development. Common methods of Sending Requests to the
3 min read
How to define the HTTP method for sending data to the action URL in HTML5?
In this article, we will learn how to define an HTTP method to send data to the action URL. The method attribute is used to indicate how form data can be sent to the given server in the action URL. The form-data may be sent using the GET or POST method depending on the requirement. The differences between the GET and POST method are defined below:
2 min read