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.

134 lines
2.9 KiB

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