#!/bin/bash # Save piped email to "$1/YYMMDD_SUBJECT.eml" # Make sure commands 's' and '|' have default meaning and are NOT unbinded # Don't overwrite existing file set -o noclobber message=$(cat) maildate=$(<<<"$message" grep -oPm 1 '^Date: ?\K.*') fdate=$(date -d "$maildate" "+%y%m%d") subject=$(<<<"$message" grep -oPm 1 '^Subject: ?\K.*' | sed 's,/,∕,g') if [[ "$fdate" == '' ]]; then echo "Error: no date parsed" exit 1 elif [[ "$subject" == '' ]]; then echo "Warning: no subject found" fi echo "${message}" > "$1/$fdate""_""$subject.eml" && echo "Email saved to $1/$fdate""_""$subject.eml" exit