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. 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. if [ -z "$MAIL_TO" ]; then
  42. _err "It seems that account email is empty."
  43. return 1
  44. fi
  45. fi
  46. contenttype="text/plain; charset=utf-8"
  47. subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
  48. result=$({ echo "$_MAIL_BODY" | eval "$_MAIL_CMD"; } 2>&1)
  49. if [ $? -ne 0 ]; then
  50. _debug "mail send error."
  51. _err "$result"
  52. return 1
  53. fi
  54. _debug "mail send success."
  55. return 0
  56. }
  57. _mail_cmnd() {
  58. if [ -n "$MAIL_BIN" ]; then
  59. _MAIL_BIN="$MAIL_BIN"
  60. elif _exists "sendmail"; then
  61. _MAIL_BIN="sendmail"
  62. elif _exists "ssmtp"; then
  63. _MAIL_BIN="ssmtp"
  64. elif _exists "mutt"; then
  65. _MAIL_BIN="mutt"
  66. elif _exists "mail"; then
  67. _MAIL_BIN="mail"
  68. else
  69. _err "Please install sendmail, ssmtp, mutt or mail first."
  70. return 1
  71. fi
  72. case $(basename "$_MAIL_BIN") in
  73. sendmail)
  74. if [ -n "$MAIL_FROM" ]; then
  75. echo "'$_MAIL_BIN' -f '$MAIL_FROM' '$MAIL_TO'"
  76. else
  77. echo "'$_MAIL_BIN' '$MAIL_TO'"
  78. fi
  79. ;;
  80. ssmtp)
  81. echo "'$_MAIL_BIN' '$MAIL_TO'"
  82. ;;
  83. mutt | mail)
  84. echo "'$_MAIL_BIN' -s '$_subject' '$MAIL_TO'"
  85. ;;
  86. *)
  87. _err "Command $MAIL_BIN is not supported, use sendmail, ssmtp, mutt or mail."
  88. return 1
  89. ;;
  90. esac
  91. }
  92. _mail_body() {
  93. if [ "$_MAIL_BIN" = "sendmail" ] || [ "$_MAIL_BIN" = "ssmtp" ]; then
  94. if [ -n "$MAIL_FROM" ]; then
  95. echo "From: $MAIL_FROM"
  96. fi
  97. echo "To: $MAIL_TO"
  98. echo "Subject: $subject"
  99. echo "Content-Type: $contenttype"
  100. echo
  101. fi
  102. echo "$_content"
  103. }