use mailrc instead of cluttering the script; reformat summary

This commit is contained in:
Iris Lightshard 2021-11-26 22:07:41 -07:00
parent f2a858b364
commit eb82854cc0
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -12,9 +12,7 @@
# addr=<the address the notification will appear to be sent from> # addr=<the address the notification will appear to be sent from>
# phone=<your phone number in email address form (eg, 9876543210@mms.att.net)> # phone=<your phone number in email address form (eg, 9876543210@mms.att.net)>
# inbox=<the location of the maildir folder corresponding to the inbox> # inbox=<the location of the maildir folder corresponding to the inbox>
# smtp_server=<the address and port (address:port) of your smtp server> # all other configuration can be done in your .mailrc
# smtp_user=<the username you use on your smtp server>
# smtp_password<the password to login to the smtp server>
. ./.config . ./.config
@ -23,22 +21,38 @@ oldnew=0
summary() { summary() {
i=0 i=0
for m in $(\ls -1r ${inbox}/new/*); do for m in $(\ls -1r ${inbox}/new/*); do
# if there are more than 3 new messages,
# just hint that there is more
i=$((i + 1)) i=$((i + 1))
if [ ${i} -gt 3 ]; then if [ ${i} -gt 3 ]; then
echo "and more..." echo "and more..."
break break
fi fi
# grab the From and Subject headers
subject=$(grep ^Subject: ${m} | head -n 1) subject=$(grep ^Subject: ${m} | head -n 1)
from=$(grep ^From: ${m} | head -n 1) from=$(grep ^From: ${m} | head -n 1)
echo ${from}
echo ${subject} # extract just the email address from the From header
echo # ie, discard all tokens but the last
# and remove angle brackets if present
set -- $from
while [ ${#} -gt 1 ]; do
shift
done
echo -n $(echo ${@} | sed -e s/[\<\>]//g)
# now append the message after discarding the Subject: key
set -- $subject
shift
echo ": \"${@}\""
done done
} }
while true; do while true; do
# sync mailbox if this is not the mailserver # Sync the inbox if we are not the mailserver
if [ "$1" != "-l" ]; then if [ "$1" != "-l" ]; then
mbsync -a mbsync -a
fi fi
@ -48,28 +62,12 @@ while true; do
# If the number of new mails has increased # If the number of new mails has increased
if [ ${newnew} -gt ${oldnew} ]; then if [ ${newnew} -gt ${oldnew} ]; then
echo "$(summary)" \
# If we are on the mailserver, just mail the alert out from here | mail -r ${addr} \
if [ "$1" = "-l" ]; then -s "new mail [${newnew}]" \
echo "$(summary)" \ ${phone}
| mail -r ${addr} \
-s "new mail [${newnew}]" \
${phone}
# Otherwise, use the smtp configuration from the config file to send it
else
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
-S smtp=${smtp_server} \
-S smtp-use-starttls \
-S smtp-auth=login \
-S smtp-auth-user=${smtp_user} \
-S smtp-auth-password=${smtp_password} \
${phone}
fi
fi fi
oldnew=${newnew} oldnew=${newnew}
sleep 2m sleep 10m
done done