From 46aae0620dfca4f774122c8da2c119358a7154cb Mon Sep 17 00:00:00 2001 From: MikelAlejoBR Date: Mon, 13 Nov 2023 12:39:16 +0100 Subject: [PATCH] feature: implement email sending settings The back office proxy supoports skipping the user resolution and overriding the email's sender and the email's default recipients, and the idea of these changes is for MBOP to support the same things too. RHCLOUD-29047 --- internal/handlers/send_email.go | 33 +++++++++++++++++---- internal/models/mail.go | 5 +++- internal/service/mailer/aws_ses_mailer.go | 4 +-- internal/service/mailer/mailer_interface.go | 2 +- internal/service/mailer/print_mailer.go | 7 +++-- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/internal/handlers/send_email.go b/internal/handlers/send_email.go index 473b6af..1a0d9de 100644 --- a/internal/handlers/send_email.go +++ b/internal/handlers/send_email.go @@ -36,21 +36,42 @@ func SendEmails(w http.ResponseWriter, r *http.Request) { return } + // The user might have wanted to override the sender that goes in the "from" field of the email. + var fromAddress string + if emails.EmailSender != "" { + fromAddress = emails.EmailSender + } else { + fromAddress = config.Get().FromEmail + } + for _, email := range emails.Emails { // creating a copy in order to pass it down into sub-functions email := email - err := mailer.LookupEmailsForUsernames(r.Context(), &email) - if err != nil { - l.Log.Error(err, "error translating usernames") - continue + // Lookup the emails for the given usernames unless the client specified otherwise. + if !emails.SkipUsersResolution { + err := mailer.LookupEmailsForUsernames(r.Context(), &email) + if err != nil { + l.Log.Error(err, "error translating usernames") + continue + } + + if len(email.Recipients) == 0 { + email.Recipients = []string{config.Get().ToEmail} + } } + // Should the user not specify any recipients, we need to determine if we should use the "default + // recipient" the user might have specified, or the default one that we set up in the configuration. if len(email.Recipients) == 0 { - email.Recipients = []string{config.Get().ToEmail} + if emails.DefaultRecipient != "" { + email.Recipients = []string{emails.DefaultRecipient} + } else { + email.Recipients = []string{config.Get().ToEmail} + } } - err = sender.SendEmail(r.Context(), &email) + err = sender.SendEmail(r.Context(), &email, fromAddress) if err != nil { l.Log.Error(err, "Error sending email", "email", email) } diff --git a/internal/models/mail.go b/internal/models/mail.go index 98d2b65..dbed078 100644 --- a/internal/models/mail.go +++ b/internal/models/mail.go @@ -8,7 +8,10 @@ import ( ) type Emails struct { - Emails []Email `json:"emails,omitempty"` + Emails []Email `json:"emails,omitempty"` + EmailSender string `json:"emailSender,omitempty"` + DefaultRecipient string `json:"defaultRecipient,omitempty"` + SkipUsersResolution bool `json:"skipUsersResolution,omitempty"` } // taken from the BOP openapi spec diff --git a/internal/service/mailer/aws_ses_mailer.go b/internal/service/mailer/aws_ses_mailer.go index ec535a0..9e40026 100644 --- a/internal/service/mailer/aws_ses_mailer.go +++ b/internal/service/mailer/aws_ses_mailer.go @@ -49,9 +49,9 @@ type awsSESEmailer struct { client *ses.Client } -func (s *awsSESEmailer) SendEmail(ctx context.Context, email *models.Email) error { +func (s *awsSESEmailer) SendEmail(ctx context.Context, email *models.Email, fromAddress string) error { out, err := s.client.SendEmail(ctx, &ses.SendEmailInput{ - FromEmailAddress: aws.String(config.Get().FromEmail), + FromEmailAddress: aws.String(fromAddress), Destination: &sesTypes.Destination{ ToAddresses: email.Recipients, CcAddresses: email.CcList, diff --git a/internal/service/mailer/mailer_interface.go b/internal/service/mailer/mailer_interface.go index 58fcb97..8975696 100644 --- a/internal/service/mailer/mailer_interface.go +++ b/internal/service/mailer/mailer_interface.go @@ -11,7 +11,7 @@ import ( ) type Emailer interface { - SendEmail(ctx context.Context, email *models.Email) error + SendEmail(ctx context.Context, email *models.Email, fromAddress string) error } func NewMailer() (Emailer, error) { diff --git a/internal/service/mailer/print_mailer.go b/internal/service/mailer/print_mailer.go index 3fcc046..5827163 100644 --- a/internal/service/mailer/print_mailer.go +++ b/internal/service/mailer/print_mailer.go @@ -12,19 +12,20 @@ type printEmailer struct{} var _ = (Emailer)(&printEmailer{}) -func (p printEmailer) SendEmail(_ context.Context, email *models.Email) error { +func (p printEmailer) SendEmail(_ context.Context, email *models.Email, fromAddress string) error { l := 50 if len(email.Body) < 50 { l = len(email.Body) } - fmt.Printf(`To: %v + fmt.Printf(`From: %v +To: %v CC: %v BCC: %v Subject: %v BodyType: %v Message: %v...(truncated to 50 chars) -`, email.Recipients, email.CcList, email.BccList, email.Subject, email.BodyType, email.Body[:l]) +`, fromAddress, email.Recipients, email.CcList, email.BccList, email.Subject, email.BodyType, email.Body[:l]) return nil }