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.

59 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. export LC_ALL=C
  9. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. set -o pipefail
  11. # source function library if necessary
  12. #
  13. if [ -z "${_C}" ]; then
  14. . "/lib/functions.sh"
  15. fi
  16. trm_debug="$(uci_get travelmate global trm_debug "0")"
  17. trm_mailreceiver="$(uci_get travelmate global trm_mailreceiver)"
  18. trm_mailprofile="$(uci_get travelmate global trm_mailprofile "trm_notify")"
  19. trm_mailsender="$(uci_get travelmate global trm_mailsender "no-reply@travelmate")"
  20. trm_rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
  21. trm_mailpgm="$(command -v msmtp)"
  22. trm_logger="$(command -v logger)"
  23. if [ -z "${trm_mailreceiver}" ]; then
  24. "${trm_logger}" -p "err" -t "trm-mail [${$}]" "please set the mail receiver with the 'trm_mailreceiver' option" 2>/dev/null
  25. exit 1
  26. fi
  27. if [ "${trm_debug}" = "1" ]; then
  28. debug="--debug"
  29. fi
  30. # info preparation
  31. #
  32. sys_info="$(
  33. strings /etc/banner 2>/dev/null
  34. ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}'
  35. )"
  36. trm_info="$(/etc/init.d/travelmate status 2>/dev/null)"
  37. sta_info="$(jsonfilter -i "${trm_rtfile}" -q -l1 -e '@.data.station_id')"
  38. trm_mailtopic="$(uci_get travelmate global trm_mailtopic "travelmate connection to '${sta_info}'")"
  39. 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"
  40. # mail body
  41. #
  42. trm_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  43. trm_mailtext="${trm_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  44. trm_mailtext="${trm_mailtext}\n\n<strong>++\n++ Travelmate Information ++\n++</strong>\n${trm_info}"
  45. trm_mailtext="${trm_mailtext}</pre></body></html>"
  46. # send mail
  47. #
  48. printf "%b" "${trm_mailhead}${trm_mailtext}" 2>/dev/null | "${trm_mailpgm}" ${debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1
  49. "${trm_logger}" -p "info" -t "trm-mail [${$}]" "mail sent to '${trm_mailreceiver}' with rc '${?}'" 2>/dev/null