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.

123 lines
3.9 KiB

  1. #!/usr/bin/env sh
  2. #Support mailgun.com api
  3. #MAILGUN_API_KEY="xxxx"
  4. #MAILGUN_TO="yyyy@gmail.com"
  5. #MAILGUN_REGION="us|eu" #optional, use "us" as default
  6. #MAILGUN_API_DOMAIN="xxxxxx.com" #optional, use the default sandbox domain
  7. #MAILGUN_FROM="xxx@xxxxx.com" #optional, use the default sendbox account
  8. _MAILGUN_BASE="https://api.mailgun.net/v3"
  9. # subject content statusCode
  10. mailgun_send() {
  11. _subject="$1"
  12. _content="$2"
  13. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  14. _debug "_statusCode" "$_statusCode"
  15. MAILGUN_API_KEY="${MAILGUN_API_KEY:-$(_readaccountconf_mutable MAILGUN_API_KEY)}"
  16. if [ -z "$MAILGUN_API_KEY" ]; then
  17. MAILGUN_API_KEY=""
  18. _err "You didn't specify a mailgun api key MAILGUN_API_KEY yet ."
  19. _err "You can get yours from here https://mailgun.com"
  20. return 1
  21. fi
  22. _saveaccountconf_mutable MAILGUN_API_KEY "$MAILGUN_API_KEY"
  23. MAILGUN_REGION="${MAILGUN_REGION:-$(_readaccountconf_mutable MAILGUN_REGION)}"
  24. if [ -z "$MAILGUN_REGION" ]; then
  25. MAILGUN_REGION=""
  26. _info "The MAILGUN_REGION is not set, so use the default us region."
  27. _MAILGUN_BASE="https://api.mailgun.net/v3"
  28. else
  29. _saveaccountconf_mutable MAILGUN_REGION "$MAILGUN_REGION"
  30. _MAILGUN_BASE="https://api.eu.mailgun.net/v3"
  31. fi
  32. MAILGUN_TO="${MAILGUN_TO:-$(_readaccountconf_mutable MAILGUN_TO)}"
  33. if [ -z "$MAILGUN_TO" ]; then
  34. MAILGUN_TO=""
  35. _err "You didn't specify an email to MAILGUN_TO receive messages."
  36. return 1
  37. fi
  38. _saveaccountconf_mutable MAILGUN_TO "$MAILGUN_TO"
  39. MAILGUN_API_DOMAIN="${MAILGUN_API_DOMAIN:-$(_readaccountconf_mutable MAILGUN_API_DOMAIN)}"
  40. if [ -z "$MAILGUN_API_DOMAIN" ]; then
  41. _info "The MAILGUN_API_DOMAIN is not set, try to get the default sending sandbox domain for you."
  42. if ! _mailgun_rest GET "/domains"; then
  43. _err "Can not get sandbox domain."
  44. return 1
  45. fi
  46. _sendboxDomain="$(echo "$response" | _egrep_o '"name": *"sandbox.*.mailgun.org"' | cut -d : -f 2 | tr -d '" ')"
  47. _debug _sendboxDomain "$_sendboxDomain"
  48. MAILGUN_API_DOMAIN="$_sendboxDomain"
  49. if [ -z "$MAILGUN_API_DOMAIN" ]; then
  50. _err "Can not get sandbox domain for MAILGUN_API_DOMAIN"
  51. return 1
  52. fi
  53. _info "$(__green "When using sandbox domain, you must verify your email first.")"
  54. #todo: add recepient
  55. fi
  56. if [ -z "$MAILGUN_API_DOMAIN" ]; then
  57. _err "Can not get MAILGUN_API_DOMAIN"
  58. return 1
  59. fi
  60. _saveaccountconf_mutable MAILGUN_API_DOMAIN "$MAILGUN_API_DOMAIN"
  61. MAILGUN_FROM="${MAILGUN_FROM:-$(_readaccountconf_mutable MAILGUN_FROM)}"
  62. if [ -z "$MAILGUN_FROM" ]; then
  63. MAILGUN_FROM="$PROJECT_NAME@$MAILGUN_API_DOMAIN"
  64. _info "The MAILGUN_FROM is not set, so use the default value: $MAILGUN_FROM"
  65. else
  66. _debug MAILGUN_FROM "$MAILGUN_FROM"
  67. _saveaccountconf_mutable MAILGUN_FROM "$MAILGUN_FROM"
  68. fi
  69. #send from url
  70. _msg="/$MAILGUN_API_DOMAIN/messages?from=$(printf "%s" "$MAILGUN_FROM" | _url_encode)&to=$(printf "%s" "$MAILGUN_TO" | _url_encode)&subject=$(printf "%s" "$_subject" | _url_encode)&text=$(printf "%s" "$_content" | _url_encode)"
  71. _debug "_msg" "$_msg"
  72. _mailgun_rest POST "$_msg"
  73. if _contains "$response" "Queued. Thank you."; then
  74. _info "mailgun send success."
  75. return 0
  76. else
  77. _err "mailgun send error"
  78. _err "$response"
  79. return 1
  80. fi
  81. }
  82. # method uri data
  83. _mailgun_rest() {
  84. _method="$1"
  85. _mguri="$2"
  86. _mgdata="$3"
  87. _debug _mguri "$_mguri"
  88. _mgurl="$_MAILGUN_BASE$_mguri"
  89. _debug _mgurl "$_mgurl"
  90. _auth="$(printf "%s" "api:$MAILGUN_API_KEY" | _base64)"
  91. export _H1="Authorization: Basic $_auth"
  92. export _H2="Content-Type: application/json"
  93. if [ "$_method" = "GET" ]; then
  94. response="$(_get "$_mgurl")"
  95. else
  96. _debug _mgdata "$_mgdata"
  97. response="$(_post "$_mgdata" "$_mgurl" "" "$_method")"
  98. fi
  99. if [ "$?" != "0" ]; then
  100. _err "Error: $_mguri"
  101. _err "$response"
  102. return 1
  103. fi
  104. _debug2 response "$response"
  105. return 0
  106. }