dockcheck/notify_templates/notify_smtp.sh
vorezal c63e2441fa
Refactor notifications and add helper functions (#182)
* Refactor notifications and add helper functions

* Add helper functions to simplify sourcing files and executing functions if they exist

* Create notify_v2.sh wrapper script

* Simplify and consolidate notification logic within notify_v2.sh

* Support notification management via environment variables

* Move secrets to dockcheck.config

* Fix NOTIFY_CHANNELS default value when not set

* Feedback changes

* Remove leading spaces from MessageBody

* Check for valid notify v2 variables

* Warn on missing configuration and bypass notifications

* Update readme

* Additional feedback fixes

* More comments in default.config with different # depth for comments and settings

* Rename NOTIFY_TOPIC_NAME variable to NTFY_TOPIC_NAME for consistency

* Add TELEGRAM_TOPIC_ID

* Fix AppriseURL variable

* Add an ending newline to all MessageBody statements for consistency

* Remove troubleshooting echo statement

* Prevent attempting to trigger notifications for notification templates if versions are the same

---------

Co-authored-by: Matthew Oleksowicz <matt@everyoneneeds.it>
2025-05-25 18:26:13 +02:00

41 lines
1.3 KiB
Bash

### DISCLAIMER: This is a third party addition to dockcheck - best effort testing.
NOTIFY_SMTP_VERSION="v0.2"
# INFO: ssmtp is depcerated - consider to use msmtp instead.
#
# mSMTP/sSMTP has to be installed and configured manually.
# Do not modify this file directly. Set SMTP_MAIL_FROM, SMTP_MAIL_TO, and SMTP_SUBJECT_TAG in your dockcheck.config file.
if [[ -z "${SMTP_MAIL_FROM:-}" ]] || [[ -z "${SMTP_MAIL_TO:-}" ]] || [[ -z "${SMTP_SUBJECT_TAG:-}" ]]; then
printf "SMTP notification channel enabled, but required configuration variables are missing. SMTP notifications will not be sent.\n"
remove_channel smtp
fi
MSMTP=$(which msmtp)
SSMTP=$(which ssmtp)
if [ -n "$MSMTP" ] ; then
MailPkg=$MSMTP
elif [ -n "$SSMTP" ] ; then
MailPkg=$SSMTP
else
echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1
fi
trigger_smtp_notification() {
SendMailFrom="${SMTP_MAIL_FROM}" # e.g. MAIL_FROM=me@mydomain.tld
SendMailTo="${SMTP_MAIL_TO}" # e.g. MAIL_TO=me@mydomain.tld
SubjectTag="${SMTP_SUBJECT_TAG}" # e.g. SUBJECT_TAG=dockcheck
$MailPkg $SendMailTo << __EOF
From: "$FromHost" <$SendMailFrom>
date:$(date -R)
To: <$SendMailTo>
Subject: [$SubjectTag] $MessageTitle $FromHost
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
$MessageBody
__EOF
}