How Nylas Made IMAP & SMTP as Simple as Chat

Nylas is the expert at IMAP and SMTP integrations so you don’t need to be. Learn how we’ve made email integrations as simple as chat.

Ben Lloyd Pearson | February 1, 2021

Ask any developer who has worked with IMAP or SMTP, and they’re likely to have at least one story filled with strife and despair. It’s no wonder why: email was one of the first consumer products to emerge from the internet. Its history stretches back to a time where communications needs were drastically simpler than they are now. Somehow, email has stood the test of time and survives as an essential communications tool even today. But its legacy brings a substantial amount of baggage that causes numerous headaches for anyone that needs to work with email.

What would it take to make working with an email server as easy as implementing basic chat functionality? Enter Nylas. This problem is one of many that we set out to solve with our Email API, and this article will show you how Nylas is the best option for your next IMAP and SMTP integration.

Want a PDF of this article?

Share it with a friend or save it for later reading.

Working With Basic Technologies

The first challenge developers face when working with IMAP and SMTP is the inherent low level of interactions that standard HTTP connections provide. HTTP requires you to pass commands as raw text, which can become extremely challenging when you need to construct even simple interactions. The mere act of reading the latest email from a user’s account on an IMAP server has multiple steps that involve authenticating on behalf of the user, identifying the inbox’s name, setting up a stateful session, and issuing fetch commands that indicate the exact data you want to extract. Each of these steps has stringent requirements.

Nylas solves this with our Email API that returns data in a clean JSON format via a simple REST integration. Nylas Hosted Auth makes it extremely easy to connect your users’ email inboxes to your app by auto-detecting 100% of email providers to provide the correct authentication flow for each user. After they’ve authenticated, Nylas gives your app an access token it can use to ingest email inbox data, send emails on behalf of the user, access file attachments, and manage their inbox with labels and folders.

Illustration of the Nylas Hosted Auth service that integrates with 100% of email, calendar, and contacts providers, including Google

Nobody’s Got Time For MIME

When you integrate directly with an IMAP or SMTP server, you will inevitably need to work with Multipurpose Internet Mail Extensions (MIME) data because it’s the standard form used by virtually every email provider. MIME is relatively complicated and requires you to create proper email headers and content type declarations and encode all data in an acceptable format: typically base64 encoded strings. If you make a mistake at any one of these steps, there is a good chance that your email will end up in the recipient’s spam folder or simply not get delivered at all.

Creating MIME data to send in an email is only half the struggle; if you want to ingest email data, you will need to comb through a substantial amount of unstructured data to parse the exact content you want. Simple fields like an email’s subject line, body content, participants, file attachments, and more are often buried under a mountain of unrelated raw text and encoded binary content. 

Nylas solves this problem by expressing email messages as simple, clean JSON that includes all of the data you’d expect to have available to you when building an email integration. Here is an example of how to create a new email draft with Nylas:

curl --location --request POST 'https://api.nylas.com/drafts/' -H 'Authorization: Bearer <access_token>' \
-d '{
    "subject": "Take a look at our new line of nuclear engines.",
    "body": "We recently released our newest nuclear engine models, check out the attached product catalog to learn more.",
    "from": [
        {
            "email": "[email protected]",
            "name": "Albert Einstein"
        }
    ],
    "to": [
        {
            "email": "[email protected]",
            "name": "Marie Curie"
        }
    ],
    "file_ids": ["f94ks04kf9"]
}'

Then, when you’re ready to send the draft you can make a request to the Nylas Send endpoint, passing the draft’s ID:

curl -X POST 'https://api.nylas.com/send' -H 'Authorization: Bearer <access_token>' \
-d '{
    "draft_id": "f8fjd94jkkw",
    "version": 0
}'

Nylas also represents email attachments, drafts, folders, and labels as JSON to make working with all aspects of a user’s email inbox as simple as possible.

Making Sense of Human Conversation

The most important emails in a person’s inbox are often email conversations they’ve had with other people. It’s common for a conversation thread to span across multiple email messages that include various senders and recipients. IMAP doesn’t have built-in support for identifying all emails that are a part of the same thread, forcing developers to build complex tracing functions that use the reply-to field to reconstruct an email conversation history. 

