aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2022-02-10 08:17:29 -0300
committerSilvio Rhatto <rhatto@riseup.net>2022-02-10 08:17:29 -0300
commit22903e3000ec12ee49bd28d136f63078bd676c46 (patch)
tree1d99f651877029e6cf58247c39ea8fbd589db58b
parent8f6f663e74f1ef41fbbfda607ff52e67b728d15d (diff)
downloadutils-mail-22903e3000ec12ee49bd28d136f63078bd676c46.tar.gz
utils-mail-22903e3000ec12ee49bd28d136f63078bd676c46.tar.bz2
Feat: adds minimailer
-rwxr-xr-xminimailer59
1 files changed, 59 insertions, 0 deletions
diff --git a/minimailer b/minimailer
new file mode 100755
index 0000000..09c66ae
--- /dev/null
+++ b/minimailer
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+#
+# Send mails based on a template and a recipient list using MSMTP.
+#
+# See also https://hostpresto.com/community/tutorials/how-to-send-email-from-the-command-line-with-msmtp-and-mutt/
+# https://deaddabe.fr/blog/2021/10/06/sending-templated-emails-using-python-and-msmtp/
+#
+
+# Example template file
+cat > /dev/null <<EOF
+To: recipient
+From: myself@mydomain
+Subject: Email Test using MSMTP from File
+
+Hi $recipient! This is an automated message.
+
+King regards,
+Myself
+
+--
+my signature
+EOF
+
+# Example recipient list
+cat > /dev/null <<EOF
+Some One <someone@example.org>
+somebody@example.org
+EOF
+
+# Parameters
+BASENAME="`basename $0`"
+BASEDIR="`dirname $0`"
+#TEMPLATE="$BASEDIR/message.tmpl"
+#RECIPIENTS="$BASEDIR/recipients.lst"
+#MSMTP_ACCOUNT="my-smtp-account-from-msmtprc"
+TEMPLATE="$1"
+RECIPIENTS="$2"
+MSMTP_ACCOUNT="$3"
+
+# Checks
+if [ -z "$MSMTP_ACCOUNT" ]; then
+ echo "usage: $BASENAME <template-file> <recipients-filelist> <msmtp-account-name>"
+ echo "example: $BASENAME message.tmpl recipients.lst my-provider"
+ echo "check the source code for template and recipient filelist formats"
+ exit 1
+elif [ ! -e "$TEMPLATE" ] || [ ! -e "$RECIPIENTS" ]; then
+ echo "missing config files"
+ exit 1
+fi
+
+# Dispatch
+cat $RECIPIENTS | while read recipient; do
+ # Thanks for the quick sed regexp:
+ # https://linuxconfig.org/extract-email-address-from-a-text-file
+ address="`echo $recipient | sed -r 's/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/\n&\n/ig;s/(^|\n)[^@]*(\n|$)/\n/g;s/^\n|\n$//g;/^$/d'`"
+
+ sed -e "s/\$recipient/$recipient/g" $TEMPLATE | \
+ msmtp -a $MSMTP_ACCOUNT $address
+done