GoAdmio

subjecttop

Show the 25 most common email subjects in the Postfix mail log, or list every subject

On this page: Overview · Quick start · Requirements · Syntax · Output · Examples · Troubleshooting · Details · See also


Overview

/go/subjecttop helps you spot bulk or spam mail by showing which email subjects appear most often in the Postfix mail log. It reads /var/log/maillog, pulls out the subject of each logged message, and counts them.

Typical uses:

The command only reads the log. It doesn't change anything.

[!NOTE] Subjects can contain private information. Use the command only for administration, and treat its output as confidential.


Quick start

/go/subjecttop

Requirements


Syntax

/go/subjecttop
/go/subjecttop <any value>
Form Behavior
/go/subjecttop Shows the 25 most common subjects with their counts.
/go/subjecttop <any value> Prints every subject in the log, one per line, in log order, without counts. The value itself is ignored.

The command has no other options.


Output

Without an argument, each line shows how many times a subject was logged, then the subject:

    842 Make Money Fast
    120 Invoice 12345
     63 Your order has shipped
Detail Behavior
Number of lines At most 25, the most common first.
Subject length Cut to the first 75 characters.
Case Counted without regard to case, but see Troubleshooting.
Period covered Everything in the current /var/log/maillog, which is what has been logged since the last log rotation.

With an argument the output has no counts and can be very long.

Logging

Every run is appended to /var/log/go/subjecttop.log.


Examples

# The 25 most common subjects
/go/subjecttop
# Every subject, searched for a word
/go/subjecttop all | grep -i invoice
# Check that the log contains any subjects at all
grep -c "Subject:" /var/log/maillog

Troubleshooting

Symptom Likely cause Fix
No output The log has no subjects, because Postfix isn't set to log them. Check with grep -c "Subject:" /var/log/maillog.
cat: /var/log/maillog: No such file or directory The mail log is in another place on this system. Find the real log file. The script has the path fixed.
A subject is cut off in the middle The command removes everything from the first from in the line, so a subject that contains the word from is shortened. Expected. See Notes for maintainers.
Subjects that differ only in case appear as separate lines The list is sorted with case sensitivity before the case-insensitive count. Expected. Add up the lines by hand.
Old mail is missing Only the current log file is read, not the rotated ones. Run it on the rotated files by hand.
The command is slow It reads the whole log each time. Run it at a quiet time on very large logs.
An enormous amount of output An argument was given. Run it without an argument, or pipe the output to less or grep.
Permission denied The command wasn't run as root, or without sudo rights. Run it as root.

Details

What it does

  1. Logs the call to /var/log/go/subjecttop.log.
  2. Reads /var/log/maillog and keeps, for every line that contains Subject:, the text from Subject: to the end of the line.
  3. Removes the Subject: label.
  4. Without an argument:
    1. Removes everything from the first from to the end of the line. In Postfix's header warnings this is the part that comes after the subject, with the client address and the sender.
    2. Cuts each subject to 75 characters.
    3. Sorts the subjects, counts the repeats ignoring case, and sorts by count.
    4. Shows the top 25.
  5. With an argument: prints the subjects without any further processing.
flowchart TD
    A["subjecttop"] --> B["Read /var/log/maillog, keep Subject: text"]
    B --> C{"Any argument?"}
    C -- "No" --> D["Cut at ' from ' and at 75 characters"]
    D --> E["Count repeats, sort, show top 25"]
    C -- "Yes" --> F["Print every subject"]

Notes for maintainers

Issue Why it matters Suggested fix
from is removed from the first occurrence to the end of the line A subject such as Message from John becomes Message, and different subjects merge into one. Remove only Postfix's trailer, for example with sed -E 's/ from [^ ]+\[[^]]+\]; .*//'.
sort is case-sensitive, but uniq -ci ignores case Repeats that differ in case are only joined when they happen to be next to each other. Use sort -f | uniq -ci.
The path /var/log/maillog is fixed Empty output on systems that log to /var/log/mail.log or the journal. Detect the log file, or read journalctl -u postfix.
Any argument switches to dump mode A typo prints the whole log's subjects, and there is no way to choose the number of lines or the file. Add real options, such as --all and --top N.
The pattern Subject:.* matches any log line that contains the word Lines from other programs and bounce texts are mixed in. Match only header Subject: lines.
Only the current log is read, and always in full Rotated logs are ignored, and big logs are slow. Add a date range, and read rotated files when asked.
The output shows the subject of everyone's mail Private data. Restrict who can run the command.
The limits (25 lines, 75 characters) are fixed Not adjustable. Make them options.

See also

Command Description
mailtop Check the most active email accounts in GoAdmio
spam Track spam email accounts in GoAdmio
incoming Report today's inbound mail sessions
outgoing Report today's outbound mail sessions
filter Reset the server-wide email filter list to the built-in spam and virus keyword rules
deletemailqueue Delete all emails in the Postfix mail queue
postfwd Build the Postfwd rate-limiting rules