mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
refactor: Update models to include sender email, name, and mail recipients for notification requests
This commit is contained in:
@@ -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:"-"`
|
||||
|
||||
@@ -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:"-"`
|
||||
|
||||
@@ -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 {
|
||||
@@ -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
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user