mailservice.md - www.codemadness.org - www.codemadness.org saait content files
(HTM) git clone git://git.codemadness.org/www.codemadness.org
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
mailservice.md (3612B)
---
1 ## How it works
2
3 * The user sends a mail with an attachment to a certain mail address, for
4 example: paste@somehost.org
5 * The mail daemon configuration has an mail alias to pipe the raw mail to a
6 shellscript.
7 * This shellscript processes the raw mail contents from stdin.
8
9
10 ## What it does
11
12 * Process a mail with the attachments automatically.
13 * The script processes the attachments in the mail and stores them.
14 * It will mail (back) the URL where the file(s) are stored.
15
16 This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
17 gopher daemon geomyidae.
18
19
20 ## Install dependencies
21
22 On OpenBSD:
23
24 pkg_add mblaze
25
26
27 ## smtpd mail configuration
28
29 In your mail aliases (for example /etc/mail/aliases) put:
30
31 paste: |/usr/local/bin/paste-mail
32
33
34 This pipes the mail to the script paste-mail for processing, this script is
35 described below. Copy the below contents in /usr/local/bin/paste-mail
36
37
38 Script:
39
40 #!/bin/sh
41
42 d="/home/www/domains/www.codemadness.org/htdocs/mailpaste"
43 tmpmsg=$(mktemp)
44 tmpmail=$(mktemp)
45
46 cleanup() {
47 rm -f "$tmpmail" "$tmpmsg"
48 }
49
50 # store whole mail from stdin temporarily, on exit remove temporary file.
51 trap "cleanup" EXIT
52 cat > "$tmpmail"
53
54 # mblaze: don't store mail sequence.
55 MAILSEQ=/dev/null
56 export MAILSEQ
57
58 # get from address (without display name).
59 from=$(maddr -a -h 'From' /dev/stdin < "$tmpmail")
60
61 # check if allowed or not.
62 case "$from" in
63 "hiltjo@codemadness.org")
64 ;;
65 *)
66 exit 0;;
67 esac
68
69 # prevent mail loop.
70 if printf '%s' "$from" | grep -q "paste@"; then
71 exit 0
72 fi
73
74 echo "Thank you for using the enterprise paste service." > "$tmpmsg"
75 echo "" >> "$tmpmsg"
76 echo "Your file(s) are available at:" >> "$tmpmsg"
77 echo "" >> "$tmpmsg"
78
79 # process each attachment.
80 mshow -n -q -t /dev/stdin < "$tmpmail" | sed -nE 's@.*name="(.*)".*@\1@p' | while read -r name; do
81 test "$name" = "" && continue
82
83 # extract attachment.
84 tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
85 mshow -n -O /dev/stdin "$name" < "$tmpmail" > "$tmpfile"
86
87 # use file extension.
88 ext="${name##*/}"
89 case "$ext" in
90 *.tar.*)
91 # special case: support .tar.gz, tar.bz2, etc.
92 ext="tar.${ext##*.}";;
93 *.*)
94 ext="${ext##*.}";;
95 *)
96 ext="";;
97 esac
98 ext="${ext%%*.}"
99
100 # use file extension if it is set.
101 outputfile="$tmpfile"
102 if test "$ext" != ""; then
103 outputfile="$tmpfile.$ext"
104 fi
105 mv "$tmpfile" "$outputfile"
106 b=$(basename "$outputfile")
107
108 chmod 666 "$outputfile"
109 url="gopher://codemadness.org/9/mailpaste/$b"
110
111 echo "$name:" >> "$tmpmsg"
112 echo " Text file: gopher://codemadness.org/0/mailpaste/$b" >> "$tmpmsg"
113 echo " Image file: gopher://codemadness.org/I/mailpaste/$b" >> "$tmpmsg"
114 echo " Binary file: gopher://codemadness.org/9/mailpaste/$b" >> "$tmpmsg"
115 echo "" >> "$tmpmsg"
116 done
117
118 echo "" >> "$tmpmsg"
119 echo "Sincerely," >> "$tmpmsg"
120 echo "Your friendly paste_bot" >> "$tmpmsg"
121
122 # mail back the user.
123 mail -r "$from" -s "Your files" "$from" < "$tmpmsg"
124
125 cleanup
126
127
128 The mail daemon processing the mail needs of course to be able to have
129 permissions to write to the specified directory. The user who received the mail
130 needs to be able to read it from a location they can access and have
131 permissions for it also.
132
133
134 ## Room for improvements
135
136 This is just an example script. There is room for many improvements.
137 Feel free to change it in any way you like.
138
139
140 ## References
141
142 * <https://man.openbsd.org/aliases>
143 * <https://man.openbsd.org/smtpd>
144 * <https://man.openbsd.org/httpd>
145 * <https://github.com/leahneukirchen/mblaze>
146
147
148 ## Bye bye
149
150 I hope this enterprise(tm) mail service is inspirational or something ;)