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.

64 lines
2.2 KiB

  1. #!/usr/bin/env sh
  2. #Support SENDGRID.com api
  3. #SENDGRID_API_KEY=""
  4. #SENDGRID_TO="xxxx@xxx.com"
  5. #SENDGRID_FROM="xxxx@cccc.com"
  6. sendgrid_send() {
  7. _subject="$1"
  8. _content="$2"
  9. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  10. _debug "_statusCode" "$_statusCode"
  11. SENDGRID_API_KEY="${SENDGRID_API_KEY:-$(_readaccountconf_mutable SENDGRID_API_KEY)}"
  12. if [ -z "$SENDGRID_API_KEY" ]; then
  13. SENDGRID_API_KEY=""
  14. _err "You didn't specify a sendgrid api key SENDGRID_API_KEY yet ."
  15. _err "You can get yours from here https://sendgrid.com"
  16. return 1
  17. fi
  18. _saveaccountconf_mutable SENDGRID_API_KEY "$SENDGRID_API_KEY"
  19. SENDGRID_TO="${SENDGRID_TO:-$(_readaccountconf_mutable SENDGRID_TO)}"
  20. if [ -z "$SENDGRID_TO" ]; then
  21. SENDGRID_TO=""
  22. _err "You didn't specify an email to SENDGRID_TO receive messages."
  23. return 1
  24. fi
  25. _saveaccountconf_mutable SENDGRID_TO "$SENDGRID_TO"
  26. SENDGRID_FROM="${SENDGRID_FROM:-$(_readaccountconf_mutable SENDGRID_FROM)}"
  27. if [ -z "$SENDGRID_FROM" ]; then
  28. SENDGRID_FROM=""
  29. _err "You didn't specify an email to SENDGRID_FROM receive messages."
  30. return 1
  31. fi
  32. _saveaccountconf_mutable SENDGRID_FROM "$SENDGRID_FROM"
  33. SENDGRID_FROM_NAME="${SENDGRID_FROM_NAME:-$(_readaccountconf_mutable SENDGRID_FROM_NAME)}"
  34. _saveaccountconf_mutable SENDGRID_FROM_NAME "$SENDGRID_FROM_NAME"
  35. export _H1="Authorization: Bearer $SENDGRID_API_KEY"
  36. export _H2="Content-Type: application/json"
  37. _content="$(echo "$_content" | _json_encode)"
  38. if [ -z "$SENDGRID_FROM_NAME" ]; then
  39. _data="{\"personalizations\": [{\"to\": [{\"email\": \"$SENDGRID_TO\"}]}],\"from\": {\"email\": \"$SENDGRID_FROM\"},\"subject\": \"$_subject\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$_content\"}]}"
  40. else
  41. _data="{\"personalizations\": [{\"to\": [{\"email\": \"$SENDGRID_TO\"}]}],\"from\": {\"email\": \"$SENDGRID_FROM\", \"name\": \"$SENDGRID_FROM_NAME\"},\"subject\": \"$_subject\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$_content\"}]}"
  42. fi
  43. response="$(_post "$_data" "https://api.sendgrid.com/v3/mail/send")"
  44. if [ "$?" = "0" ] && [ -z "$response" ]; then
  45. _info "sendgrid send sccess."
  46. return 0
  47. fi
  48. _err "sendgrid send error."
  49. _err "$response"
  50. return 1
  51. }