Airpost
Terminal and scripting

Morning digest

A shell script that mails you a summary of yesterday's unread mail every morning, run by cron or launchd, with optional AI summaries.

What you’ll build

A script that runs at 7:30 every morning, collects the unread inbox threads from the last day, and sends you one email listing them by sender and subject. With AI set up, each thread also gets a one-paragraph summary. It uses only the airpost CLI and jq.

Prerequisites

  • airpost on your PATH. See Install the CLI.

  • An engine that runs without the desktop app, so the script works at 7:30 whether Airpost is open or not:

    airpost engine install
    
  • jq 1.6 or later.

  • Optional, for summaries: a model set up in Preferences > AI (AI features, or Local AI with Ollama).

Write the script

~/bin/airpost-digest
#!/usr/bin/env bash
# Mail a digest of the last day's unread inbox threads to yourself.
set -euo pipefail

TO="${DIGEST_TO:?set DIGEST_TO to your address}"
QUERY="${DIGEST_QUERY:-in:inbox is:unread after:yesterday}"
SUMMARIES="${DIGEST_SUMMARIES:-0}"   # 1 to add an AI summary per thread
LIMIT="${DIGEST_LIMIT:-100}"

# Wait up to a minute for the engine (exit code 3 means none is running).
for _ in $(seq 60); do
  airpost status --json >/dev/null 2>&1 && break
  sleep 1
done

threads=$(airpost search "$QUERY" -n "$LIMIT" --json)
count=$(jq '.threads | length' <<<"$threads")

if [ "$count" -eq 0 ]; then
  echo "no unread mail; nothing sent"
  exit 0
fi

body=$(mktemp)
trap 'rm -f "$body"' EXIT

{
  echo "$count unread threads ($QUERY)"
  echo
  jq -r '.threads[]
    | [ (.lmrt | localtime | strftime("%a %H:%M")),
        ((.participants[0].name // .participants[0].email) // "?"),
        .subject,
        .id ]
    | @tsv' <<<"$threads" |
  while IFS=$'\t' read -r when who subject id; do
    echo "- $when  $who: $subject"
    if [ "$SUMMARIES" = "1" ]; then
      summary=$(airpost ai summarize "$id" --json 2>/dev/null | jq -r '.text // empty' || true)
      [ -n "$summary" ] && printf '  %s\n' "$summary"
    fi
  done
} >"$body"

id=$(airpost draft new --to "$TO" \
       --subject "Mail digest: $count unread ($(date +%a\ %d\ %b))" \
       --body-file "$body" --plain --json | jq -r '.draft.id')
airpost draft send "$id" --wait --timeout 120
echo "sent digest of $count threads"
chmod +x ~/bin/airpost-digest

Run it once by hand

DIGEST_TO=you@example.com ~/bin/airpost-digest

Schedule it

~/Library/LaunchAgents/com.example.airpost-digest.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.example.airpost-digest</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/you/bin/airpost-digest</string>
  </array>
  <key>EnvironmentVariables</key>
  <dict>
    <key>DIGEST_TO</key>
    <string>you@example.com</string>
    <key>PATH</key>
    <string>/Users/you/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin</string>
  </dict>
  <key>StartCalendarInterval</key>
  <dict>
    <key>Hour</key>
    <integer>7</integer>
    <key>Minute</key>
    <integer>30</integer>
  </dict>
  <key>StandardOutPath</key>
  <string>/tmp/airpost-digest.log</string>
  <key>StandardErrorPath</key>
  <string>/tmp/airpost-digest.log</string>
</dict>
</plist>
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.airpost-digest.plist
launchctl kickstart gui/$(id -u)/com.example.airpost-digest   # run now to test

If the Mac is asleep at 7:30, launchd runs the job when it wakes.

Expected output

The script prints sent digest of 14 threads. The email reads:

14 unread threads (in:inbox is:unread after:yesterday)

- Mon 18:02  Dana Ruiz: Contract redlines
  Dana sent revised redlines and asks you to approve section 4 by Wednesday.
- Mon 21:40  GitHub: [acme/api] Release v2.3.0
- Tue 06:15  Sam Okafor: Thursday standup moved?

The summary lines only appear with DIGEST_SUMMARIES=1.

How it works

airpost search takes the search bar’s grammar and returns {"threads": [...]}, newest first, across every account. Each thread has subject, participants ({name, email}), id and lmrt, the last received message’s time in Unix seconds. airpost ai summarize --json returns the summary’s text, plus model and cached; a summary already made is returned from the cache without asking the model again.

The digest is sent through your own account with draft new and draft send --wait, which exits 1 if the send fails, so a broken run shows in the log.

Variations

  • One account. Add -a you@work.com to the search and draft new lines.
  • Print instead of mail. Replace the last three lines with cat "$body" and point cron’s output at a file, or pipe it to notify-send.
  • Different window. DIGEST_QUERY='in:inbox is:unread after:"last week"' for a weekly digest on Mondays (0 8 * * 1 in cron).
  • Leave yourself out. Digests land in your own inbox unread. Add NOT subject:"Mail digest" to the query so tomorrow’s digest skips today’s.

Cleanup

launchctl bootout gui/$(id -u)/com.example.airpost-digest
rm ~/Library/LaunchAgents/com.example.airpost-digest.plist
# Linux: remove the line with crontab -e
rm ~/bin/airpost-digest