From 15a9e8a6e92d6a864576eeb3ca445c530295976d Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 21 Nov 2023 14:59:26 +0100 Subject: [PATCH] channel/email: Enhance ConfigOptions --- cmd/channel/email/main.go | 53 +++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/cmd/channel/email/main.go b/cmd/channel/email/main.go index 3232bac7c..9cb637e7d 100644 --- a/cmd/channel/email/main.go +++ b/cmd/channel/email/main.go @@ -26,7 +26,8 @@ const ( type Email struct { Host string `json:"host"` Port string `json:"port"` - From string `json:"from"` + SenderName string `json:"sender_name"` + SenderMail string `json:"sender_mail"` User string `json:"user"` Password string `json:"password"` Encryption string `json:"encryption"` @@ -53,7 +54,7 @@ func (ch *Email) SendNotification(req *plugin.NotificationRequest) error { return enmime.Builder(). ToAddrs(to). - From("", ch.From). + From(ch.SenderName, ch.SenderMail). Subject(fmt.Sprintf("[#%d] %s %s is %s", req.Incident.Id, req.Event.Type, req.Object.Name, req.Incident.Severity)). Text(msg.Bytes()). Send(ch) @@ -75,10 +76,6 @@ func (ch *Email) Send(reversePath string, recipients []string, msg []byte) error } defer client.Close() - if err = client.Hello("localhost"); err != nil { - return err - } - if ch.Encryption == TypeStartTls { if err = client.StartTLS(nil); err != nil { return err @@ -112,7 +109,7 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error { ch.Port = "25" } - if ch.From == "" { + if ch.SenderMail == "" { hostname, err := os.Hostname() if err != nil { return fmt.Errorf("failed to get the os's hostname: %w", err) @@ -123,11 +120,11 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error { return fmt.Errorf("failed to get the os's current user: %w", err) } - ch.From = usr.Username + "@" + hostname + ch.SenderMail = usr.Username + "@" + hostname } if ch.User == "" { - ch.User = ch.From + ch.User = ch.SenderMail } return nil @@ -135,6 +132,23 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error { func (ch *Email) GetInfo() *plugin.Info { elements := []*plugin.ConfigOption{ + { + Name: "sender_name", + Type: "string", + Label: map[string]string{ + "en_US": "Sender Name", + "de_DE": "Absendername", + }, + }, + { + Name: "sender_mail", + Type: "string", + Label: map[string]string{ + "en_US": "Sender Address", + "de_DE": "Absenderadresse", + }, + Default: "icinga@example.com", + }, { Name: "host", Type: "string", @@ -153,21 +167,12 @@ func (ch *Email) GetInfo() *plugin.Info { Min: types.Int{NullInt64: sql.NullInt64{Int64: 1, Valid: true}}, Max: types.Int{NullInt64: sql.NullInt64{Int64: 65535, Valid: true}}, }, - { - Name: "from", - Type: "string", - Label: map[string]string{ - "en_US": "From", - "de_DE": "Von", - }, - Default: "icinga@example.com", - }, { Name: "user", Type: "string", Label: map[string]string{ - "en_US": "User", - "de_DE": "Benutzer", + "en_US": "SMTP User", + "de_DE": "SMTP Benutzer", }, Default: "user@example.com", }, @@ -175,8 +180,8 @@ func (ch *Email) GetInfo() *plugin.Info { Name: "password", Type: "secret", Label: map[string]string{ - "en_US": "Password", - "de_DE": "Passwort", + "en_US": "SMTP Password", + "de_DE": "SMTP Passwort", }, }, { @@ -184,8 +189,8 @@ func (ch *Email) GetInfo() *plugin.Info { Type: "option", Required: true, Label: map[string]string{ - "en_US": "TLS / SSL", - "de_DE": "TLS / SSL", + "en_US": "Transport Encryption", + "de_DE": "Transportverschlüsselung", }, Options: map[string]string{ TypeNone: "None",