Last active 1731171329

savemail.sh Raw
1#!/bin/sh
2#
3# Saves an incoming email as plain text file (or .eml)
4# somewhere in the webservers directories
5# Author: Dominic Reich <quick.hat4396@qtztsjosmprqmgtunjyf.com>
6# License: MIT
7
8#umask 133
9
10date=`date +%Y%m%d-%H%M%S`
11tmpf="/tmp/mail-$(echo ${date} | sha1).tmp"
12folder="/var/www/htdocs/emails"
13
14#cat $* > ${FILE}
15
16while read line; do
17 case "$line" in
18 From:*|To:*|C[Cc]:*|Delivered-To:*|Date:*|Subject:*|Received:*|Message-ID:*|User-Agent:*) echo $line >> ${tmpf};;
19 "") (echo && cat $*) >> ${tmpf};;
20 *) ;;
21 esac
22done
23
24#SUBJECT="$(grep -i Subject ${TMPF} | awk -F': ' '{ print $2 }' | tr -cs "[:alnum:]" "-" | tr "äöüÄÖÜ" "aouAOU" | sed "s/-$//")"
25#SUBJECT="$(grep -i "^Subject:" ${TMPF} | cut -d: -f2- | sed "s/^\ //" | tr -cs "[a-zA-z0-9äöüÄÖÜ]" "-" | tr "äöüÄÖÜ" "aouAOU" | sed "s/-$//" | qprint -d)"
26
27# last sed: remove all "-" not only the last one, shrinks the name, but is not very readable...
28# subject="$(grep -i "^Subject:" ${tmpf} | cut -d: -f2- | sed "s/^\ //" | tr -Cs "[:alnum:]" "-" | sed "s/-//g" | qprint -d)"
29subject="$(grep -i "^Subject:" ${tmpf} | cut -d: -f2- | sed "s/^\ //" | tr -Cs "[:alnum:]" "-" | sed "s/-$//" | qprint -d)"
30to="$(grep -i "^Delivered-To:" ${tmpf} | cut -d: -f2- | cut -d@ -f1 | xargs)"
31
32#: "${VARIABLE:=DEFAULT_VALUE}"
33
34: "${subject:=NoSubjectGiven}"
35: "${to:=unknown}"
36
37filename="${date}_${subject}.txt"
38
39file="${folder}/${to}/${filename}"
40link="http://bor.oe7drt.com/mailarchive/${to}/${filename}"
41
42mv "${tmpf}" "${file}"
43
44echo -e "New mail in archives: ${subject}\n\n<${link}>\n\n-- \nMail sorting script \`${0}\` to your service.\n" | \
45mail -s "New mail in archives: ${to}" -- recipient@localhost.localdomain