sendgrid.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. export _H1="Authorization: Bearer $SENDGRID_API_KEY"
  34. export _H2="Content-Type: application/json"
  35. _content="$(echo "$_content" | _json_encode)"
  36. _data="{\"personalizations\": [{\"to\": [{\"email\": \"$SENDGRID_TO\"}]}],\"from\": {\"email\": \"$SENDGRID_FROM\"},\"subject\": \"$_subject\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$_content\"}]}"
  37. response="" #just make shellcheck happy
  38. if _post "$_data" "https://api.sendgrid.com/v3/mail/send"; then
  39. if [ -z "$response" ]; then
  40. _info "sendgrid send sccess."
  41. return 0
  42. fi
  43. fi
  44. _err "sendgrid send error."
  45. _err "$response"
  46. return 1
  47. }