| 94 | } |
| 95 | |
| 96 | func (n *EmailPlugin) Notify(_ context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) { |
| 97 | name := notification.GetName() |
| 98 | cfg, ok := n.ConfigByName[name] |
| 99 | |
| 100 | if !ok { |
| 101 | return nil, fmt.Errorf("invalid plugin config name %s", name) |
| 102 | } |
| 103 | |
| 104 | logger := baseLogger.Named(cfg.Name) |
| 105 | |
| 106 | if cfg.LogLevel != "" { |
| 107 | logger.SetLevel(hclog.LevelFromString(cfg.LogLevel)) |
| 108 | } |
| 109 | |
| 110 | logger.Debug("got notification") |
| 111 | |
| 112 | server := mail.NewSMTPClient() |
| 113 | server.Host = cfg.SMTPHost |
| 114 | server.Port = cfg.SMTPPort |
| 115 | server.Username = cfg.SMTPUsername |
| 116 | server.Password = cfg.SMTPPassword |
| 117 | server.Encryption = EncryptionStringToType[cfg.EncryptionType] |
| 118 | server.Authentication = AuthStringToType[cfg.AuthType] |
| 119 | server.Helo = cfg.HeloHost |
| 120 | |
| 121 | var err error |
| 122 | |
| 123 | if cfg.ConnectTimeout != "" { |
| 124 | server.ConnectTimeout, err = time.ParseDuration(cfg.ConnectTimeout) |
| 125 | if err != nil { |
| 126 | logger.Warn(fmt.Sprintf("invalid connect timeout '%s', using default '10s'", cfg.ConnectTimeout)) |
| 127 | |
| 128 | server.ConnectTimeout = 10 * time.Second |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if cfg.SendTimeout != "" { |
| 133 | server.SendTimeout, err = time.ParseDuration(cfg.SendTimeout) |
| 134 | if err != nil { |
| 135 | logger.Warn(fmt.Sprintf("invalid send timeout '%s', using default '10s'", cfg.SendTimeout)) |
| 136 | |
| 137 | server.SendTimeout = 10 * time.Second |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | logger.Debug("making smtp connection") |
| 142 | |
| 143 | smtpClient, err := server.Connect() |
| 144 | if err != nil { |
| 145 | return &protobufs.Empty{}, err |
| 146 | } |
| 147 | |
| 148 | logger.Debug("smtp connection done") |
| 149 | |
| 150 | email := mail.NewMSG() |
| 151 | email.SetFrom(fmt.Sprintf("%s <%s>", cfg.SenderName, cfg.SenderEmail)). |
| 152 | AddTo(cfg.ReceiverEmails...). |
| 153 | SetSubject(cfg.EmailSubject) |