You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.4 KiB

  1. #!/bin/sh
  2. # send mail script for travelmate notifications
  3. # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # set (s)hellcheck exceptions
  6. # shellcheck disable=1091,3040
  7. # Please note: you have to setup the package 'msmtp' before using this script
  8. . "/lib/functions.sh"
  9. export LC_ALL=C
  10. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. set -o pipefail
  12. trm_debug="$(uci_get travelmate global trm_debug "0")"
  13. trm_mailreceiver="$(uci_get travelmate global trm_mailreceiver)"
  14. trm_mailprofile="$(uci_get travelmate global trm_mailprofile "trm_notify")"
  15. trm_mailsender="$(uci_get travelmate global trm_mailsender "no-reply@travelmate")"
  16. trm_rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
  17. trm_mailpgm="$(command -v msmtp)"
  18. trm_logger="$(command -v logger)"
  19. if [ -z "${trm_mailreceiver}" ]; then
  20. "${trm_logger}" -p "err" -t "trm-mail [${$}]" "please set the mail receiver with the 'trm_mailreceiver' option" 2>/dev/null
  21. exit 1
  22. fi
  23. if [ "${trm_debug}" = "1" ]; then
  24. debug="--debug"
  25. fi
  26. # info preparation
  27. #
  28. sys_info="$(
  29. strings /etc/banner 2>/dev/null
  30. ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}'
  31. )"
  32. trm_info="$(/etc/init.d/travelmate status 2>/dev/null)"
  33. sta_info="$(jsonfilter -i "${trm_rtfile}" -q -l1 -e '@.data.station_id')"
  34. trm_mailtopic="$(uci_get travelmate global trm_mailtopic "travelmate connection to '${sta_info}'")"
  35. trm_mailhead="From: ${trm_mailsender}\nTo: ${trm_mailreceiver}\nSubject: ${trm_mailtopic}\nReply-to: ${trm_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n"
  36. # mail body
  37. #
  38. trm_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  39. trm_mailtext="${trm_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  40. trm_mailtext="${trm_mailtext}\n\n<strong>++\n++ Travelmate Information ++\n++</strong>\n${trm_info}"
  41. trm_mailtext="${trm_mailtext}</pre></body></html>"
  42. # send mail
  43. #
  44. printf "%b" "${trm_mailhead}${trm_mailtext}" 2>/dev/null | "${trm_mailpgm}" ${debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1
  45. "${trm_logger}" -p "info" -t "trm-mail [${$}]" "mail sent to '${trm_mailreceiver}' with rc '${?}'" 2>/dev/null