Skip to content
Skip to main content

Clean email messages and extract body text

Last updated:

The Clean Messages endpoint removes extra information from messages (for example, quoted replies, signatures, and images) and returns only the body text that you need. Cleaned messages are well suited to training AI and machine learning models with email data. You can also use them to trigger automations, create chat-like views of threads, and more.

When you make a request to the Clean Messages endpoint, Nylas processes up to 20 messages and returns each one with its cleaned body text in the conversation field. Nylas always removes extraneous information such as HTML <script> and <style> tags. You can use the following configuration parameters to further specify the information Nylas returns:

  • ignore_links: When true, removes links in the message or signature. Defaults to true.
  • ignore_images: When true, removes images from the message or signature. Defaults to true.
  • images_as_markdown: When true, Nylas returns images as Markdown links. Defaults to true, or false for Agent Accounts.
  • ignore_tables: When true, removes tables from the message or signature. Defaults to true.
  • remove_conclusion_phrases: When true, removes phrases such as “Best” and “Regards” in the signature. Defaults to true.
  • html_as_markdown: When true, converts the message to markdown. Defaults to false.

You set each of these options on a per-request basis, so no need to worry about deciding how you want all of your cleaned messages to look. For more information, see the Clean Messages references.

Raw message bodies contain quoted replies, signatures, tracking images, and HTML markup that add noise to a training dataset. The Clean Messages endpoint returns only the text that the sender wrote in each message, which you can use to do the following tasks:

  • Build datasets to train or fine-tune machine learning models.
  • Generate embeddings for search and retrieval.
  • Pass email content to a large language model (LLM) as context.

Set html_as_markdown and images_as_markdown to true to keep headings and lists in Markdown format. To collect cleaned text as new messages arrive, subscribe to the message.created.cleaned webhook trigger.

You train models with the cleaned text in your own systems. Nylas doesn’t use your messages or the cleaned text to train models.

How Nylas handles message data when cleaning

Section titled “How Nylas handles message data when cleaning”

Nylas cleans messages when you make the request and returns the result in the response. It doesn’t cache or store the cleaned text, so each request processes the message again. If you need the same cleaned text more than once, store it in your application. A single request can include up to 20 message IDs.

Cleaning runs on Nylas infrastructure in the data region of your application: U.S. (Iowa) or Europe (London). Nylas doesn’t send message content to third-party services to clean it, and the endpoint doesn’t use generative AI or large language models (LLMs).

Nylas removes HTML, quoted replies, and forwarded content with fixed rules. When remove_conclusion_phrases is true, it also uses a pre-trained classifier to detect signatures and sign-offs. The classifier runs on Nylas infrastructure. Nylas doesn’t use your messages or the cleaned text to train this classifier or any other model.

To follow along with the samples on this page, you first need to sign up for a Nylas developer account, which gets you a free Nylas application and API key.

For a guided introduction, you can follow the Getting started guide to set up a Nylas account and Sandbox application. When you have those, you can connect an account from a calendar provider (such as Google, Microsoft, or iCloud) and use your API key with the sample API calls on this page to access that account’s data.

You’ll also need to set up a provider auth app (Google or Microsoft Azure) and connector with at least the following scopes:

  • Google: gmail.readonly
  • Microsoft: Mail.Read
  • EWS: ews.messages

Make a Clean Messages request that includes the IDs of up to 20 messages. Nylas returns the cleaned message in the conversation field and any images in the attachments object.

Before and after Clean Message request

Before

Nylas v3

After

Unsubscribe \n\n \nHi there,\n\nNylas API v3 is now generally available! The new infrastructure introduces: \n\n * Instant email and event functionality, boosting performance, security, and scalability.\n * Simplified integrations such as API keys for auth, enhanced notifications, Microsoft Graph, and more to streamline engineering efforts even further.\n * New email features such as Bounce Detection, Smart Compose, Scheduled Send, and custom domain for email tracking links to improve email deliverability and efficiency.\n\nCheck out Nylas docs and join our webinar on Feb 15 to learn more, and start\nmigrating today!\n\nHave questions about migrating? Contact your dedicated Nylas customer success\nmanager (CSM) or email customeronboarding@nylas.com. \n\n© 2024 Nylas Inc. All rights reserved.\n\nPrivacy Policy | Copyright | Unsubscribe\n\n2100 Geng Rd. #210, Palo Alto, CA 94303

When you set html_as_markdown and images_as_markdown to true in your request, Nylas formats the cleaned message as Markdown and returns it in the conversation field. For Agent Accounts, Nylas returns the Markdown in the email_as_markdown field instead.

When you specify a message that contains images in a Clean Message request, you can choose how Nylas handles the images. If you want to return them, set ignore_images to false. If you want to return the image tags as Markdown links, set images_as_markdown to true and ignore_images to false.

Nylas returns inline images in the conversation field as part of the cleaned message, and includes their content ID (cid):

  • Inline image: "conversation": "<img src='cid:1781777f666586677621' />\n\nImage from Gmail"
  • Inline image as Markdown: "conversation": "![Nylas logo](cid:1781777f666586677621)\n\nImage from Gmail"

Nylas returns some inline images with a link to the original source, if available. If the link isn’t available, Nylas returns the cid.

Nylas doesn’t return image attachments as part of a cleaned message. You can find information about attached images in the attachments object.

To download an image that was included in a cleaned message, make a Download Attachment request that includes the attachment and message IDs. Nylas returns the image as a binary data blob.

curl --compressed --request GET \
--url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/attachments/<ATTACHMENT_ID>/download?message_id=<MESSAGE_ID>' \
--header 'Authorization: Bearer <NYLAS_API_KEY>'

Instead of polling the Clean Messages endpoint, you can subscribe to the message.created.cleaned webhook trigger to receive cleaned message content automatically when new messages are synced. The webhook payload includes the cleaned markdown in the body field, plus a cleaning_status field indicating whether cleaning succeeded.

Subscribing to message.created.cleaned does not suppress message.created notifications. If you subscribe to both triggers, Nylas sends two separate notifications for each new message: one with the original HTML body and one with the cleaned content.

To use cleaned message webhooks, configure Clean Conversation settings for your application and subscribe to the message.created.cleaned trigger. For more information, see the message.created.cleaned notification reference.

Keep the following things in mind as you work with the Clean Message endpoint:

  • Nylas currently detects English-language conclusion phrases only when you set remove_conclusion_phrases to true.
  • Nylas removes any reply and forward content from the message, and returns only the latest message in the thread.