Browse Source

use python injection script and simplify

Pi 1 year ago
parent
commit
eab82437f9
2 changed files with 32 additions and 41 deletions
  1. 23 37
      cropswap-sendmail.sh
  2. 9 4
      mime-inject.py

+ 23 - 37
cropswap-sendmail.sh

@@ -8,59 +8,45 @@ trap cleanup EXIT
 
 cd "${WD}"
 
+MIME_INJECT=/opt/cropswap/mime-inject.py
 DELIVERED=false
 SENDMAIL="/usr/sbin/sendmail"
 if ((DEBUG != 0)); then
 	SENDMAIL="$DEBUGMAIL"
 fi
-cat - > original
+
+mk_subject(){
+	subject="$(cat subject)"
+	if [ "${subject/\[${SUBJECT}\]/}" == "${subject}" ]; then
+		subject="[${SUBJECT}] ${subject## }"
+	fi
+	echo "${subject}"
+}
+
+tee >(reformail -c -x "Subject:" >subject) original | "${MIME_INJECT}" "To unsubscribe, send an email to $MAILGROUP, with [UNSUBSCRIBE] as the subject (square brackets included)." "$(cat <<EOF
+<div class="banner"><span>To unsubscribe, click <a href="mailto:$MAILGROUP?subject=[UNSUBSCRIBE]">here</a> or send an email to $MAILGROUP, with [UNSUBSCRIBE] as the subject (square brackets included).</span></div>
+EOF
+)" "$(cat <<EOF
+.banner{position:sticky;top:0;background-color:#00f;color:#fff;height:2em;width:100vw;text-align:center;padding:0.5em;box-sizing:border-box;text-transform:uppercase;}
+EOF
+)" | reformime -s1 -e > body
 reformail \
 	-X "Date:" \
 	-X "MIME-Version:" \
 	-X "Subject:" \
 	-X "Content-Type:" \
-< original > headers
-subject="$(reformail -c -x "Subject:" < headers)"
-if [ "${subject/\[${SUBJECT}\]/}" == "${subject}" ]; then
-	subject="[${SUBJECT}] ${subject## }"
-fi
-
-banner="NOTICE:======== <<<"
-makemime \
-	-c "text/plain" -C 'UTF-8' - <<< "${banner}" | reformail \
-	-I 'Content-Disposition: inline;filename=NOTICE.txt' \
-> banner
-content_type="$(reformail -x 'Content-Type:' < headers | cut -d';' -f1)"
-if ! [ "${content_type}" == "text/plain" ]; then
-	reformail \
-		-X 'Content-Type:' \
-		-X 'Content-Transfer-Encoding:' \
-	< original | reformail \
-		-a 'Content-Transfer-Encoding: 8bit' \
-		-a 'Content-Disposition: inline' \
-	> mime-headers
-	reformime -s1 -e < original | cat mime-headers - > alternative
-	makemime -m 'multipart/related' -C 'UTF-8' alternative > related
-	makemime -j related banner > mixed
-	makemime -m 'multipart/mixed' -a 'MIME-Version: 1.0' -C 'UTF-8' mixed | cat headers -
-else
-	cat headers - <<-EOF
-
-	$(reformime -s1 -e < original)
-
-	${banner}
-	EOF
-fi | reformail \
+< original | reformail \
 	-U "MIME-Version:" \
-	-U "Content-Type:" \
-	-I "Subject: ${subject}" \
+	-U "Content-Type:" | reformail \
+	-I "Subject: $(mk_subject)" \
 	-I "List-Id: $MAILGROUP" \
 	-I "List-Unsubscribe: <mailto:$MAILGROUP?subject=[UNSUBSCRIBE]>" \
 	-I "List-Subscribe: <mailto:$MAILGROUP?subject=[SUBSCRIBE]>" \
 	-I "List-Post: <mailto:$MAILGROUP>" \
 	-I "List-Unsubscribe-Post: List-Unsubscribe=One-Click" \
-> mail.new
-mv mail{.new,}
+> headers
+cat headers body > mail
+
 
 declare -A SUBSCRIBED
 declare -a actions

+ 9 - 4
mime-inject.py

@@ -7,14 +7,19 @@ INJECTED = {
   'text/plain': False,
   'text/html': False,
 }
-
+txt, html, css = sys.argv[1:4]
 def inject(m):
   c = m.get_content_type()
   payload = m.get_payload()
-  if c == 'text/plain':
-    payload = 'BANNER\r' + payload
+  if c == 'text/plain' and txt not in payload:
+    payload = payload + f"""
+{txt}
+"""
   if c == 'text/html':
-    payload = payload.replace('</body>', '<p>BANNER</p></body>')
+    if '</body>' in payload and '</head>' in payload:
+      payload = payload.replace('</body>', f'{html}</body>').replace('</head>', f'<style>{css}</style></head>')
+    else:
+      payload = payload + f'<style>{css}</style>{html}'
   m.set_payload(payload)
 
 def visit(m):