Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

fixed #15: when add first domain, set BILLIONMAIL_HOSTNAME.

Jackcedea9519506
diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.go
index 04879ac0..f3629f82 100644
--- a/core/internal/service/domains/domains.go
+++ b/core/internal/service/domains/domains.go
@@ -26,6 +26,47 @@ func Add(ctx context.Context, domain *v1.Domain) error {
domain.Active = 1
_, err := g.DB().Model("domain").Ctx(ctx).Insert(domain)
+
+ if err == nil {
+ // attempt update hostname in .env file
+ hostname := public.MustGetDockerEnv("BILLIONMAIL_HOSTNAME", "")
+
+ if hostname == "" || hostname == "mail.example.com" {
+ err = public.SetDockerEnv("BILLIONMAIL_HOSTNAME", public.FormatMX(domain.Domain))
+
+ if err != nil {
+ return fmt.Errorf("failed to update BILLIONMAIL_HOSTNAME in .env file: %v", err)
+ }
+
+ // update postfix main.cf myhostname
+ mainCFPath := public.AbsPath(consts.POSTFIX_MAIN_CONF)
+
+ content, err := public.ReadFile(mainCFPath)
+
+ if err != nil {
+ return fmt.Errorf("failed to read postfix main.cf: %v", err)
+ }
+
+ // replace the hostname in the content
+ content = strings.ReplaceAll(content, "myhostname = ", fmt.Sprintf("myhostname = %s", public.FormatMX(domain.Domain)))
+ content = strings.ReplaceAll(content, "myhostname = mail.example.com", fmt.Sprintf("myhostname = %s", public.FormatMX(domain.Domain)))
+
+ // write the updated content back to the file
+ _, err = public.WriteFile(mainCFPath, content)
+
+ if err != nil {
+ return fmt.Errorf("failed to write postfix main.cf: %v", err)
+ }
+
+ // restart postfix service
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(ctx, "billionmail-postfix-billionmail-1")
+
+ if err != nil {
+ return fmt.Errorf("failed to restart postfix container: %v", err)
+ }
+ }
+ }
+
return err
}
diff --git a/core/internal/service/public/common.go b/core/internal/service/public/common.go
index 9c2fb209..4e004194 100644
--- a/core/internal/service/public/common.go
+++ b/core/internal/service/public/common.go
@@ -2359,6 +2359,61 @@ func MustGetDockerEnv(envName string, def string) (envVal string) {
return
}
+// SetDockerEnv set Docker Environment Configuration
+func SetDockerEnv(envName string, envVal string) (err error) {
+ // Read environment from ../.env
+ envFile := AbsPath(consts.DEFAULT_DOCKER_ENV_FILE)
+
+ if !FileExists(envFile) {
+ err = errors.New("environment file not found: " + envFile)
+ return
+ }
+
+ rows := make([]string, 0, 256)
+ updated := false
+
+ // Read each lines
+ err = ReadEach(envFile, func(row string, cnt int) bool {
+ rows = append(rows, strings.TrimRight(row, "\r\n"))
+
+ // Trim whitespace
+ row = strings.TrimSpace(row)
+
+ // Ingore empty line
+ if row == "" {
+ return true
+ }
+
+ // Ingore comment line
+ if strings.HasPrefix(row, "#") {
+ return true
+ }
+
+ // Split by =
+ env := strings.Split(row, "=")
+ if len(env) == 2 {
+ if strings.TrimSpace(env[0]) == envName {
+ row = fmt.Sprintf("%s=%s", envName, envVal)
+ updated = true
+ rows[cnt] = row
+ return true
+ }
+ }
+ return true
+ })
+
+ if !updated {
+ rows = append(rows, fmt.Sprintf("%s=%s", envName, envVal))
+ }
+
+ fmt.Println(rows)
+
+ // Write environment to ../.env
+ _, err = WriteFile(envFile, strings.Join(rows, "\n"))
+
+ return
+}
+
// WithFileRestoration Create a backup of the specified file
func WithFileRestoration(filePaths ...string) (doneFunc func(), restoreFunc func(), err error) {
// backup paths