dominic revised this gist . Go to revision
1 file changed, 45 insertions
savemail.sh(file created)
@@ -0,0 +1,45 @@ | |||
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 |
Newer
Older