Skip to content

Commit

Permalink
channel/email: Enhance ConfigOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jan 9, 2024
1 parent 45cb07b commit 15a9e8a
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions cmd/channel/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -123,18 +120,35 @@ 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
}

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: "[email protected]",
},
{
Name: "host",
Type: "string",
Expand All @@ -153,39 +167,30 @@ 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: "[email protected]",
},
{
Name: "user",
Type: "string",
Label: map[string]string{
"en_US": "User",
"de_DE": "Benutzer",
"en_US": "SMTP User",
"de_DE": "SMTP Benutzer",
},
Default: "[email protected]",
},
{
Name: "password",
Type: "secret",
Label: map[string]string{
"en_US": "Password",
"de_DE": "Passwort",
"en_US": "SMTP Password",
"de_DE": "SMTP Passwort",
},
},
{
Name: "encryption",
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",
Expand Down

0 comments on commit 15a9e8a

Please sign in to comment.