flake-update-20260505
1#!/usr/bin/env nix-shell
2#! nix-shell -i bash -p mu jq
3# shellcheck shell=bash
4
5# extract-email-attachments.sh
6# Extract attachments from emails matching a mu query
7# Usage: ./extract-email-attachments.sh "query" /output/dir
8
9set -euo pipefail
10
11query="$1"
12output_dir="$2"
13
14if [ -z "$query" ] || [ -z "$output_dir" ]; then
15 echo "Usage: $0 <mu-query> <output-directory>"
16 echo ""
17 echo "Examples:"
18 echo " $0 'attach:*.pdf' ./pdfs"
19 echo " $0 'from:alice@example.com attach:*' ./alice-attachments"
20 echo " $0 'maildir:/redhat/* date:1m.. attach:*.xlsx' ./work-spreadsheets"
21 exit 1
22fi
23
24mkdir -p "$output_dir"
25
26echo "Searching for emails matching: $query"
27# Count emails with attachments
28count=$(mu find "$query" attach:* --format=links 2>/dev/null | wc -l)
29
30if [ "$count" -eq 0 ]; then
31 echo "No emails found matching query with attachments."
32 exit 0
33fi
34
35echo "Found $count emails with attachments"
36echo "Extracting to: $output_dir"
37echo ""
38
39extracted=0
40mu find "$query" attach:* --format=links | while read -r email_path; do
41 if [ -f "$email_path" ]; then
42 echo "Processing: $(basename "$email_path")"
43 mu extract --target-dir="$output_dir" "$email_path" 2>/dev/null && extracted=$((extracted + 1))
44 fi
45done
46
47echo ""
48echo "Extraction complete. Files saved to: $output_dir"
49echo ""
50find "$output_dir" -maxdepth 1 -type f -printf '%s %f\n' | head -20