Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHCLOUD-29047 | feature: implement email sending settings #103

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions internal/handlers/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@ import (
"github.com/redhatinsights/mbop/internal/service/mailer"
)

// SendEmails sends the incoming payload's emails through the configured mailer module.
func SendEmails(w http.ResponseWriter, r *http.Request) {
switch config.Get().MailerModule {
case awsModule, printModule:
body, err := io.ReadAll(r.Body)
if err != nil {
do500(w, "failed to read request body: "+err.Error())
return
}
defer r.Body.Close()

var emails models.Emails
err = json.Unmarshal(body, &emails)
if err != nil {
do400(w, "failed to parse request body: "+err.Error())
return
}

// create our mailer (using the correct interface)
sender, err := mailer.NewMailer()
if err != nil {
Expand All @@ -36,29 +23,67 @@ func SendEmails(w http.ResponseWriter, r *http.Request) {
return
}

for _, email := range emails.Emails {
// creating a copy in order to pass it down into sub-functions
email := email
sendEmails(w, r, sender)
default:
CatchAll(w, r)
}
}

// sendEmails reads the unmarshalls the incoming body, and it verifies that it is correct. It determines if the email
// sender was overridden and resolves the specified non-email users through the configured user module. Finally, it
// determines if the default recipient must be grabbed from the config or if, on the other hand, we must set it to what
// the user asked it to be set.
func sendEmails(w http.ResponseWriter, r *http.Request, sender mailer.Emailer) {
body, err := io.ReadAll(r.Body)
if err != nil {
do500(w, "failed to read request body: "+err.Error())
return
}
defer r.Body.Close()

var emails models.Emails
err = json.Unmarshal(body, &emails)
if err != nil {
do400(w, "failed to parse request body: "+err.Error())
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

// 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 {
// 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 {
if emails.DefaultRecipient == "" {
email.Recipients = []string{config.Get().ToEmail}
}

err = sender.SendEmail(r.Context(), &email)
if err != nil {
l.Log.Error(err, "Error sending email", "email", email)
} else {
email.Recipients = []string{emails.DefaultRecipient}
}
}

sendJSON(w, newResponse("success"))

default:
CatchAll(w, r)
err = sender.SendEmail(r.Context(), &email, fromAddress)
if err != nil {
l.Log.Error(err, "Error sending email", "email", email)
}
}

sendJSON(w, newResponse("success"))
}
Loading
Loading