Preview Mailcap File Handling with mimeview

mimeview shows exactly which mailcap command it would run for a file, before it lets that command near your screen. This guide sticks to that safe preview: identifying a file's MIME type and checking the proposed display command without ever launching a viewer. The examples use GNU Mailutils 3.17, installed here as Debian package version 1:3.17-1.1build3.

Allow about fifteen minutes. You need a shell, a file you can read, and a working mailcap setup. No command in this guide needs elevated privileges. Do not use sudo to fix a missing MIME rule: root access does not make a missing viewer or broken configuration correct.

1. Check the installed command

Confirm that the command is the GNU Mailutils implementation and see its option spelling:

$ command -v mimeview
/usr/bin/mimeview
$ mimeview --version
mimeview (GNU Mailutils) 3.17
$ mimeview --help | sed -n '1,35p'

The command accepts one or more file names. It uses MIME type information to select a mailcap entry, then uses that entry's display command. A file name is data, but the selected viewer is executable software, so inspect the proposed action before allowing it to run.

Checkpoint: You know which binary will handle the file and have recorded its version. The rest of this guide assumes GNU Mailutils 3.17 or a compatible command with the same options.

2. Identify a file without displaying it

Make a private test file, or replace the path below with an existing file that you trust:

$ printf '%s\n' 'sample text' > /tmp/mimeview-note.txt
$ mimeview --no-config --mimetypes=/etc/mime.types \
    --dry-run --identify /tmp/mimeview-note.txt
/tmp/mimeview-note.txt: text/plain

--identify asks mimeview to print the MIME type it selected for each file. --dry-run prevents the display action and prints what would have been done. The exact type depends on the MIME mapping and file name, so treat the output as a result to check rather than as a promise that every text file will be handled alike.

The --mimetypes option is included here to make the mapping source explicit. On this machine, the normal configuration refers to /usr/share/cups/mime, which is absent. Without an explicit, valid file, mimeview exits with a stat error before it can identify the test file. If your normal setup works, you can omit --no-config --mimetypes=..., but keep the explicit form while diagnosing a host.

3. Preview the mailcap action

Once the type is known, run a dry run without --identify:

$ mimeview --no-config --mimetypes=/etc/mime.types \
    --dry-run --no-interactive /tmp/mimeview-note.txt
typefield: text/plain
view-command: less %s
        fields[1]: needsterminal
...

The installed command prints the candidate mailcap entries in a dry run. The output can include several viewers, such as less, more or vim, and it can differ between machines. The important check is that the MIME type and proposed command are appropriate before you allow normal display.

--no-interactive, also spelled --print or -h, disables interactive mode. It is useful for scripts and inspection. It does not turn an untrusted mailcap entry into a safe one, and it does not replace reviewing the command that will receive the file.

Safety boundary: Do not add --no-ask to a batch job until you have reviewed the relevant mailcap entries. That option suppresses confirmation for all files, or for MIME patterns supplied to it. A mailcap entry can invoke a terminal editor, a graphical viewer or another program with access to the file.

4. Validate a MIME mapping before using it

For a small, controlled test, create a MIME mapping with one rule per line. The format is a MIME type followed by one or more filename extensions:

$ printf '%s\n' \
    'text/plain txt' \
    'application/octet-stream bin' > /tmp/mimeview.types
$ mimeview --no-config --mimetypes=/tmp/mimeview.types --lint
$ echo "$?"
0

--lint checks the mime.types syntax and exits. It does not prove that a mailcap viewer exists, that the file content is safe, or that the mapping is semantically right. A zero status means that this parser accepted the syntax.

Use the same file for an identification test:

$ mimeview --no-config --mimetypes=/tmp/mimeview.types \
    --dry-run --identify /tmp/mimeview-note.txt
/tmp/mimeview-note.txt: text/plain

If lint reports a line and column, correct that mapping and rerun lint before investigating mailcap. Do not edit /etc/mime.types merely to silence a test. A system-wide change affects other programs and needs a documented package or configuration change. The temporary files in these examples can be removed after inspection; do not remove a shared mapping or mailcap file as a cleanup shortcut.

5. Diagnose the common failures

A missing file produces a non-zero status. Check the path and read permission without changing anything:

$ test -r /path/to/input && echo readable
$ mimeview --no-config --mimetypes=/tmp/mimeview.types \
    --dry-run --identify /path/to/input
$ echo "$?"

If the command says it cannot stat a MIME file, supply a known-good file with --mimetypes=FILE and use --no-config while isolating the problem. If lint fails, the mapping is not usable yet. If identification succeeds but no display command is selected, inspect the mailcap entries for the resulting MIME type. A MIME type map and a mailcap viewer entry are separate pieces of configuration.

Use --debug when you need parser traces. The manpage documents g for MIME mapping parser traces, l for lexical traces and digits for a debugging level. Debug output is diagnostic noise, not a substitute for --dry-run. Keep the file path and any data passed to a viewer quoted in surrounding shell scripts.

Done means