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.

101 lines
2.2 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 [ -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=$({ _mail_body | _mail_send; } 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_send() {
  58. case "$_MAIL_BIN" in
  59. sendmail)
  60. if [ -n "$MAIL_FROM" ]; then
  61. "$_MAIL_BIN" -f "$MAIL_FROM" "$MAIL_TO"
  62. else
  63. "$_MAIL_BIN" "$MAIL_TO"
  64. fi
  65. ;;
  66. ssmtp)
  67. "$_MAIL_BIN" "$MAIL_TO"
  68. ;;
  69. mutt | mail)
  70. "$_MAIL_BIN" -s "$_subject" "$MAIL_TO"
  71. ;;
  72. esac
  73. }
  74. _mail_body() {
  75. if [ "$_MAIL_BIN" = "sendmail" ] || [ "$_MAIL_BIN" = "ssmtp" ]; then
  76. if [ -n "$MAIL_FROM" ]; then
  77. echo "From: $MAIL_FROM"
  78. fi
  79. echo "To: $MAIL_TO"
  80. echo "Subject: $subject"
  81. echo "Content-Type: $contenttype"
  82. echo
  83. fi
  84. echo "$_content"
  85. }