Email notification plugin add sender_name option (#1297)

* add sender_name option

* default config and validation
This commit is contained in:
Christophe Jauffret 2022-03-07 10:18:12 +01:00 committed by GitHub
parent e35efc5b2d
commit a77b3e2690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View file

@ -27,6 +27,7 @@ smtp_username: # Replace with your actual username
smtp_password: # Replace with your actual password
smtp_port: # Common values are any of [25, 465, 587, 2525]
auth_type: # Valid choices are "none", "crammd5", "login", "plain"
sender_name: "CrowdSec"
sender_email: # example: foo@gmail.com
email_subject: "CrowdSec Notification"
receiver_emails:

View file

@ -40,6 +40,7 @@ type PluginConfig struct {
SMTPUsername string `yaml:"smtp_username"`
SMTPPassword string `yaml:"smtp_password"`
SenderEmail string `yaml:"sender_email"`
SenderName string `yaml:"sender_name"`
ReceiverEmails []string `yaml:"receiver_emails"`
EmailSubject string `yaml:"email_subject"`
EncryptionType string `yaml:"encryption_type"`
@ -51,10 +52,42 @@ type EmailPlugin struct {
}
func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
d := PluginConfig{}
d := PluginConfig{
SMTPPort: 587,
SenderName: "Crowdsec",
EmailSubject: "Crowdsec notification",
EncryptionType: "ssltls",
AuthType: "login",
}
if err := yaml.Unmarshal(config.Config, &d); err != nil {
return nil, err
}
if d.Name == "" {
return nil, fmt.Errorf("name is required")
}
if d.SMTPHost == "" {
return nil, fmt.Errorf("SMTP host is not set")
}
if d.SMTPUsername == "" {
return nil, fmt.Errorf("SMTP username is not set")
}
if d.SMTPPassword == "" {
return nil, fmt.Errorf("SMTP password is not set")
}
if d.SenderEmail == "" {
return nil, fmt.Errorf("Sender email is not set")
}
if d.ReceiverEmails == nil || len(d.ReceiverEmails) == 0 {
return nil, fmt.Errorf("Receiver emails are not set")
}
n.ConfigByName[d.Name] = d
return &protobufs.Empty{}, nil
}
@ -88,7 +121,7 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
logger.Debug("smtp connection done")
email := mail.NewMSG()
email.SetFrom(fmt.Sprintf("From <%s>", cfg.SenderEmail)).
email.SetFrom(fmt.Sprintf("%s <%s>", cfg.SenderName, cfg.SenderEmail)).
AddTo(cfg.ReceiverEmails...).
SetSubject(cfg.EmailSubject)
email.SetBody(mail.TextHTML, notification.Text)