Skip to content

Commit

Permalink
feature: implement email sending settings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MikelAlejoBR committed Mar 25, 2024
1 parent 965bc4b commit 46aae06
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
33 changes: 27 additions & 6 deletions internal/handlers/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/models/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/service/mailer/aws_ses_mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/service/mailer/mailer_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions internal/service/mailer/print_mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 46aae06

Please sign in to comment.