(emailSubject, msg string, config EmailConfig, lg log.Logger)
| 55 | } |
| 56 | |
| 57 | func SendEmail(emailSubject, msg string, config EmailConfig, lg log.Logger) error { |
| 58 | lg.Info("Sending email notification", "subject", emailSubject) |
| 59 | |
| 60 | emailBody := fmt.Sprintf("Subject: %s\r\n", emailSubject) |
| 61 | emailBody += fmt.Sprintf("To: %s\r\n", config.To) |
| 62 | emailBody += fmt.Sprintf("From: \"EthStorage\" <%s>\r\n", config.From) |
| 63 | localIP := p2p.GetLocalPublicIPv4() |
| 64 | if localIP != nil { |
| 65 | msg = strings.Replace(msg, "es-node", fmt.Sprintf("the es-node at %s", localIP.String()), 1) |
| 66 | } |
| 67 | msg += "\n\nSent at: " + time.Now().Format(time.RFC1123Z) |
| 68 | emailBody += "\r\n" + msg |
| 69 | lg.Debug("Email body", "body", emailBody) |
| 70 | |
| 71 | err := smtp.SendMail( |
| 72 | fmt.Sprintf("%s:%d", config.Host, config.Port), |
| 73 | smtp.PlainAuth("", config.Username, config.Password, config.Host), |
| 74 | config.From, |
| 75 | strings.Split(config.To, ","), |
| 76 | []byte(emailBody), |
| 77 | ) |
| 78 | if err != nil { |
| 79 | lg.Error("Failed to send email", "error", err, "config", config.String()) |
| 80 | } else { |
| 81 | lg.Info("Email notification sent successfully!") |
| 82 | } |
| 83 | return err |
| 84 | } |
no test coverage detected