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.

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