From a73e912971f716485fa2cff05eb5fcc0252c193d Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Tue, 24 Aug 2021 23:10:03 -0600 Subject: [PATCH] move offlineimap spawning to inside the loop in case it crashes, include some explanatory comments --- mail2mms.sh | 61 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/mail2mms.sh b/mail2mms.sh index 5321b86..f84b4ef 100755 --- a/mail2mms.sh +++ b/mail2mms.sh @@ -19,13 +19,6 @@ . ./.config -if [ "$1" != -l ]; then - - if ! pgrep offlineimap; then - offlineimap & - fi -fi - oldnew=0 summary() { @@ -45,25 +38,43 @@ summary() { } while true; do - newnew=$(ls -1 ${inbox}/new | wc -l) - if [ ${newnew} -gt ${oldnew} ]; then - if [ "$1" = -l ]; then - echo "$(summary)" \ - | mail -r ${addr} \ - -s "new mail [${newnew}]" \ - ${phone} - 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 + + # Start offlineimap if it's not running and we are not on the mailserver + # Sometimes it can crash, so it's in the loop here instead of at startup + + if [ "$1" != -l ]; then + if ! pgrep offlineimap; then + offlineimap & fi + fi + + # Count the number of new mails + newnew=$(ls -1 ${inbox}/new | wc -l) + + # If the number of new mails has increased + if [ ${newnew} -gt ${oldnew} ]; then + + # If we are on the mailserver, just mail the alert out from here + if [ "$1" = "-l" ]; then + echo "$(summary)" \ + | 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 + oldnew=${newnew} sleep 2m done