Skip to content

Commit

Permalink
fix: allow setting the mailer service headers as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Dec 6, 2024
1 parent 9fe5b1e commit 2ad60c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 5 additions & 5 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}

Expand Down
15 changes: 11 additions & 4 deletions internal/mailer/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 2ad60c6

Please sign in to comment.