- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
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 PHP, C#, 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.
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
And it will run the editor on the following address:
http://127.0.0.1:1880/
Being a visual environment, we can simply drag and drop elements into the canvas, and connect them via wires.
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.
Then select Manage Palette.
Then, on the Install tab, type sysm (to bring up autocompletion) on the search box and click on Install.
Once installed, a new node will appear on the left section on our canvas.
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.
With a double click, we can access its properties. We want to delete the msg.topic message.
We need to choose timestamp and change it for JSON.
Then, select the three points to expand the editor.
And paste the following JSON code.
{ "subject": "Hello from Node-Red", "to": [ { "email": "[email protected]", "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.
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.
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.
Connect all the nodes with wires so it will look like this:
Or, you can simply copy and paste the following JSON code.
To import this, go to the menu and choose import.
Paste the JSON code and press Import.
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.
Once deployed, we just need to press the Inject note in order to send the email and get the notification message.
Also, we will get the JSON response back from calling the Nylas API.
With minimal effort and no code, we sent an email using Node-Red and the Nylas APIs.
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.
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.
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.
We’re going to add a Change node. This will allow us to read the /:limit parameter from before.
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.
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:
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.
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.
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:
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;
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.
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.
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).
The last piece is the http response node which will allow us to see the template on the web browser.
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:
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:
And we can also pass parameters, so calling it this way:
http://localhost:1880/GetEmails/1
This will fetch just one email.
We can choose any number from one to one hundred (as that’s currently the fetch limit).
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:
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.