Then, once you’ve managed to collect all of the messages that are a part of a single thread, you still need to parse the actual conversation text from the email body, which can contain a substantial amount of superfluous data. An email reply can include images, copied text from previous messages in the thread, multiple email signatures, and more. The task of identifying a small handful of sentences someone sent in an email body is akin to searching for a needle in a haystack, and the results are often inconsistent.

Nylas has a few significant features that make working with human conversations much more straightforward. The first is the Threads endpoint, which automatically groups messages into threads. With the Threads endpoint, developers can request every message that’s a part of a threaded conversation with a single API request. The next example shows a response to a GET request for a thread:

{
    "object": "thread",
    "subject": "Take a look at our new line of nuclear engines.",
    "messages": [
        {
            "object": "message",
            "subject": "Take a look at our new line of nuclear engines.",
            "body": "<div dir=\"ltr\">We recently released our newest nuclear engine models, check out the attached product catalog to learn more.<br clear=\"all\"><div><br></div>-- <br><div dir=\"ltr\" class=\"gmail_signature\" data-smartmail=\"gmail_signature\"><div dir=\"ltr\"><div><div dir=\"ltr\"><div><div><font size=\"4\" face=\"trebuchet ms,sans-serif\">Albert</font><font size=\"4\"> Einstein</font><br></div></div>Particle Tech Inc.<br></div></div></div></div></div>",
            "date": 1610400855,
            "files": [ "7dh29ka03" ],
            "from": [
                { "email": "[email protected]", "name": "Albert Einstein" }
            ],
            "to": [
                { "email": "[email protected]", "name": "Marie Curie" }
            ],
        },
{
            "object": "message",
            "subject": "Re: Take a look at our new line of nuclear engines.",
            "body": "<div dir=\"ltr\">These look really awesome! Let's set up a time to chat.<br clear=\"all\"><div><br></div>-- <br><div dir=\"ltr\" class=\"gmail_signature\" data-smartmail=\"gmail_signature\"><div dir=\"ltr\"><div><div dir=\"ltr\"><div><div><font size=\"4\" face=\"trebuchet ms,sans-serif\">Marie</font><font size=\"4\"> Curie</font><br></div></div>Chief Scientist<br></div></div></div></div></div>",
            "date": 1611244437,
            "files": [ ],
            "from": [
                { "email": "[email protected]", "name": "Marie Curie" }
            ],
            "to": [
                { "email": "[email protected]", "name": "Albert Einstein" }
            ]
        },
    ],
    "first_message_timestamp": 1610400855,
    "has_attachments": true,
    "last_message_timestamp": 1611244437,
}  

In the example above, you might notice that the email bodies contain HTML and signature content that isn’t relevant to the discussion at hand. Fortunately, the Nylas Neural API makes it extremely easy to extract the conversation text from this content. The Categorizer endpoint identifies emails that are part of a human to human conversation. The Clean Conversations endpoint extracts the conversation text from the email body, leaving behind only the text related to the conversation hand. This results in exceptionally clean message objects that make an email conversation as simple as chat:

{
    "object": "thread",
    "messages": [
        {
            "object": "message",
            "category": "conversation",
            "conversation": "We recently released our newest nuclear engine models, check out the attached product catalog to learn more.",
            "from": {
                "email": "[email protected]",
                "name": "Albert Einstein"
            }
        },
        {
            "object": "message",
            "category": "conversation",
            "conversation": "These look really awesome! Let's set up a time to chat.",
            "from": {
                    "email": "[email protected]",
                    "name": "Marie Curie"
            }
        }
    ]
}

Build Great Features, Not Basic Connectivity

Most developers simply want to build great features quickly, not become experts at things like IMAP and SMTP connectivity. Nylas steps up to be the expert for you. Not only do we handle the complexities of integrating with IMAP and SMTP, but we also abstract away the differences for third-party email provider APIs like Gmail, Outlook  Exchange, and Office 365. If you’re ready to build your email integration, sign up for a free trial today.

Ben Lloyd Pearson

Ben is the Developer Advocate for Nylas. He is a triathlete, musician, avid gamer, and loves to seek out the best breakfast tacos in Austin, Texas.