Extract text from Email inbox

The whole message, not a link, since a private inbox has no download URL. Here's how a document actually gets from Email inbox to a txtfetch response.

The problem

An email message lives inside a private inbox, with no equivalent of a shareable file link. Reaching it always means calling a mail API or protocol with your own credentials first. ?url= has nothing to point at until you do.

POST the bytes, and why

How Email inbox fits the decision table, cited and dated
Pre-authenticated URL?No — a mailbox has no download URL at all
txtfetch pathPOST the bytes
Link lifetimeN/A
The trapThe Gmail API's raw field is base64url-encoded text, not a file. Decode it before you write the .eml file, or txtfetch reads garbage.

A mailbox has no public download URL for any message. Fetch the raw message yourself, then POST it to txtfetch as a file.

How it works

Call the Gmail API's users.messages.get with format=raw, or run an IMAP FETCH BODY[] over a logged-in connection. Either path returns the message as a complete RFC 822 document, headers and attachments included. Decode the Gmail API's base64url raw field into bytes, then POST that .eml file to txtfetch. txtfetch parses the message body and every attachment in the same call.

the script

A plain HTTP call against Email inbox's own REST API, then a plain call to txtfetch. No vendor SDK either side.

Python
import base64
import os

import requests

MESSAGE_ID = "18c8f2a1b3d4e5f6"
headers = {"Authorization": f"Bearer {os.environ['GMAIL_ACCESS_TOKEN']}"}

message = requests.get(
    f"https://gmail.googleapis.com/gmail/v1/users/me/messages/{MESSAGE_ID}",
    headers=headers,
    params={"format": "raw"},
)
message.raise_for_status()
raw_bytes = base64.urlsafe_b64decode(message.json()["raw"])

r = requests.post(
    "https://api.txtfetch.com/v1/extract",
    headers={"Authorization": f"Bearer {os.environ['TXTFETCH_KEY']}"},
    files={"file": ("message.eml", raw_bytes)},
)

print(r.json()["extracted_text"])
JavaScript
const messageId = "18c8f2a1b3d4e5f6";

const gmailRes = await fetch(
  `https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?format=raw`,
  { headers: { Authorization: `Bearer ${process.env.GMAIL_ACCESS_TOKEN}` } },
);
const { raw } = await gmailRes.json();
const rawBytes = Buffer.from(raw, "base64url");

const form = new FormData();
form.append("file", new Blob([rawBytes]), "message.eml");

const res = await fetch("https://api.txtfetch.com/v1/extract", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.TXTFETCH_KEY}` },
  body: form,
});

const { extracted_text } = await res.json();
console.log(extracted_text);

txtfetch ships no connector, plugin, or client for Email inbox. The script above is the whole integration. Fetch the document with Email inbox's own API, then hand it to txtfetch, the same as any other source of text.

frequently asked questions

Does txtfetch have a Gmail or Outlook connector?
No. txtfetch ships no mailbox connector for any provider. Fetch the raw message with the provider's own API, then POST it to txtfetch as a file.
Can txtfetch read my inbox directly?
No. txtfetch never holds mailbox credentials of any kind. Your own code authenticates, fetches the message, and sends only that message's bytes.
Does txtfetch see the message's attachments too?
Yes. An RFC 822 .eml file carries its attachments inline, and txtfetch parses the message body and every attachment in the same request.

sources

Related

Point it at your Email inbox files.

Pass a signed URL and the text comes back. There is no connector to install.

Get an API key →

See the URL docs →