Send and Read Emails Using Node-Red and the Nylas APIs

Learn how to send and read emails without a single line of code using the visual low-code tool Node-Red and the Nylas APIs.

Node-Red

If you’re a regular reader of our DevRel blogs, you might know that we had published several blogs on how to send and read emails using Nylas SDKs for languages like Python, Ruby, NodeJS and Java. We also have talked about using PHPC#, and Rust.

Today, we’re going to learn how to send and read emails using Node-Red, a low-code programming environment based on NodeJS, that is mostly used for IoT scenarios.

Installing Node-Red

We can use npm to install Node-Red by typing the following in the terminal window:

$ sudo npm install -g --unsafe-perm node-red

If you’re on Windows, don’t use sudo.

Once installed, we can simply call it from the terminal like this:

$ node-red
Launch Node-Red

And it will run the editor on the following address:

http://127.0.0.1:1880/

The Node-Red environment

Being a visual environment, we can simply drag and drop elements into the canvas, and connect them via wires.

Node-Red flow

Installing Nodes

In Node-Red, we can install additional Nodes that will help us in making our work easier.

We’re going to install sysmessage, a node that will allow us to create notifications in a simple and easy way.

First, we need to press the menu button on the right hand side.

Deploy nodes

Then select Manage Palette.

Manage palette

Then, on the Install tab, type sysm (to bring up autocompletion) on the search box and click on Install.

sysmessage package

Once installed, a new node will appear on the left section on our canvas.

sysmessage installed

Sending an email using Node-Red

Now, we’re ready to send our email with Node-Red, later on, we’re going to read emails.

First, we’re going to select an Inject node and drag it into the canvas.

inject node

With a double click, we can access its properties. We want to delete the msg.topic message.

define msg.topic

We need to choose timestamp and change it for JSON.

JSON payload

Then, select the three points to expand the editor.

Expands JSON editor

And paste the following JSON code.

{
   "subject": "Hello from Node-Red",
   "to": [
       {
           "email": "devrel@nylas.com",
           "name": "Nylas' DevRel Team"
       }
   ],
   "body": "This email was sent using Node-Red and the Nylas APIs."
}

Now, we’re going to drag an http request node.

http request done

Here, we need to change the Method to POST and pass the Nylas Send Endpoint which is https://api.nylas.com/send. Then, we need to tick on Use authentication and select bearer authentication and pass our access token. Lastly, we need to add a couple of headers, Accept and Content-Type.

Configure HTTP Request

Once that’s done, we need to drag and add the sysmessage node and configure it like this; Choose the type of alert, OSX Alert in this case, and add an appropriate title. Leave the rest as it is.

Configure Sysmessage

Connect all the nodes with wires so it will look like this:

Send email layout

Or, you can simply copy and paste the following JSON code.

To import this, go to the menu and choose import.

Import send email code

Paste the JSON code and press Import.

JSON Email Send Code

We should have all nodes ready to be pasted. Once we finish, we can press the Deploy button, which is located next to the menu button.

Deploy nodes

Once deployed, we just need to press the Inject note in order to send the email and get the notification message.

Click inject send email

Also, we will get the JSON response back from calling the Nylas API.

Send email response

With minimal effort and no code, we sent an email using Node-Red and the Nylas APIs.

Reading Emails using Node-Red

Send emails was simple and almost straightforward, but when it comes to read emails, it gets a little bit complicated. No worries, it will be fun as Node-Red makes things easy.

First, we’re going to create an http in node.

http in node

And configure it like this:

The method is GET, the URL will include /:limit which is going to how many emails we want to fetch and the Name is actually optional.

define GetEmails with limit

We can also add an extra http in node and get the URL but without the /:limit as this will help us to define a default page.

define GetEmails without limit

We’re going to add a Change node. This will allow us to read the /:limit parameter from before.

change node

The name is optional, but the SET and to the value are very important. We’re getting the parameter value and assigning it to the payload.

change payload

This parameter is going to be a String, but we need an Integer. So, we need to add a Function node and type the following:

function node
if (msg.payload == null){
   msg.payload = "5";
}
msg.payload = Number(msg.payload);
return msg;

This code will simply check if we are passing or not the :limit parameter and if we’re not, pass 5 as the limit, then it will convert the String parameter to an Integer parameter.

to Number function

The next step will be to add an http request node. It will be the same as Sending an email but the URL would be https://api.nylas.com/messages?in=inbox&limit={{{payload}}}  and the return will be a parsed JSON object instead of a UTF-8 String.

Define read email as JSON

On the URL, https://api.nylas.com/messages?in=inbox&limit={{{payload}}} the in=inbox part means that we’re reading mail coming from the inbox only, and the limit={{{payload}}} means that the amount of emails that we want to fetch would be the same as the number we used as parameter.

We’re going to get back an array of emails, and we need to access them one by one, so our best choice is to use the split node:

split node to split the array of emails

This node will grab each item in the array of emails. What we want to do is to change the format of the date field, as it is currently displayed as Epoch (number of seconds since January 01, 1970) and we want something more human readable. The best thing is that we don’t need to modify any property of this node; we can use it just as it comes.

This is the code for the function:

msg.payload.date = new Date(msg.payload.date * 1000).toLocaleString();
return msg;
Change date format

Once we’re done fixing the dates, it’s time to make the elements an array again, and for that we need to use the join node, which in the same manner as split, can be used without any further modifications.

join node

Once the array is ready and back in place, we need to display the information from the emails, and for that we can use the template node. This node uses the mustache system, which is a logicless template engine for creating dynamic content. In other words, it generates HTML code by using tags that are replaced by code.

Template node to display emails inbox

This is the code that we will need to use:

<html>
   <head>
       <script src="https://cdn.tailwindcss.com"></script>
   </head>
<body>
<h1 class="font-black" style="text-align:center">Inbox</h2>
<table class="table-auto">
   <tr>
       <th>Subject</th>
       <th>From (Name)</th>
       <th>From (Email)</th>
       <th>Date</th>
       <th>Snippet</th>
   </tr>
   {{#payload}}
   <tr>
   <td>
   {{subject}}
   </td>
   <td>
   {{from.0.name}}
   </td>
   <td>
   {{from.0.email}}
   </td>   
   <td>
   {{date}}
   </td>   
   <td>
   {{snippet}}
   </td>
   </tr>
   {{/payload}}
</table> 
</body>
</html>

This will simply create a table, loop each record of the array and print out the subject, name and email of sender, date and the snippet (email preview).

Define payload template

The last piece is the http response node which will allow us to see the template on the web browser.

http response node

We don’t need to change anything on this node.

Once we click on Deploy, we can start testing.

Our canvas should look like this one:

Read emails layout

And of course, we can import the following JSON code.

On our browser of choice, we need to navigate to:

http://localhost:1880/GetEmails

And we will get the first five emails in our inbox:

Read first five emails

And we can also pass parameters, so calling it this way:

http://localhost:1880/GetEmails/1

This will fetch just one email.

Read just one email

We can choose any number from one to one hundred (as that’s currently the fetch limit).

Read three emails

And that’s it. Hope you like this one. Send and read emails using Node-Red and the Nylas APIs is both fast and easy.

If you want to learn more about email management with Nylas, visit our documentation page Email API Overview.

You can sign up Nylas for free and start building!

Don’t miss the action, watch our LiveStream Coding with Nylas:

You May Also Like

Transactional Email APIs vs Contextual Email APIs
Best email tracker
Find the best email tracker and elevate your app’s email game
How to create and read Webhooks with PHP, Koyeb and Bruno

Subscribe for our updates

Please enter your email address and receive the latest updates.