mail2mms/mail2mms.sh

77 lines
2 KiB
Bash
Raw Normal View History

2021-08-23 16:00:04 +00:00
#!/bin/sh
# Send an MMS message to your phone when new emails come in.
# The message will have the number of new mails in the subject
# and the body will contain the From and Subject fields of up to the 3
# most recent new mails. If the new mail count is above 3, there will also
# be 'and more...' at the end.
2021-08-29 03:35:59 +00:00
# Assumes you either have mbsync setup to sync your inbox folder,
# or use -l if this machine is actually your mailserver
2021-08-23 16:00:04 +00:00
# import the config file ./.config
# addr=<the address the notification will appear to be sent from>
# phone=<your phone number in email address form (eg, 9876543210@mms.att.net)>
# inbox=<the location of the maildir folder corresponding to the inbox>
# all other configuration can be done in your .mailrc
2021-08-23 16:00:04 +00:00
. ./.config
oldnew=0
summary() {
i=0
2021-08-29 03:35:59 +00:00
for m in $(\ls -1r ${inbox}/new/*); do
# disable glob expansion
set -f
# if there are more than 3 new messages,
# just hint that there is more
2021-08-23 16:00:04 +00:00
i=$((i + 1))
if [ ${i} -gt 3 ]; then
echo "and more..."
break
fi
# grab the From and Subject headers
2021-08-23 16:00:04 +00:00
subject=$(grep ^Subject: ${m} | head -n 1)
from=$(grep ^From: ${m} | head -n 1)
# extract just the email address from the From header
# 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 ": \"${@}\""
#reenable glob expansion
set +f
2021-08-23 16:00:04 +00:00
done
}
while true; do
# Sync the inbox if we are not the mailserver
if [ "$1" != "-l" ]; then
2021-08-29 03:35:59 +00:00
mbsync -a
fi
# Count the number of new mails
2021-08-29 03:35:59 +00:00
newnew=$(\ls -1 ${inbox}/new | wc -l)
# If the number of new mails has increased
if [ ${newnew} -gt ${oldnew} ]; then
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
${phone}
fi
2021-08-23 16:00:04 +00:00
oldnew=${newnew}
sleep 10m
2021-08-23 16:00:04 +00:00
done