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.

73 lines
1.7 KiB

  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 "mail"; then
  15. _MAIL_BIN="mail"
  16. else
  17. _err "Please install mail or sendmail first."
  18. return 1
  19. fi
  20. MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
  21. if [ -z "$MAIL_FROM" ]; then
  22. MAIL_FROM="$USER@$(hostname -f)"
  23. _info "The MAIL_FROM is not set, so use the default value: $MAIL_FROM"
  24. fi
  25. _saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
  26. MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
  27. if [ -z "$MAIL_TO" ]; then
  28. MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
  29. _info "The MAIL_TO is not set, so use the account email: $MAIL_TO"
  30. fi
  31. _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
  32. subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
  33. result=$({ _mail_body | _mail_send; } 2>&1)
  34. if [ $? -ne 0 ]; then
  35. _debug "mail send error."
  36. _err "$result"
  37. return 1
  38. fi
  39. _debug "mail send success."
  40. return 0
  41. }
  42. _mail_send() {
  43. case "$_MAIL_BIN" in
  44. sendmail)
  45. sendmail -f "$MAIL_FROM" "$MAIL_TO"
  46. ;;
  47. mail)
  48. mail -s "$subject" -a "From:$MAIL_FROM" -a "Content-Type:text/plain; charset=utf-8" "$MAIL_TO"
  49. ;;
  50. esac
  51. }
  52. _mail_body() {
  53. if [ "$_MAIL_BIN" = "sendmail" ]; then
  54. echo "From: $MAIL_FROM"
  55. echo "To: $MAIL_TO"
  56. echo "Subject: =?UTF-8?B?$(echo "$_subject" | _base64)?="
  57. echo "Content-Type: text/plain; charset=utf-8"
  58. echo
  59. fi
  60. echo "$_content"
  61. }