mail.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if [ -n "$MAIL_BIN" ]; then
  67. _MAIL_BIN="$MAIL_BIN"
  68. elif _exists "sendmail"; then
  69. _MAIL_BIN="sendmail"
  70. elif _exists "ssmtp"; then
  71. _MAIL_BIN="ssmtp"
  72. elif _exists "mutt"; then
  73. _MAIL_BIN="mutt"
  74. elif _exists "mail"; then
  75. _MAIL_BIN="mail"
  76. else
  77. _err "Please install sendmail, ssmtp, mutt or mail first."
  78. return 1
  79. fi
  80. echo "$_MAIL_BIN"
  81. }
  82. _mail_cmnd() {
  83. case $(basename "$_MAIL_BIN") in
  84. sendmail)
  85. if [ -n "$MAIL_FROM" ]; then
  86. echo "'$_MAIL_BIN' -f '$MAIL_FROM' '$MAIL_TO'"
  87. else
  88. echo "'$_MAIL_BIN' '$MAIL_TO'"
  89. fi
  90. ;;
  91. ssmtp)
  92. echo "'$_MAIL_BIN' '$MAIL_TO'"
  93. ;;
  94. mutt | mail)
  95. echo "'$_MAIL_BIN' -s '$_subject' '$MAIL_TO'"
  96. ;;
  97. *)
  98. _err "Command $MAIL_BIN is not supported, use sendmail, ssmtp, mutt or mail."
  99. return 1
  100. ;;
  101. esac
  102. }
  103. _mail_body() {
  104. case $(basename "$_MAIL_BIN") in
  105. sendmail | ssmtp)
  106. if [ -n "$MAIL_FROM" ]; then
  107. echo "From: $MAIL_FROM"
  108. fi
  109. echo "To: $MAIL_TO"
  110. echo "Subject: $subject"
  111. echo "Content-Type: $contenttype"
  112. echo
  113. ;;
  114. esac
  115. echo "$_content"
  116. }
  117. _mail_valid() {
  118. [ -n "$MAIL_NOVALIDATE" ] || _contains "$1" "@"
  119. }