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