An earlier version of this page, kept as it was (blog commit 0508bc9). Read the current introduction.
Introducing Gmail: mail links that open on any device
Gmail on iOS sucks. Tap a link to one particular email on an iPhone and the Gmail app opens, shows you your inbox, and forgets what you asked for. There is no working deep link to a message. On a laptop the same link works perfectly, which is how everyone ends up sending links that only work on the device they were sent from.
Gmail is a small app that fixes that one problem: a link to a mail message that works wherever it is tapped. When Misha's mind mentions a mail in a chat, it links to it, and its instructions say to use this app's address instead of mail.google.com, keeping the path, query and hash exactly as Gmail writes them. The app's paths mimic Gmail's own, so the same link is a valid Gmail link with only the host changed.
It is for Misha, who reads his mind's messages on a phone and sometimes on a laptop. The page itself is public and holds no mail; everything that reads mail is members only.
How it behaves
A link looks like any Gmail link, with the host swapped: a thread by Gmail's token after #inbox/, a search after #search/, a label after #label/, or the older query-string forms with th= or permmsgid=.
- On a desktop, the page replaces itself with the same link on mail.google.com, path, query and hash intact. Gmail does the showing there, so the desktop needs no sign-in here.
- On a phone, it shows the mail itself. It asks the server for the thread or the search results; if the visitor isn't signed in, it goes straight to the platform's sign-in and comes back to the same link. A thread shows each message in a sandboxed frame that runs no script, with its attachments; a search or label shows the matching threads.
?render=1forces the phone view anywhere, and?render=0always goes on to Gmail.
The phone view has one row of chrome, which scrolls away: a search box (return searches) and one button. The button exists because the iOS Gmail app has no working deep link to a message. So it copies a search that finds the message (or the list's query) and opens the Gmail app, ready to be pasted into its search.
The app only reads. Nothing is marked read, labelled or changed.
Where it came from
It started on 24 October as a page on the apex domain that handed links to the iOS Gmail app, with message ids kept in the URL's fragment. That experiment is still there. On 30 October it became its own host, with the desktop redirect and the phone view.
The same day it changed shape twice. A version that put the whole host behind sign-in was walked back the same day: a desktop only redirects, so it never needs to sign in here, and a phone should be sent to sign-in rather than shown a signed-out page. The final split is a public page with no data and members-only data paths on the server. Later that day attachments were reworked: they had been looked up by ids that the mail API changes between fetches, and PDFs had been served under a CSP sandbox that Chrome refuses to display.
On 6 November it learned to read a second mailbox. A link names its mailbox by address the way Gmail allows, in the path or as ?authuser=, and only that mailbox is read; an address not on the app's list is refused. A plain numeric index such as /mail/u/1/ is only the order of a browser's signed-in accounts, so it names no mailbox: a thread is looked for in the first mailbox, then the second.
The token inside a Gmail link
The newer Gmail links carry tokens like FMfcg…, which the mail API doesn't accept. They turn out to be base64 rewritten in an alphabet of 40 consonants, and the base64 decodes to something like thread-f: plus the decimal form of the API's hex id. Decoding is a change of base:
const span = (a: string, b: string) =>
Array.from({ length: b.charCodeAt(0) - a.charCodeAt(0) + 1 }, (_, i) => String.fromCharCode(a.charCodeAt(0) + i)).join("");
const LETTERS = span("A", "Z") + span("a", "z");
const BASE64 = LETTERS + span("0", "9") + "+/";
const CONSONANTS = LETTERS.replace(/[AEIOUYaeiouy]/g, ""); // the 40 letters Gmail uses
function convert(text: string, from: string, to: string): string {
let n = 0n;
for (const c of text) n = n * BigInt(from.length) + BigInt(from.indexOf(c));
let out = "";
for (; n > 0n; n /= BigInt(to.length)) out = to[Number(n % BigInt(to.length))] + out;
return out;
}
// "FMfcg…" -> "thread-f:1880…" -> the API's hex id
const decoded = atob(convert(token, CONSONANTS, BASE64));
Tokens for threads Gmail made on the sender's side (thread-a:) have no API id at all, and the app says so rather than guessing.
Related
The mind's mail handling has its own posts: how the hourly check does the looking in code and wakes an agent only when there is something to read, and how a single drafting rule was corrected five times in one evening.