Command line

Mbox Viewer has a command line that lists, searches and reads the mailboxes you indexed in the app. It prints readable text by default and one JSON document with --json, which makes it usable from shell scripts and other programs. The command line is read-only. It reads the indexes that the app built, so open and index each mailbox in the app before you query it here. Version 0.5.1 added it.

Where is the executable?

The command line is the app executable started with a command flag. It opens no window.

System Executable
macOS /Applications/Mbox Viewer.app/Contents/MacOS/mbox-viewer
Windows %LOCALAPPDATA%\Mbox Viewer\mbox-viewer.exe in a default installation
Linux, .deb package /usr/bin/mbox-viewer
Linux, AppImage The .AppImage file itself, for example ~/Applications/mbox-viewer.AppImage

The examples on this page write mbox-viewer. On macOS, an alias saves typing:

alias mbox-viewer='/Applications/Mbox\ Viewer.app/Contents/MacOS/mbox-viewer'

How do I run it from cmd.exe or PowerShell?

In cmd.exe, run the executable through start "" /wait. The empty "" is the window title that start expects before a quoted path, and /wait makes the prompt wait for the exit code:

start "" /wait "%LOCALAPPDATA%\Mbox Viewer\mbox-viewer.exe" --list-mailboxes --json > out.json
echo %ERRORLEVEL%

In PowerShell, Start-Process waits, writes the output to a file and returns the exit code:

$exe = "$env:LOCALAPPDATA\Mbox Viewer\mbox-viewer.exe"
$p = Start-Process -FilePath $exe -ArgumentList '--list-mailboxes','--json' -NoNewWindow -Wait -PassThru -RedirectStandardOutput out.json
$p.ExitCode

