Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: Configurable Mailgun base URL (#745)

Khazuyo04718b2b081e
diff --git a/.example.env b/.example.env
index a71d29381..7e64a71cf 100644
--- a/.example.env
+++ b/.example.env
@@ -22,8 +22,9 @@ [email protected]
EMAIL_MAILGUN_API=
EMAIL_MAILGUN_DOMAIN=
+EMAIL_MAILGUN_REGION=US
EMAIL_SMTP_HOST=smtp.yourdomain.com
EMAIL_SMTP_PORT=587
EMAIL_SMTP_USERNAME=
-EMAIL_SMTP_PASSWORD=
\ No newline at end of file
+EMAIL_SMTP_PASSWORD=
diff --git a/app/pkg/email/mailgun/mailgun.go b/app/pkg/email/mailgun/mailgun.go
index f365c7040..e1cd30b94 100644
--- a/app/pkg/email/mailgun/mailgun.go
+++ b/app/pkg/email/mailgun/mailgun.go
@@ -13,7 +13,13 @@ import (
"github.com/getfider/fider/app/pkg/web/http"
)
-var baseURL = "https://api.mailgun.net/v3/%s/messages"
+// Known base URLs
+// Should Mailgun add other regions we'll just need to add their URLs here
+// Use upper case keys - incoming env var values are normalized before being used
+var baseURLs = map[string]string{
+ "US" : "https://api.mailgun.net/v3/%s/messages",
+ "EU" : "https://api.eu.mailgun.net/v3/%s/messages",
+}
//Sender is used to send emails
type Sender struct {
@@ -33,6 +39,30 @@ func (s *Sender) Send(ctx email.Context, templateName string, params email.Param
return s.BatchSend(ctx, templateName, params, from, []email.Recipient{to})
}
+// Try getting the base URL of the Mailgun API using Environment vars and the Sender's domain
+// Fall back to the URL for the US region if that fails to maintain compatibility with older installs
+func (s *Sender) GetBaseURL() string {
+ var regionCode = env.Config.Email.Mailgun.Region
+ regionCode = strings.ToUpper(regionCode)
+
+ // Default to the US domain if no region code was provided (ENV not set)
+ // or if the provided code isn't valid
+ if len(regionCode) < 1 {
+ regionCode = "US"
+ } else if len(baseURLs[regionCode]) < 1 {
+ s.logger.Warnf(
+ "Unknown Mailgun region code '@{Code}' configured - falling back to 'US'",
+ log.Props{
+ "Code": env.Config.Email.Mailgun.Region,
+ },
+ )
+
+ regionCode = "US"
+ }
+
+ return fmt.Sprintf(baseURLs[regionCode], s.domain)
+}
+
// BatchSend an email to multiple recipients
func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.Params, from string, to []email.Recipient) error {
if len(to) == 0 {
@@ -104,8 +134,7 @@ func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.
})
}
- url := fmt.Sprintf(baseURL, s.domain)
- request, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))
+ request, err := http.NewRequest("POST", s.GetBaseURL(), strings.NewReader(form.Encode()))
if err != nil {
return errors.Wrap(err, "failed to create POST request")
}
diff --git a/app/pkg/email/mailgun/mailgun_test.go b/app/pkg/email/mailgun/mailgun_test.go
index 01de464af..42d959bfe 100644
--- a/app/pkg/email/mailgun/mailgun_test.go
+++ b/app/pkg/email/mailgun/mailgun_test.go
@@ -198,3 +198,28 @@ func TestBatch_Success(t *testing.T) {
</body>
</html>`)
}
+
+func TestGetBaseURL(t *testing.T) {
+ RegisterT(t)
+
+ // Fall back to US if there is nothing set
+ env.Config.Email.Mailgun.Region = ""
+ Expect(sender.GetBaseURL()).Equals("https://api.mailgun.net/v3/mydomain.com/messages")
+
+ // Return the EU domain for EU, ignore the case
+ env.Config.Email.Mailgun.Region = "EU"
+ Expect(sender.GetBaseURL()).Equals("https://api.eu.mailgun.net/v3/mydomain.com/messages")
+ env.Config.Email.Mailgun.Region = "eu"
+ Expect(sender.GetBaseURL()).Equals("https://api.eu.mailgun.net/v3/mydomain.com/messages")
+
+ // Return the US domain for US, ignore the case
+ env.Config.Email.Mailgun.Region = "US"
+ Expect(sender.GetBaseURL()).Equals("https://api.mailgun.net/v3/mydomain.com/messages")
+ env.Config.Email.Mailgun.Region = "us"
+ Expect(sender.GetBaseURL()).Equals("https://api.mailgun.net/v3/mydomain.com/messages")
+
+ // Return the US domain if the region is invalid
+ env.Config.Email.Mailgun.Region = "Mars"
+ Expect(sender.GetBaseURL()).Equals("https://api.mailgun.net/v3/mydomain.com/messages")
+
+}
\ No newline at end of file
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index cdb438f80..b1c4ba285 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -59,6 +59,7 @@ type config struct {
Mailgun struct {
APIKey string `env:"EMAIL_MAILGUN_API"`
Domain string `env:"EMAIL_MAILGUN_DOMAIN"`
+ Region string `env:"EMAIL_MAILGUN_REGION,default=US"`
}
SMTP struct {
Host string `env:"EMAIL_SMTP_HOST"`