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.

120 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. else
  22. _clearaccountconf "MAIL_BIN"
  23. fi
  24. _MAIL_BODY=$(_mail_body)
  25. MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
  26. if [ -n "$MAIL_FROM" ]; then
  27. if ! _contains "$MAIL_FROM" "@"; then
  28. _err "It seems that the MAIL_FROM=$MAIL_FROM is not a valid email address."
  29. return 1
  30. fi
  31. _saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
  32. fi
  33. MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
  34. if [ -n "$MAIL_TO" ]; then
  35. if ! _contains "$MAIL_TO" "@"; then
  36. _err "It seems that the MAIL_TO=$MAIL_TO is not a valid email address."
  37. return 1
  38. fi
  39. _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
  40. else
  41. MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
  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. }