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.

118 lines
2.8 KiB

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