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