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.

54 lines
2.4 KiB

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