-
Notifications
You must be signed in to change notification settings - Fork 8
/
mailto.sh
executable file
·51 lines (39 loc) · 1.36 KB
/
mailto.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /bin/sh
# this sends the message in dir/file.txt to the addresses in dir/#address.txt
if ! command -v bsd-mailx >/dev/null 2>&1; then
echo "Who am I? Why am I here? Am I on lilo? bsd-mailx is missing!" >& 2
exit 1
fi
set -e
FROM=`whoami`@science.ru.nl
BCC="$FROM"
PREFIX="OpenCourseWare: "
if [ -z "$*" ]; then
echo "Usage: mailto.sh */file.txt" 1>&2
exit
fi
for file in "$@"; do
if [ ! -e "$file" ]; then
echo "$file" not found. >& 2
exit 1
fi
# correct encoding errors caused by Windows tools
ENC=`file -b "$file"`
if [ "$ENC" = "UTF-8 Unicode (with BOM) text" ]; then
(echo '1s/^.//'; echo wq) | ed -s "$file"
fi
if [ "${ENC%% *}" != "UTF-8" ] && [ "${ENC%% *}" != "ASCII" ]; then
#recode `file -b --mime-encoding "$file"`..utf-8 "$file"
iconv -c -o "$file" -f `file -b --mime-encoding "$file"` "$file"
fi
SUBJECT="$PREFIX Feedback $ASSIGNMENT"
MIME="Content-Type: $(file -b --mime "$file")"
if [ -e "${file}.sent" ]; then
echo Skipping $TOID. Already mailed to: `cat "${file}.sent"` >&2
elif ! TO=`sed 's/.*,//' "$(dirname "$file")/#address.txt"` || [ -z "$TO" ]; then
echo Could not find any email address to send "$file" to >&2
touch "${file}.could_not_sent"
else
cat "$file" | tr -d '\r' | bsd-mailx -a "$MIME" -n -s "$SUBJECT" ${FROM:+-a "From: $FROM"} ${BCC:+-a "Bcc: $BCC"} ${BCC:+-b "$BCC"} $TO && echo "$TO" > "${file}.sent"
fi
done