From f30881de3df6f3d69fc2fefef3145f683a625f61 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 5 Jan 2024 13:06:43 +0300 Subject: [PATCH] mailgun-rest --- server/email/email.go | 10 +++++ server/email/mailgun.go | 96 +++++++++++++++++++++++++++++++++++++++++ server/go.mod | 1 + server/go.sum | 9 ++++ 4 files changed, 116 insertions(+) create mode 100644 server/email/mailgun.go diff --git a/server/email/email.go b/server/email/email.go index 8376931..c5ff7c7 100644 --- a/server/email/email.go +++ b/server/email/email.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/tls" + "os" "strconv" "strings" "text/template" @@ -95,6 +96,15 @@ func SendEmail(to []string, event string, data map[string]interface{}) error { return err } + mailgunAPIKey := os.Getenv("MAILGUN_API_KEY") + + if len(mailgunAPIKey) > 0 { + err = SendMailgun(to, event, data) + if err != nil { + return err + } + } + m := gomail.NewMessage() senderEmail, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeySenderEmail) if err != nil { diff --git a/server/email/mailgun.go b/server/email/mailgun.go new file mode 100644 index 0000000..ac90520 --- /dev/null +++ b/server/email/mailgun.go @@ -0,0 +1,96 @@ +package email + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "os" + + // log "github.com/sirupsen/logrus" + + "github.com/authorizerdev/authorizer/server/constants" +) + +const apiURL = "https://api.mailgun.net/v3/%s/messages" + +func mailgun_send(to []string, data map[string]interface{}, subject string, template string) error { + var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY") + var mailgunDomain = os.Getenv("MAILGUN_DOMAIN") + + // Create custom variables JSON + customVariablesJSON, err := json.Marshal(data) + if err != nil { + return err + } + + // Create payload + payload := map[string]interface{}{ + "from": mailgunDomain + " ", + "to": to, + "subject": subject, + "template": template, + "h:X-Mailgun-Variables": string(customVariablesJSON), + } + + // Convert payload to JSON + payloadJSON, err := json.Marshal(payload) + if err != nil { + return err + } + + // Make HTTP POST request + client := &http.Client{} + req, err := http.NewRequest("POST", fmt.Sprintf(apiURL, mailgunDomain), bytes.NewBuffer(payloadJSON)) + if err != nil { + return err + } + + req.SetBasicAuth("api", mailgunAPIKey) + req.Header.Set("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { + return err + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("failed to send email, status code: %d", resp.StatusCode) + } + + return nil +} + +// SendMailgun function to send +func SendMailgun(to []string, event string, data map[string]interface{}) error { + template := "email_confirmation" + + switch event { + case constants.VerificationTypeBasicAuthSignup: + template = "email_confirmation" + case constants.VerificationTypeForgotPassword: + template = "reset_password" + case constants.VerificationTypeInviteMember: + template = "author_invited" + case constants.VerificationTypeMagicLinkLogin: + template = "magic_link_login" + case constants.VerificationTypeOTP: + template = "one_time_password" + case constants.VerificationTypeUpdateEmail: + template = "email_update" + } + + subject := "Подтверждение почты" + + // TODO: language selection logic here + + err := mailgun_send(to, data, subject, template) + + if err != nil { + return err + } + + return nil +} diff --git a/server/go.mod b/server/go.mod index 62cf9b3..af1c540 100644 --- a/server/go.mod +++ b/server/go.mod @@ -56,6 +56,7 @@ require ( github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/glebarez/go-sqlite v1.21.2 // indirect + github.com/go-chi/chi/v5 v5.0.8 // indirect github.com/go-jose/go-jose/v3 v3.0.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/server/go.sum b/server/go.sum index a0788c6..4351793 100644 --- a/server/go.sum +++ b/server/go.sum @@ -81,6 +81,9 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/ekristen/gorm-libsql v0.0.0-20231101204708-6e113112bcc2 h1:3f6DAUkYKbZSJ1bBM0/RiX5NHVt7YgmB0BWzKWUd45g= github.com/ekristen/gorm-libsql v0.0.0-20231101204708-6e113112bcc2/go.mod h1:5g9wSYpR/MvkR6W7SumX9zdha7Yt1iM4nxOAWfRfcPA= +github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= +github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= +github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -92,6 +95,8 @@ github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9g github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= github.com/glebarez/sqlite v1.10.0 h1:u4gt8y7OND/cCei/NMHmfbLxF6xP2wgKcT/BJf2pYkc= github.com/glebarez/sqlite v1.10.0/go.mod h1:IJ+lfSOmiekhQsFTJRx/lHtGYmCdtAiTaf5wI9u5uHA= +github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= +github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyMWVo= github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -190,6 +195,7 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= @@ -217,6 +223,8 @@ github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 h1:6Pf github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475/go.mod h1:20nXSmcf0nAscrzqsXeC2/tA3KkV2eCiJqYuyAgl+ss= github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275 h1:IZycmTpoUtQK3PD60UYBwjaCUHUP7cML494ao9/O8+Q= github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275/go.mod h1:zt6UU74K6Z6oMOYJbJzYpYucqdcQwSMPBEdSvGiaUMw= +github.com/mailgun/mailgun-go/v4 v4.12.0 h1:TtuQCgqSp4cB6swPxP5VF/u4JeeBIAjTdpuQ+4Usd/w= +github.com/mailgun/mailgun-go/v4 v4.12.0/go.mod h1:L9s941Lgk7iB3TgywTPz074pK2Ekkg4kgbnAaAyJ2z8= github.com/maruel/rs v1.1.0 h1:dh4OceAF5yD06EASOrb+DS358LI4g0B90YApSdjCP6U= github.com/maruel/rs v1.1.0/go.mod h1:vzwMjzSJJxLIXmU62qHj6O5QRn5kvCKxFrfaFCxBcUY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= @@ -243,6 +251,7 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=