From 2ad60c61b3f1d7dab79648d681fa76f517f22eb7 Mon Sep 17 00:00:00 2001 From: Kang Ming Date: Fri, 6 Dec 2024 16:21:38 +0800 Subject: [PATCH] fix: allow setting the mailer service headers as strings --- internal/conf/configuration.go | 10 +++++----- internal/mailer/validate.go | 15 +++++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/conf/configuration.go b/internal/conf/configuration.go index 390cb29e4..40cd0f297 100644 --- a/internal/conf/configuration.go +++ b/internal/conf/configuration.go @@ -403,18 +403,18 @@ type MailerConfiguration struct { // EXPERIMENTAL: May be removed in a future release. EmailValidationExtended bool `json:"email_validation_extended" split_words:"true" default:"false"` EmailValidationServiceURL string `json:"email_validation_service_url" split_words:"true"` - EmailValidationServiceHeaders string `json:"email_validation_service_key" split_words:"true"` + EmailValidationServiceHeaders string `json:"email_validation_service_headers" split_words:"true"` - serviceHeaders map[string][]string `json:"-"` + serviceHeaders map[string]any `json:"-"` } func (c *MailerConfiguration) Validate() error { - headers := make(map[string][]string) + headers := make(map[string]any) if c.EmailValidationServiceHeaders != "" { err := json.Unmarshal([]byte(c.EmailValidationServiceHeaders), &headers) if err != nil { - return fmt.Errorf("conf: SMTP headers not a map[string][]string format: %w", err) + return fmt.Errorf("conf: mailer validation headers not a map[string][]string format: %w", err) } } @@ -424,7 +424,7 @@ func (c *MailerConfiguration) Validate() error { return nil } -func (c *MailerConfiguration) GetEmailValidationServiceHeaders() map[string][]string { +func (c *MailerConfiguration) GetEmailValidationServiceHeaders() map[string]any { return c.serviceHeaders } diff --git a/internal/mailer/validate.go b/internal/mailer/validate.go index b18a08769..274cf97db 100644 --- a/internal/mailer/validate.go +++ b/internal/mailer/validate.go @@ -79,7 +79,7 @@ var ( type EmailValidator struct { extended bool serviceURL string - serviceHeaders map[string][]string + serviceHeaders map[string]any } func newEmailValidator(mc conf.MailerConfiguration) *EmailValidator { @@ -197,14 +197,21 @@ func (ev *EmailValidator) validateService(ctx context.Context, email string) err } rdr := bytes.NewReader(reqData) - req, err := http.NewRequestWithContext(ctx, "GET", ev.serviceURL, rdr) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, ev.serviceURL, rdr) if err != nil { return nil } req.Header.Set("Content-Type", "application/json") for name, vals := range ev.serviceHeaders { - for _, val := range vals { - req.Header.Set(name, val) + switch v := vals.(type) { + case string: + req.Header.Set(name, v) + default: + b, err := json.Marshal(v) + if err != nil { + return err + } + req.Header.Set(name, string(b)) } }