Last active 1731167120

file_email.sh Raw
1#!/usr/bin/env bash
2# Save piped email to "$1/YYMMDD SUBJECT.eml"
3#
4# Author: Dominic Reich <quick.hat4396@qtztsjosmprqmgtunjyf.com>
5
6# Don't overwrite existing file
7set -o noclobber
8
9message=$(cat)
10
11mail_date=$(<<<"$message" ggrep -oPm 1 '^Date: ?\K.*')
12formatted_date=$(date -f "$mail_date" +%y%m%d)
13# Get the first line of the subject, and change / to ∕ so it's not a subdirectory
14subject=$(<<<"$message" ggrep -oPm 1 '^Subject: ?\K.*' | sed 's,/,∕,g')
15
16if [[ $formatted_date == '' ]]; then
17 echo Error: no date parsed
18 exit 1
19elif [[ $subject == '' ]]; then
20 echo Warning: no subject found
21fi
22
23echo "${message}" > "$1/$formatted_date $subject.eml" && echo Email saved to "$1/$formatted_date $subject.eml"
24