How to Read Email Inbox Data With Nylas Ruby SDK

Learn how you can use the Nylas Ruby SDK to read email inbox data and apply different filters with just a few lines of code.

Ruby

Programmatically reading unstructured email data is an important task that can be sometimes daunting. Thanks to the Nylas Connectivity APIs we can use different kinds of objects and filters that treat emails like first-class citizens, making this task easier.

To make things even easier for Ruby developers, we offer the Nylas Ruby SDK. Today we’re going to review how to read email inbox data using Ruby and the Nylas Ruby SDK.

Is your system ready?

If you already have the Nylas Ruby SDK installed and your Ruby environment is configured, then continue along with the blog.

Otherwise, I would recommend that you read the post How to Send Emails with the Nylas Ruby SDK where the basic setup is clearly explained.

Reading the Inbox with the Nylas Ruby SDK

Now we get to write some code! For my code editor, I’m going to use Geany, however, you can use the IDE of your choice.

I called this file ReadInbox.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Read your email messages and print them out
messages = nylas.messages
messages.each{ |message|
    puts(
        "Subject: #{message.subject} | "\
        "From: #{message.from.first.email} | "\
        "\n\n"
    )
}

We can execute this script from the terminal by typing:

$ ruby ReadInbox.rb

Which will give us output that looks like this:

Read messages

Now, we might notice we’re getting more than expected in the response. As we’re not specifying any particular source, we’re getting Inbox but also Sent, Spam, and Trash messages.

We can easily fix this. I called this file ReadOnlyInbox.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Read messagse only from the Inbox folder and print them out
messages = nylas.messages.where(in: ‘inbox’)
messages.each{ |message|
    puts(
        "Subject: #{message.subject} | "\
        "From: #{message.from.first.email} | "\
        "\n\n"
    )
}

We can execute this script from the terminal by typing:

$ ruby ReadOnlyInbox.rb

Which will give us output that looks like this:

Read just the inbox

If we want to only read the first 3 emails, we can just add .limit(3). I called this file ReadOnlyThreeInbox.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Read the first 3 messages of the Inbox folder and print them out
messages = nylas.messages.where(in: 'inbox').limit(3)
messages.each{ |message|
    puts(
        "Subject: #{message.subject} | "\
        "From: #{message.from.first.email} | "\
        "\n\n"
    )
}

We can execute this script from the terminal by typing:

$ ruby ReadOnlyThreeInbox.rb

Which will give us output that looks like this:

Read the first three messages

Above, we looked at reading emails from the account, specifying the source folder with .where(), and setting a limit on the number of messages to read with .limit(). There’s a lot more you can configure when reading messages. Have a look at our documentation to see what else you can do.

Reading threads using the Nylas Ruby SDK

A thread is a message shared between 2 or more people. It consists of the original message with its corresponding replies.

For this example, we’re going to limit the results to just one thread. I called this file EmailThreads.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Read the first thread
threads = nylas.threads.limit(1)

# Print out the information
threads.each{ |thread|
    participants = thread.participants
    participants.each{ |participant|
        puts(
            "Subject: #{thread.subject} | "\
            "Participant: #{participant.name} | "\
            "Email: #{participant.email}"
        )
    }	
}

We can execute this script from the terminal by typing:

$ ruby EmailThreads.rb

Which will give us output that looks like this:

Read email threads

In this quick example, I’m emailing myself, so each participant has the same name, but the messages are coming from different email addresses.

Now, let’s say we want to search for the latest message sent by a particular email account. I called this file SearchEmail.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Search messages and get the first result
message = nylas.messages.search("from:alvaro.t@nylas.com").first
print("Subject: #{message.subject} | ")

# Are there attachments? Print them out
if message.files.length > 0
	attachs = ""
	print("Attachments: ")
	message.files.each{ |attachment|
		attachs += attachment.filename.to_s + ", "
	}
	puts(attachs.chomp(", "))
else
	puts("Attachments: None")
end

We can execute this script from the terminal by typing:

$ ruby SearchEmail.rb

Which will give us output that looks like this:

Search messages

We can easily perform the same search on threads by replacing messages with threads.

And that’s it. As you can see, by using the Nylas Ruby SDK, reading email inbox data becomes an easy task. If you want to learn more, visit our Documentation Page.

Tags:

You May Also Like

How to create and read Webhooks with PHP, Koyeb and Bruno
How to create a scheduler with Python and Taipy
How to create and read Google Webhooks using Kotlin

Subscribe for our updates

Please enter your email address and receive the latest updates.