Skip to content

Commit

Permalink
refactor: Update models to include sender email, name, and mail recip…
Browse files Browse the repository at this point in the history
…ients for notification requests
  • Loading branch information
tikazyq committed Jul 27, 2024
1 parent 8d5a0b0 commit e8bdfd2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
10 changes: 5 additions & 5 deletions core/models/models/v2/notification_request_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type NotificationRequestV2 struct {
Error string `json:"error,omitempty" bson:"error,omitempty"`
Title string `json:"title" bson:"title"`
Content string `json:"content" bson:"content"`
SenderEmail string `json:"sender_email" bson:"sender_email"`
SenderName string `json:"sender_name" bson:"sender_name"`
MailTo string `json:"mail_to" bson:"mail_to"`
MailCc string `json:"mail_cc" bson:"mail_cc"`
MailBcc string `json:"mail_bcc" bson:"mail_bcc"`
SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"`
SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"`
MailTo []string `json:"mail_to,omitempty" bson:"mail_to,omitempty"`
MailCc []string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"`
MailBcc []string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"`
SettingId primitive.ObjectID `json:"setting_id" bson:"setting_id"`
ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"`
Setting *NotificationSettingV2 `json:"setting,omitempty" bson:"-"`
Expand Down
10 changes: 5 additions & 5 deletions core/models/models/v2/notification_setting_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ type NotificationSettingV2 struct {
TriggerTarget string `json:"trigger_target" bson:"trigger_target"`
Trigger string `json:"trigger" bson:"trigger"`

SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"`
SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"`
MailTo string `json:"mail_to" bson:"mail_to,omitempty"`
MailCc string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"`
MailBcc string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"`
SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"`
SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"`
MailTo []string `json:"mail_to,omitempty" bson:"mail_to,omitempty"`
MailCc []string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"`
MailBcc []string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"`

ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"`
Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"`
Expand Down
44 changes: 10 additions & 34 deletions core/notification/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
)

func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc, title, content string) error {
func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc []string, title, content string) error {
// compatibility for different providers
var auth *XOAuth2Auth
if ch.Provider == ChannelMailProviderOutlook {
Expand Down Expand Up @@ -92,9 +92,9 @@ type smtpAuthentication struct {
// sendOptions are options for sending an email
type sendOptions struct {
Subject string
To string
Cc string
Bcc string
To []string
Cc []string
Bcc []string
}

// send email
Expand All @@ -111,7 +111,7 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin
return errors.New("SMTP user is empty")
}

if options.To == "" {
if len(options.To) == 0 {
return errors.New("no receiver emails configured")
}

Expand All @@ -120,26 +120,15 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin
Address: smtpConfig.SenderEmail,
}

var toList []string
if strings.Contains(options.To, ";") {
toList = strings.Split(options.To, ";")
// trim space
for i, to := range toList {
toList[i] = strings.TrimSpace(to)
}
} else {
toList = []string{options.To}
}

m := gomail.NewMessage()
m.SetHeader("From", from.String())
m.SetHeader("To", getRecipientList(options.To)...)
m.SetHeader("To", options.To...)
m.SetHeader("Subject", options.Subject)
if options.Cc != "" {
m.SetHeader("Cc", getRecipientList(options.Cc)...)
if len(options.Cc) > 0 {
m.SetHeader("Cc", options.Cc...)
}
if options.Bcc != "" {
m.SetHeader("Bcc", getRecipientList(options.Bcc)...)
if len(options.Bcc) > 0 {
m.SetHeader("Bcc", options.Bcc...)
}

m.SetBody("text/plain", txtBody)
Expand All @@ -152,16 +141,3 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin

return d.DialAndSend(m)
}

func getRecipientList(value string) (values []string) {
if strings.Contains(value, ";") {
values = strings.Split(value, ";")
// trim space
for i, v := range values {
values[i] = strings.TrimSpace(v)
}
} else {
values = []string{value}
}
return values
}

0 comments on commit e8bdfd2

Please sign in to comment.