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.

143 lines
3.1 KiB

5 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. for b in $MAIL_BIN sendmail ssmtp mutt mail msmtp; do
  69. if _exists "$b"; then
  70. _MAIL_BIN="$b"
  71. break
  72. fi
  73. done
  74. if [ -z "$_MAIL_BIN" ]; then
  75. _err "Please install sendmail, ssmtp, mutt, mail or msmtp first."
  76. return 1
  77. fi
  78. echo "$_MAIL_BIN"
  79. }
  80. _mail_cmnd() {
  81. _MAIL_ARGS=""
  82. case $(basename "$_MAIL_BIN") in
  83. sendmail)
  84. if [ -n "$MAIL_FROM" ]; then
  85. _MAIL_ARGS="-f '$MAIL_FROM'"
  86. fi
  87. ;;
  88. mutt | mail)
  89. _MAIL_ARGS="-s '$_subject'"
  90. ;;
  91. msmtp)
  92. if [ -n "$MAIL_FROM" ]; then
  93. _MAIL_ARGS="-f '$MAIL_FROM'"
  94. fi
  95. if [ -n "$MAIL_MSMTP_ACCOUNT" ]; then
  96. _MAIL_ARGS="$_MAIL_ARGS -a '$MAIL_MSMTP_ACCOUNT'"
  97. fi
  98. ;;
  99. *) ;;
  100. esac
  101. echo "'$_MAIL_BIN' $_MAIL_ARGS '$MAIL_TO'"
  102. }
  103. _mail_body() {
  104. case $(basename "$_MAIL_BIN") in
  105. sendmail | ssmtp | msmtp)
  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. }