Email is still one of the most popular forms of textual communication, particularly among corporations for notifications, outreach, etc. through their business product. SendGrid is one of the most widely used email APIs to integrate email services into business products. SendGrid makes it simple to send welcome emails, password reset emails, communication updates, and other types of emails, as well as new product feature announcements. In the transactional email space, SendGrid is probably the industry leader. In this blog, we will do a small implementation on how to integrate and configure SendGrid API with a Node.js application.
In this blog we will cover:
- What is SendGrid API?
- Benefits of using SendGrid API
- Why choose SendGrid over other solutions to send emails
- Popularity of SendGrid API
- Hands-on
- Conclusion
What is SendGrid API?
The Email API by developers, for developers!
SendGrid is a service that allows businesses to send transactional and marketing emails to their consumers. It offers scalability, dependability, and deliverability, all of which are critical for any business.
In the transactional email space, SendGrid is arguably the industry leader. It is a cloud-based email service provider. A popular email service provider that provides services for sending, receiving, tracking, and storing emails. Many developers use SendGrid to send emails from their Node.js applications.
The SendGrid platform enables businesses to scale their infrastructure and provide high performance at a low cost, allowing them to scale their email delivery to clients through the cloud.
Over 30 million businesses throughout the world have been assisted by SendGrid in enhancing their customer relationships by offering tools that include transactional emails, SMS messages, and marketing automation.
SendGrid provides over 700 open-source projects and over 100 Fortune 500 companies with web, mobile, API, integration, and services.
Benefits of using SendGrid API
- You can only send a specific number of emails per day if you use Nodemailer with Gmail.
- You also don’t have to set up your own SMTP server.
- SMTP does not guarantee delivery, which means that emails may or may not be sent.
Why choose SendGrid over other solutions to send emails?
Any programming language, including Node.js, may send emails in a variety of ways. For example, we could use software like Nodemailer, which supports several means of transport such as SMTP, Sendmail, and even Amazon SES.
The issue with using an arbitrary SMTP or Sendmail server is that the emails would almost certainly be categorized as spam and end up in our clients’ rubbish folders. This is not something we want to happen.
Here are a few benefits that you get by using SendGrid:
- Send mass emails with excellent deliverability rates using this powerful email service.
- Provides built-in email templates that are interactive and eye-catching, as well as the ability to change them.
- You can track the real-time performance of emails with SendGrid by looking at metrics such as bounce rates, unsubscribers, delivered emails rate, unique reads/opens/clicks, and more.
- Excellent client service
- Sendgrid has so many technologies integrated that it allows you to centralize your marketing activities. The integration of the API with the various tools is really powerful.
Popularity of SendGrid API
Hands-on
Required Installations
- Node.js: It is an open-source server environment that acts as a backend allowing us to run JavaScript on the server to execute the JavaScript code outside the web browser.
- Express.js: It is also known as Express and plays the role of being a backend web application framework for Node.js for building web applications and APIs.
- Git Bash: It is a Linux-based version source control management system for Windows that provides us with the flexibility to type in different git commands.
- SendGrid: It is a customer communication platform that can be easily integrated with different web applications for sending out bulk transactional and marketing emails to the target audiences.
Now let’s begin with a small implementation…
- Considering a use case wherein the team of Workfall is currently facing challenges to use the most trustworthy, cost-effective, flexible, smooth, and efficient API to send the Partner emails to the required target audiences. To eliminate the process of sending the partner emails manually, they wish to automate the process making it easy for the team to focus on other important tasks. The team finally decides to proceed with the SendGrid API and build a Node.js application to send emails on hitting the URL to automate the mail-sending process.
- To do the same, we will create a SendGrid account followed by creating a sender’s identity and an API key to integrate it within the application. We will then set up the code base building a node.js application integrating the SendGrid account to the application using the Access Key. To test out the connection, we will send a test email and on success, we will make the required changes in the code base followed by triggering the application on hitting the URL via the postman application to automate the process of sending the partner emails to the target audiences.
Navigate to the official site of SendGrid. Click on Start for Free.
Entire an email address and a password for the account and click on Create Account.
Before proceeding, you will have to fill in the details and click on Create Account.
On the main Dashboard, click on the Setup Guide.
Expand the create a sender identity. Click on sender authentication.
Create a new Sender to create a newly verified identity. Click on create and verify the identity.
On the web page, click on choose below Web API.
Click on choose besides Node.js.
Enter a name for the API key Workfall-Email-Sender-Key. Click on Create Key.
On success, you will see the screen as shown below with the newly created API key.
You can note the further steps to create a simple mail sender API and the steps to install the same.
Lastly, once integrated, we can test the integration.
Create a new folder on your local machine.
Right-click and select Git Bash Here.
Run the below command to initiate a directory.
A new package.json file will be created.
Use the below command to install express and SendGrid mailer packages.
On success, you will see the screen as shown below.
The new files will be created as required with the node_modules directory installed.
Open the directory in a code editor.
Check the package.json file to verify the package installations.
Create a new file with the name app.js in the folder structure.
Paste the below code into the app.js file. This is the server file that imports express and starts the server on port 3000.
Run the following command to start the node.js app.
Verify if the server is up and running on port 3000.
Create a new folder with the name templates that will contain the template of the email to be sent. Create a new file emailTemplate.js with the code of the body and the subject of the email.
The below code will import the SendGrid package using the Access Key that we created above. It will contain a function to send an email as per required and will export the same function to the file that will give it a call. In place of ACCESS_KEY, enter the key that we created above. In place of receivers’ and senders’ email addresses, enter in the email of the verified identities. You can then replace the subject and the body of the email as required.
Now, we will create a route file sendAnEmail.js that will trigger the function sendMail in the emailTemplate.js file.
The below code will import the express library, and create a new route to hit the URL on sendAnEmail thereby calling the sendMail function. The user variable will take input of the email and name of the user. On success, it will print the message on the console and if there’s an error, it will catch the error and print it.
Now, navigate to the app.js file and import the route that we created above. We will use that route so that when the server is started, hitting the URL will trigger the route to call the sendMail function.
Now initiate the below command to trigger the node application.
On success, the server will be up and running on port 3000.
Hit the URL on the API sendAnEmail using the below URL to trigger the mailer application.
On success, you will see the Email successfully sent the message as shown in the image below.
You can then navigate to your mail account and check the received email. This will be the subject of the mailer.
The following will be the body of the mail.
Now we’ll see how we can integrate and send an HTML email body rather than a normal text body. To do so, make the following changes as shown in the image below for the sendMail function.
Open the sendAnEmail.js router file and make the required changes for success and error messages.
Run the below command to start the server.
On success, the server will be running on port 3000 successfully.
Now, open the Postman application. To send API request. Click on the plus icon.
Enter the URL as http://localhost:3000/sendAnEmail to trigger the API to send an email. Now click on the Body section below.
In the editor area, click key-value pairs for name and email. Configure the email that is a verified identity in your SendGrid account and the name as required.
Click on the type and change the type to JSON. Once done, hit Send.
On success, you will see the success message as shown in the image below.
On success, you will see the email received in your mailbox as per the designed template.
To clone the code repository, use the link with the git clone command in a directory on your local machine: https://github.com/workfall/SendGrid-Code.git
Conclusion
In this blog, we have integrated and configured SendGrid API with the Node.js application. In hands-on, we implemented a solution to resolve a use case for automating the process of sending out emails to the target audiences of Workfall.
To do the same, we created a SendGrid account followed by creating a sender’s identity and an API key to integrate it within the application. We then created a setup for the code base building a Node.js application integrating the SendGrid account to the application using the Access Key.
To test out the connection, we sent out a test email, and with success, we made the required changes in the code base followed by triggering the application on hitting the URL via the Postman application to automate the process of sending out the partner emails to the target audiences.
We will come up with more such use cases in our upcoming blogs. Stay tuned to keep getting all updates about our upcoming new blogs on different technologies.
Meanwhile…
Keep Exploring -> Keep Learning -> Keep Mastering
This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.