diff --git a/pkg/mail/domain.go b/pkg/mail/domain.go index f3c65bec8..2bfc58c51 100644 --- a/pkg/mail/domain.go +++ b/pkg/mail/domain.go @@ -18,8 +18,10 @@ package mail import ( "net/url" + "os" "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/log" ) // GetMailDomain returns the hostname from the configured public URL, @@ -32,5 +34,10 @@ func GetMailDomain() string { return parsedURL.Hostname() } } + if hostname, err := os.Hostname(); err == nil && hostname != "" { + log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname) + return hostname + } + log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja") return "vikunja" } diff --git a/pkg/mail/domain_test.go b/pkg/mail/domain_test.go index 9055090d2..b94677e07 100644 --- a/pkg/mail/domain_test.go +++ b/pkg/mail/domain_test.go @@ -17,6 +17,7 @@ package mail import ( + "os" "testing" "code.vikunja.io/api/pkg/config" @@ -24,9 +25,14 @@ import ( ) func TestGetMailDomain(t *testing.T) { - t.Run("returns fallback when public URL is empty", func(t *testing.T) { + t.Run("falls back to os.Hostname when public URL is empty", func(t *testing.T) { config.ServicePublicURL.Set("") - assert.Equal(t, "vikunja", GetMailDomain()) + expectedHostname, err := os.Hostname() + if err != nil || expectedHostname == "" { + assert.Equal(t, "vikunja", GetMailDomain()) + } else { + assert.Equal(t, expectedHostname, GetMailDomain()) + } }) t.Run("extracts hostname from public URL", func(t *testing.T) { @@ -39,8 +45,13 @@ func TestGetMailDomain(t *testing.T) { assert.Equal(t, "tasks.example.com", GetMailDomain()) }) - t.Run("returns fallback for invalid URL", func(t *testing.T) { + t.Run("falls back to os.Hostname for invalid URL", func(t *testing.T) { config.ServicePublicURL.Set("://bad") - assert.Equal(t, "vikunja", GetMailDomain()) + expectedHostname, err := os.Hostname() + if err != nil || expectedHostname == "" { + assert.Equal(t, "vikunja", GetMailDomain()) + } else { + assert.Equal(t, expectedHostname, GetMailDomain()) + } }) }