DEV Community

Cover image for Send and Read Emails Using Node-Red and the Nylas APIs
Alvaro (Blag) Tejada Galindo for Nylas

Posted on • Originally published at nylas.com

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

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
Enter fullscreen mode Exit fullscreen mode

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

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

$ node-red
Enter fullscreen mode Exit fullscreen mode

Running Node-Red

And it will run the editor on the following address:

http://127.0.0.1:1880/
Enter fullscreen mode Exit fullscreen mode

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

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

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

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."
}
Enter fullscreen mode Exit fullscreen mode

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

http request node

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.

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

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

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;
Enter fullscreen mode Exit fullscreen mode

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}}}
Enter fullscreen mode Exit fullscreen mode


 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}}}**
Enter fullscreen mode Exit fullscreen mode


the in=inbox part means that we’re reading mail coming from the inbox only, and the

**limit={{{payload}}}**
Enter fullscreen mode Exit fullscreen mode


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;
Enter fullscreen mode Exit fullscreen mode

Change the 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will fetch just one email.

Read just one image

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. 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.

Watch the Coding with Nylas episode 18 - Send and Read emails using Node-Red.

Top comments (1)

Collapse
 
steve-lebleu profile image
Steve Lebleu

Powerful, thanks for the sharing. Should be challenged against MOM's architectures, but it's interesting to know.

Speaking about email sending, I'll do my pub: there is a little package to deal with email sending in node.js with flexibility and simplicity: github.com/steve-lebleu/cliam