Last active 2 weeks ago

Save incoming emails as plain text (with stripped headers) or place attached files in subdirectories.

aliases Raw
1# postfix aliases file `/etc/postfix/aliases`
2# an excerpt
3
4files: | /home/dominic/bin/savemail-files.sh
5general: | /home/dominic/bin/savemail.sh
6tech: | /home/dominic/bin/savemail.sh
savemail-files.sh Raw
1#!/bin/sh
2#
3# Author: Dominic Reich <quick.hat4396@qtztsjosmprqmgtunjyf.com>
4# License: MIT
5
6date=`date +%Y%m%d-%H%M%S`
7dated=`date +%Y%m%d`
8tmpf="/tmp/fullmail-$(echo ${date} | sha1sum -zt | cut -d' ' -f1).tmp"
9tmpfs="/tmp/stripped-$(echo ${date} | sha1sum -zt | cut -d' ' -f1).tmp"
10folder="/srv/sites/files/repository/private/mails"
11
12cat $* > ${tmpf}
13
14subject="$(grep -i "^Subject:" ${tmpf} | cut -d: -f2- | sed "s/^\ //" | tr -Cs "[:alnum:]" "-" | sed "s/-$//" | qprint -d)"
15to="$(grep -i "^Delivered-To:" ${tmpf} | cut -d: -f2- | cut -d@ -f1 | xargs)"
16
17#: "${VARIABLE:=DEFAULT_VALUE}"
18: "${subject:=NoSubjectGiven}"
19: "${to:=unknown}"
20
21mkdir -pm 755 ${folder}/${to}/${dated}
22ripmime -i ${tmpf} -d ${folder}/${to}/${dated}/ --no-nameless
23chmod -f 644 ${folder}/${to}/${dated}/*
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#
6# Author: Dominic Reich <quick.hat4396@qtztsjosmprqmgtunjyf.com>
7# Lastmod: 2025-02-23 07:17
8# License: MIT
9
10date=`date +%Y%m%d-%H%M%S`
11datey=`date +%Y`
12datem=`date +%Y-%m_%B`
13dated=`date +%Y-%m-%d`
14tmpf="/tmp/fullmail-$(echo ${date} | sha1sum -zt | cut -d' ' -f1).tmp"
15tmpfs="/tmp/stripped-$(echo ${date} | sha1sum -zt | cut -d' ' -f1).tmp"
16folder="/srv/sites/files/repository/private/mails"
17
18cat $* > ${tmpf}
19
20subject="$(grep -m 1 -i "^Subject:" ${tmpf} | cut -d: -f2- | sed "s/^\ //" | tr -Cs "[:alnum:]" "-" | sed "s/-$//" | qprint -d)"
21echo >&2 "found subject: $subject"
22to="$(grep -i "^Delivered-To:" ${tmpf} | cut -d: -f2- | cut -d@ -f1 | head -n 1 | xargs)"
23echo >&2 "found delivered-to: $to"
24
25#: "${VARIABLE:=DEFAULT_VALUE}"
26: "${subject:=NoSubjectGiven}"
27: "${to:=unknown}"
28
29echo >&2 "subject: $subject"
30echo >&2 "delivered-to: $to"
31
32while read line; do
33 case "${line}" in
34 From:*|To:*|C[Cc]:*|Delivered-To:*|Date:*|Subject:*|Received:*|Message-ID:*|Content-Type:*|Content-Transfer-Encoding:*|User-Agent:*) echo ${line} >> ${tmpfs};;
35 "") (echo && cat $*) >> ${tmpfs};;
36 *) ;;
37 esac
38done < ${tmpf}
39
40filename="${date}_${subject}.txt"
41file="${folder}/${to}/${datem}/${filename}"
42link="https://$URL/${to}/${filename}"
43
44mkdir -pm 755 ${folder}/${to}/${datem}
45
46mv "${tmpfs}" "${file}"
47chmod 644 ${file}
48echo >&2 "move to: $file"
49
50echo -e "New mail in archives: ${subject}\n\n<${link}>\n\n-- \nMail sorting script \`${0}\` to your service.\n" | \
51mail -s "New mail in archives: ${to}" -. contact@domain.local
52