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.

91 lines
2.0 KiB

6 years ago
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_FROM="yyyy@gmail.com"
  4. #MAIL_TO="yyyy@gmail.com"
  5. mail_send() {
  6. _subject="$1"
  7. _content="$2"
  8. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  9. _debug "_subject" "$_subject"
  10. _debug "_content" "$_content"
  11. _debug "_statusCode" "$_statusCode"
  12. if _exists "sendmail"; then
  13. _MAIL_BIN="sendmail"
  14. elif _exists "ssmtp"; then
  15. _MAIL_BIN="ssmtp"
  16. elif _exists "mutt"; then
  17. _MAIL_BIN="mutt"
  18. elif _exists "mail"; then
  19. _MAIL_BIN="mail"
  20. else
  21. _err "Please install sendmail, ssmtp, mutt or mail first."
  22. return 1
  23. fi
  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 [ -z "$MAIL_TO" ]; then
  34. MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
  35. _info "The MAIL_TO is not set, so use the account email: $MAIL_TO"
  36. fi
  37. _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
  38. contenttype="text/plain; charset=utf-8"
  39. subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
  40. result=$({ _mail_body | _mail_send; } 2>&1)
  41. if [ $? -ne 0 ]; then
  42. _debug "mail send error."
  43. _err "$result"
  44. return 1
  45. fi
  46. _debug "mail send success."
  47. return 0
  48. }
  49. _mail_send() {
  50. case "$_MAIL_BIN" in
  51. sendmail)
  52. if [ -n "$MAIL_FROM" ]; then
  53. "$_MAIL_BIN" -f "$MAIL_FROM" "$MAIL_TO"
  54. else
  55. "$_MAIL_BIN" "$MAIL_TO"
  56. fi
  57. ;;
  58. ssmtp)
  59. "$_MAIL_BIN" "$MAIL_TO"
  60. ;;
  61. mutt | mail)
  62. "$_MAIL_BIN" -s "$_subject" "$MAIL_TO"
  63. ;;
  64. esac
  65. }
  66. _mail_body() {
  67. if [ "$_MAIL_BIN" = "sendmail" ] || [ "$_MAIL_BIN" = "ssmtp" ]; then
  68. if [ -n "$MAIL_FROM" ]; then
  69. echo "From: $MAIL_FROM"
  70. fi
  71. echo "To: $MAIL_TO"
  72. echo "Subject: $subject"
  73. echo "Content-Type: $contenttype"
  74. echo
  75. fi
  76. echo "$_content"
  77. }