An argument that holds a space needs its own double quotes inside the single quotes. For example, write '"All mail"' for the mailbox All mail. You can also pipe the output into Out-String, as & $exe --list-mailboxes | Out-String does. PowerShell then waits and sets $LASTEXITCODE. Windows PowerShell 5.1 decodes a pipe or a > redirection with the console code page, which garbles accented letters. Run [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 first to avoid that. -RedirectStandardOutput writes the bytes unchanged.

Which commands are there?

The first argument chooses the command. Its arguments and options follow in any order.

Command Arguments What it prints
--list-mailboxes (none) Every mailbox of the app with its id, kind, status, message count, date range and path
--search <mailbox>, then an optional query One page of matching messages with their ids
--message <mailbox> <id> The headers, the attachment list and the text of one message
--thread <mailbox> <id> The messages of the thread that contains that message, in date order
--labels <mailbox> The Gmail labels of the mailbox with their message counts
--tags-and-folders <mailbox> The tags and virtual folders of the mailbox with their counts
--help (none) The usage of every command and option
--version (none) The version, as in mbox-viewer 0.5.1

A first argument that does not start with -- opens the app as usual, so mbox-viewer Inbox.mbox still opens a file in the window. Any other first argument that starts with -- is a usage error, so a typo never opens a window.

Which options are there?

Option Values Used by Effect
--limit N 1 to 1000 --search It sets the number of results on the page. The default is 50.
--offset N 0 to 10000 --search It skips that many results to show the next page. The default is 0.
--sort ORDER date_desc, date_asc or relevance --search It orders the results. The default is date_desc. It puts the newest message first.
--max-chars N 1000 to 20000 --message It cuts the text of the message at N characters. The default is 20000.
--json (none) Every data command It prints one JSON document instead of the text.
-- (none) Every command It ends the options, so that a query word can start with --.

An option with a value accepts both --limit 20 and --limit=20. A number outside its range is refused instead of being rounded to the nearest limit. An option given twice, an option of another command, a missing argument and an extra argument also stop the command with exit code 2.

How do I name a mailbox?

Every command except --list-mailboxes, --help and --version takes a <mailbox> argument. Mbox Viewer looks for it in this order, and case does not matter for names:

  1. It takes the mailbox whose 16-character id is exactly the argument.
  2. It takes the only mailbox whose name equals the argument.
  3. It takes the only mailbox whose name starts with the argument.

The name is the one shown in the sidebar. For instance, the file All mail.mbox appears as All mail. Accented letters match whether the file name stores them as one character or as a letter followed by an accent, as macOS archives often do. When the start of a name matches several mailboxes, the command prints each candidate with its id, kind and path. It then exits with code 2. Pass the id or a longer start of the name.

How do I list my mailboxes?

mbox-viewer --list-mailboxes
ID                NAME               KIND    STATUS  MESSAGES  FIRST       LAST        PATH
3f9a1c0d5b7e2a64  All mail           mbox    ready      48213  2009-04-02  2024-11-28  /home/alex/Mail/All mail.mbox
a07c6e19d2f4b851  Thunderbird Inbox  mbox    stale       9120                          /home/alex/Mail/Inbox.mbox
5d2e8b3f0a9c6714  invoices           folder  ready        312  2021-01-04  2024-10-30  /home/alex/Mail/invoices

KIND is mbox, eml, emlx or folder. The status tells you whether a mailbox can be read:

Status Meaning
ready The index matches the file, so every command works on the mailbox.
indexing The app is indexing the mailbox right now.
stale The file changed since indexing, or the index format is outdated. Reindex it in the app.
missing The file or folder is no longer at its path.
error The index cannot be read. Reindex the mailbox in the app.

With --json, the command prints the result of the list_mailboxes MCP tool for every mailbox. Empty fields are left out, so date_min, date_max and labels appear only when the mailbox has them:

{
  "mailboxes": [
    {
      "id": "3f9a1c0d5b7e2a64",
      "name": "All mail",
      "path": "/home/alex/Mail/All mail.mbox",
      "kind": "mbox",
      "status": "ready",
      "message_count": 48213,
      "date_min": "2009-04-02T10:15:00+02:00",
      "date_max": "2024-11-28T18:40:12+01:00",
      "labels": [{ "name": "Inbox", "count": 20114 }]
    }
  ]
}

When the app has no mailbox yet, the text output says so and the JSON output holds an empty mailboxes list. Both exit with code 0.

How do I search a mailbox?

Give the mailbox first, then the query:

mbox-viewer --search "All mail" 'from:olivia has:attachment after:2024/01/01' --limit 3
     ID  DATE              FROM                                SUBJECT
1834217  2024-03-14 09:12  Olivia Martin <olivia@example.com>  Mockups for the Atlas launch
1790544  2024-02-27 16:48  Olivia Martin <olivia@example.com>  Re: Brand guide, second draft
1722091  2024-01-09 11:03  Olivia Martin <olivia@example.com>  Signed contract
Messages 1 to 3 of 14. Use --offset 3 for the next page.

Quote the query, because the shell treats (, ), $ and ! as special characters. Single quotes keep them intact in bash and zsh. In PowerShell and cmd.exe, use double quotes. Several query words without quotes are joined with spaces, and no query at all lists every message. A query can hold up to 2000 characters, the same limit as the MCP tool. The ID column holds the message id that --message and --thread accept.

With --json, the command prints the result of the search_messages MCP tool:

{
  "total": 14,
  "offset": 0,
  "limit": 3,
  "next_offset": 3,
  "items": [
    {
      "id": 1834217,
      "date": "2024-03-14T09:12:40+01:00",
      "from": ["Olivia Martin <olivia@example.com>"],
      "to": ["Alex Moreau <alex@example.com>"],
      "subject": "Mockups for the Atlas launch",
      "snippet": "Here are the three mockups we discussed on Tuesday",
      "has_attachment": true,
      "labels": ["Projects/Atlas"],
      "tags": ["Client Atlas"]
    }
  ]
}

next_offset is null on the last page. snippet holds plain text of up to 200 characters, and empty lists such as folders are left out. A query that matches nothing prints No message matches the query. and exits with code 0, and with --json the total is 0. When the query has a problem that the app would show as a warning, such as an unknown operator, the warning goes to standard error as mbox-viewer: warning: <code>: <message>. This also happens with --json, and the JSON document then carries the same warnings in its warnings list.

How do I read a message or a thread?

mbox-viewer --message "All mail" 1834217
Id: 1834217
Date: 2024-03-14T09:12:40+01:00
From: Olivia Martin <olivia@example.com>
To: Alex Moreau <alex@example.com>
Subject: Mockups for the Atlas launch
Message-ID: a1b2c3@example.com
Labels: Projects/Atlas
Tags: Client Atlas
Attachments:
  0. atlas-mockups.pdf (application/pdf, 2.4 MB)

Here are the three mockups we discussed on Tuesday.

The text comes from the plain text part of the message, or from its HTML part converted to text. The command never prints HTML, raw headers or the content of an attachment. --max-chars cuts a long text, and a line at the end says so. With --json, the command prints the result of the get_message MCP tool.

--thread "All mail" 1834217 prints the thread of that message, one block per message in date order. It shows at most 50 messages around the one you named, and each text is cut at 2000 characters. When the thread is longer, the first line says how many messages it shows. Run --thread again with the first or last id shown to see the earlier or later messages. Use --message to read one of them in full. With --json, the command prints the result of the get_thread MCP tool.

How do I list labels, tags and folders?

mbox-viewer --labels "All mail"
mbox-viewer --tags-and-folders "All mail"
KIND    NAME          COUNT  DETAIL
tag     Client Atlas     12  color blue
folder  Invoices         48  query subject:invoice

--labels prints one line per Gmail label with its message count. It lists every label of the mailbox, while the MCP tool stops at 100 labels. --tags-and-folders lists the tags first, then the virtual folders, with the saved query of a dynamic folder. When the file that holds the tags and folders cannot be read, the command stops with exit code 1. Open the mailbox in the app to repair the file. With --json, they print the results of the list_labels and list_tags_and_folders MCP tools. The Tools reference describes every JSON field of these results.

Which exit codes does it return?

Code Meaning Example
0 The command succeeded, even when a search found nothing. --search with no match, or a reader such as head that stops early
1 The mailbox or the message was not found, or the mailbox cannot be read. An unknown mailbox, a mailbox that is not ready, an unknown message id or an unreadable tags file
2 The command line is wrong. An unknown flag, a missing argument, a number out of range or a name that matches several mailboxes
3 The data folder of the app cannot be read. An unreadable data folder or a corrupt mailboxes.json
4 The output cannot be written. A full disk while standard output goes to a file

Every error writes its text to standard error after mbox-viewer: and leaves standard output empty. This holds with --json too, so a script can test the exit code and parse standard output only on success. That works in a POSIX shell, in a batch file and with the Windows forms shown earlier on this page:

if mbox-viewer --search invoices 'label:paid' --json > paid.json; then
  echo "saved"
else
  echo "search failed with code $?" >&2
fi

These are the main error texts:

Text after mbox-viewer: Exit code
mailbox not found: <argument> 1
mailbox_not_ready: mailbox "<name>" is not ready (<status>) 1
message not found: <id> 1
the tags and folders of mailbox "<name>" cannot be read. 1
"<argument>" matches several mailboxes: followed by the candidates 2
unknown command <flag> or unknown option <flag> 2

What does the command line not do?

  • It never indexes or reindexes a file. Open the mailbox in the app for that.
  • It never changes tags, virtual folders, settings or the mailbox list.
  • It searches one mailbox per command.
  • It reads the data folder of the installed app only, the one described in Where your data lives.

The command line answers the person at the keyboard. It uses neither the MCP token nor the list of mailboxes shared with agents, and it writes nothing to the MCP audit log. For an AI agent, use the MCP server, which applies those permissions and logs every request.

Frequently asked questions

Does the command line change my mailboxes?

No. The command line only reads. It never indexes a file and never writes the mailbox list, the settings, the tags, the folders or the logs, and it leaves your .mbox, .eml and .emlx files untouched.

Why does the command line say that a mailbox is not ready?

The mailbox has not been indexed yet, or its file changed after indexing. Open the mailbox in the Mbox Viewer window, let indexing finish, then run the command again.

Do I need the MCP token to use the command line?

No. The command line runs under your own account and reads the data folder of the app directly. It needs no token and ignores the MCP settings.

Which mailboxes can the command line see?

It sees every mailbox listed in the sidebar of the app, whether or not you shared it with an AI agent. Mailboxes you removed from the list are not visible.

Can I run a command while the app is open?

Yes. The command line takes no lock on the data folder and opens no window, so it works with the app open or closed. A mailbox that the app is indexing at that moment answers with the status indexing.

Which query syntax does --search accept?

It accepts the same query language as the search bar of the app. Operators such as from:, has:attachment, after:, label: and tag: work, and so do OR, the minus sign and quotes.

How do I use the results in a script?

Add --json. The command then prints one JSON document on standard output, with the same shape as the matching MCP tool. Errors go to standard error with a non-zero exit code.

Do I have to type the full mailbox id?

No. You can give the mailbox name or the start of it, and case does not matter. When the start matches several mailboxes, the command lists them and stops with exit code 2.

Can I search several mailboxes at once?

No. --search searches one mailbox per command. List the ids with --list-mailboxes --json and run the search once per mailbox.

Where is the executable on my system?

On macOS it is /Applications/Mbox Viewer.app/Contents/MacOS/mbox-viewer. On Windows it is mbox-viewer.exe in the installation folder, and on Linux it is /usr/bin/mbox-viewer or the AppImage file.