# Age Of Gmail

Admin-only testing tool: GET `/tools/gmail-age` shows the form and consumes the
pending result in that admin's session; POST runs a check (CSRF protected, three checks
per minute). Requests require HTTPS and the logged-in administrator's domain.
Email addresses stay out of the URL. Results and form pages are private,
uncached and script-free. The result and submitted email display once after a
check; refreshing or reopening the page clears the input and result without
running another Gmail check.

The address must already have an active account in this domain's `accounts`
records. Gmail dots, plus aliases and googlemail.com aliases are matched using
the existing account registry. Repo connections use their encrypted OAuth
credentials; legacy connections use their existing credentials. Expired tokens
may be refreshed and saved as in existing mailbox tools. A Gmail profile check
confirms that the token belongs to the selected mailbox before searching.

The tool searches All Mail: Inbox, Sent and archived messages, excluding drafts,
Spam and Trash. It uses bounded before-timestamp existence queries with
`maxResults=1` to narrow down the oldest remaining calendar day in
Africa/Casablanca, rather than assuming
the message-list endpoint sorts oldest first or scanning every message.
Search requests retain `resultSizeEstimate` in the field projection to avoid
dropping all fields on empty results; the estimate is never used to count mail.
A valid empty search response with HTTP 204 is also accepted as no matches.
Calendar-day boundaries avoid treating a few seconds' difference between Gmail's
search index and message timestamp as a mailbox change.
The tool then paginates the oldest day's messages and compares their
`internalDate` values to select the earliest message, independent of list order.
Candidate reads use `format=metadata` and fields `id,internalDate,labelIds` only.
For the selected message it requests the From and Subject headers using repeated
`metadataHeaders` parameters, decodes MIME-encoded words, and displays escaped
text. The sender's email is a fallback when no display name exists. No message
bodies, snippets or attachments are requested. The tool does not mark read,
move, delete, send, create worker tasks, or change payment state.

Requests have a total 25-second search budget and individual HTTP timeouts.
Empty mailboxes have no age result. API errors, missing permissions, identity
mismatches, inconsistent dates, or detected mailbox changes produce a checking
error instead of an invented creation date. Pre-1970 backdated mail is rejected
as unreliable for this estimate. No real mailboxes are queried by automated
tests.

Dates and complete elapsed years are displayed in Africa/Casablanca. The result
is explicitly estimated from the oldest available message, not the real Google
account creation date. Deleted historical mail can make it younger; imported
or backdated mail can make it older.

References:

- https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.messages/list
- https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.messages
- https://developers.google.com/workspace/gmail/api/guides/filtering
