mailgun.sh 4.1 KB

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