Send and Read Emails Using Node-Red and the Nylas APIs
Tags: node, api, iot
Published: 2022-12-16 15:54:17

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](https://www.nylas.com/blog/how-to-send-emails-with-the-nylas-python-sdk/), [Ruby](https://www.nylas.com/blog/how-to-send-emails-with-the-nylas-ruby-sdk/), [NodeJS](https://www.nylas.com/blog/how-to-send-emails-with-nylas-node-sdk/) and [Java](https://www.nylas.com/blog/how-to-send-emails-with-the-nylas-java-sdk-dev/). We also have talked about using [PHP](https://www.youtube.com/watch?v=MteeGtWaK54&list=PLk-4iahO3b-xd42-RL4DJQyuEoFFYwTbC&index=6&t=735s),  [C#, and Rust](https://www.youtube.com/watch?v=7gqnMf15fAA&list=PLk-4iahO3b-xd42-RL4DJQyuEoFFYwTbC&index=7&t=836s).

Today, we’re going to learn how to send and read emails using [Node-Red](https://nodered.org), 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:

```bash
$ 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:

```bash
$ node-red
```


![Running Node-Red](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjioa270zgidg8n38ib5.png)

And it will run the editor on the following address:

```bash
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 example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpwqy75xqj3lw2prcfaj.png)

## 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](https://flows.nodered.org/node/node-red-contrib-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px2q397nfzqzu5erlgli.png)

Then select **Manage Palette**:

![Manage Palette](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td7b8hiozgg3nld3h6ir.png)

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

![sysmessage package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elm1vh5vzshxj3cnt6rn.png)

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


![sysmessage installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88o98tqt23g1ayvpv1ik.png)

## Sending an email

Now, we’re ready to send our email.

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


![Inject node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39bc0vpw9q94lplx8zjs.png)

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


![define msg.topic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m73gu064rqj1d9cgxoj.png)

We need to choose **timestamp** and change it for **JSON**:


![JSON payload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw60jvxs1wsy72v6i6lz.png)

Then, select the three points to expand the editor:


![Expands JSON editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icak8m00v9xuyghxlvfy.png)

And paste the following JSON code:

```JSON
{
   "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 node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkg4q35yubzhv34wa8bo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcuabgkwgz24oqoqpo2q.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmksmyluvgtgrw1ussgi.png)

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


![Send email layout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sm6b4aagwbxjiw7kz536.png)

Or, you can simply copy and paste the following [JSON code](https://gist.github.com/atejada/55ad2a61c57545750acf864deb896917).

Paste the JSON code and press **Import**.


![JSON Email Send Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj67cta6gm4qp8q1ojvb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5x3o5on4r75ptn0qpe0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j61ueuazvblqlr4l7uwl.png)

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


![Send email response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhn8tp7yqbi4lpiwhbh0.png)

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

## Reading Emails

Sending email was simple and almost straightforward, but reading email gets a little bit complicated. No worries, it will be fun.

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


![http in node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxrrgcyfhd84n74cdinc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpoynm2xln53orrygwmu.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ov2hy7mdvlxqd5zkqvl.png)

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


![change node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdb187eo18xbm2ek757x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/481rn36u5zbrf3xogmh0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvj891zv7jiorm1mc5cj.png)

```javascript
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umhpap3bk1h9scvh9ubt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z7132ywa8suwsio5vz7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuwi4qea90oqtojd9v82.png)

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:

```javascript
msg.payload.date = new Date(msg.payload.date * 1000).toLocaleString();
return msg;
```


![Change the format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ppdrpoe7m1rjz2a4mey.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jid2z9bdpyu9lugos8wb.png)

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](https://mustache.github.io) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zc6n3v8e7b4v8l1jehs2.png)

This is the code that we will need to use:

```html
<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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m93f3u28xff6nfao6v37.png)

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


![http response node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46zzwqxmnb74957jutle.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5avh1fhngn1te734knu.png)

And of course, we can import the following [JSON code](https://gist.github.com/atejada/a42461a3aafb929c5d04ecc9e7b43f40).

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

```bash
http://localhost:1880/GetEmails
```

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


![Read first five emails](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbyqcp2ui1e85zz08w35.png)

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

```bash
http://localhost:1880/GetEmails/1
```

This will fetch just one email.


![Read just one image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vxxigz3n25hlzgz6qpt.png)

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


![Read three emails](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57quncstomq7w1ewuem9.png)

And that’s it. Hope you like this one. Sending and reading 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](https://developer.nylas.com/docs/connectivity/email/).

Watch the [Coding with Nylas episode 18 - Send and Read emails using Node-Red](https://www.youtube.com/watch?v=zxsiXHAPffQ).