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.

148 lines
3.2 KiB

6 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. #Support local mail app
  3. #MAIL_BIN="sendmail"
  4. #MAIL_FROM="yyyy@gmail.com"
  5. #MAIL_TO="yyyy@gmail.com"
  6. #MAIL_NOVALIDATE=""
  7. #MAIL_MSMTP_ACCOUNT=""
  8. mail_send() {
  9. _subject="$1"
  10. _content="$2"
  11. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  12. _debug "_subject" "$_subject"
  13. _debug "_content" "$_content"
  14. _debug "_statusCode" "$_statusCode"
  15. MAIL_NOVALIDATE="${MAIL_NOVALIDATE:-$(_readaccountconf_mutable MAIL_NOVALIDATE)}"
  16. if [ -n "$MAIL_NOVALIDATE" ]; then
  17. _saveaccountconf_mutable MAIL_NOVALIDATE 1
  18. else
  19. _clearaccountconf "MAIL_NOVALIDATE"
  20. fi
  21. MAIL_BIN="${MAIL_BIN:-$(_readaccountconf_mutable MAIL_BIN)}"
  22. if [ -n "$MAIL_BIN" ] && ! _exists "$MAIL_BIN"; then
  23. _err "It seems that the command $MAIL_BIN is not in path."
  24. return 1
  25. fi
  26. _MAIL_BIN=$(_mail_bin)
  27. if [ -n "$MAIL_BIN" ]; then
  28. _saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
  29. else
  30. _clearaccountconf "MAIL_BIN"
  31. fi
  32. MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
  33. if [ -n "$MAIL_FROM" ]; then
  34. if ! _mail_valid "$MAIL_FROM"; then
  35. _err "It seems that the MAIL_FROM=$MAIL_FROM is not a valid email address."
  36. return 1
  37. fi
  38. _saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
  39. fi
  40. MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
  41. if [ -n "$MAIL_TO" ]; then
  42. if ! _mail_valid "$MAIL_TO"; then
  43. _err "It seems that the MAIL_TO=$MAIL_TO is not a valid email address."
  44. return 1
  45. fi
  46. _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
  47. else
  48. MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
  49. if [ -z "$MAIL_TO" ]; then
  50. _err "It seems that account email is empty."
  51. return 1
  52. fi
  53. fi
  54. contenttype="text/plain; charset=utf-8"
  55. subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
  56. result=$({ _mail_body | eval "$(_mail_cmnd)"; } 2>&1)
  57. # shellcheck disable=SC2181
  58. if [ $? -ne 0 ]; then
  59. _debug "mail send error."
  60. _err "$result"
  61. return 1
  62. fi
  63. _debug "mail send success."
  64. return 0
  65. }
  66. _mail_bin() {
  67. _MAIL_BIN=""
  68. _MAIL_BINS="sendmail ssmtp mutt mail msmtp"
  69. if [ -n "$MAIL_BIN" ]; then
  70. _MAIL_BINS="$MAIL_BIN $_MAIL_BINS"
  71. fi
  72. for b in $_MAIL_BINS; do
  73. if _exists "$b"; then
  74. _MAIL_BIN="$b"
  75. break
  76. fi
  77. done
  78. if [ -z "$_MAIL_BIN" ]; then
  79. _err "Please install sendmail, ssmtp, mutt, mail or msmtp first."
  80. return 1
  81. fi
  82. echo "$_MAIL_BIN"
  83. }
  84. _mail_cmnd() {
  85. _MAIL_ARGS=""
  86. case $(basename "$_MAIL_BIN") in
  87. sendmail)
  88. if [ -n "$MAIL_FROM" ]; then
  89. _MAIL_ARGS="-f '$MAIL_FROM'"
  90. fi
  91. ;;
  92. mutt | mail)
  93. _MAIL_ARGS="-s '$_subject'"
  94. ;;
  95. msmtp)
  96. if [ -n "$MAIL_FROM" ]; then
  97. _MAIL_ARGS="-f '$MAIL_FROM'"
  98. fi
  99. if [ -n "$MAIL_MSMTP_ACCOUNT" ]; then
  100. _MAIL_ARGS="$_MAIL_ARGS -a '$MAIL_MSMTP_ACCOUNT'"
  101. fi
  102. ;;
  103. *) ;;
  104. esac
  105. echo "'$_MAIL_BIN' $_MAIL_ARGS '$MAIL_TO'"
  106. }
  107. _mail_body() {
  108. case $(basename "$_MAIL_BIN") in
  109. sendmail | ssmtp | msmtp)
  110. if [ -n "$MAIL_FROM" ]; then
  111. echo "From: $MAIL_FROM"
  112. fi
  113. echo "To: $MAIL_TO"
  114. echo "Subject: $subject"
  115. echo "Content-Type: $contenttype"
  116. echo
  117. ;;
  118. esac
  119. echo "$_content"
  120. }
  121. _mail_valid() {
  122. [ -n "$MAIL_NOVALIDATE" ] || _contains "$1" "@"
  123. }