diff --git a/monitor/mailutils.go b/monitor/mailutils.go index 150fd46..ea06d4f 100644 --- a/monitor/mailutils.go +++ b/monitor/mailutils.go @@ -12,6 +12,7 @@ package monitor import ( "crypto/rand" "encoding/hex" + "os" ) const mailDateFormat = "Mon, 2 Jan 2006 15:04:05 -0700" @@ -23,3 +24,11 @@ func generateMessageID() string { } return hex.EncodeToString(randomBytes[:]) + "@selfhosted.certspotter.org" } + +func sendmailPath() string { + if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" { + return envVar + } else { + return "/usr/sbin/sendmail" + } +} diff --git a/monitor/notify.go b/monitor/notify.go index 5936e01..d59fbbc 100644 --- a/monitor/notify.go +++ b/monitor/notify.go @@ -67,11 +67,6 @@ func sendEmail(ctx context.Context, to []string, notif notification) error { stdin := new(bytes.Buffer) stderr := new(bytes.Buffer) - sendmailPath := "/usr/sbin/sendmail" - if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" { - sendmailPath = envVar - } - fmt.Fprintf(stdin, "To: %s\n", strings.Join(to, ", ")) fmt.Fprintf(stdin, "Subject: [certspotter] %s\n", notif.Summary()) fmt.Fprintf(stdin, "Date: %s\n", time.Now().Format(mailDateFormat)) @@ -85,7 +80,7 @@ func sendEmail(ctx context.Context, to []string, notif notification) error { args := []string{"-i", "--"} args = append(args, to...) - sendmail := exec.CommandContext(ctx, sendmailPath, args...) + sendmail := exec.CommandContext(ctx, sendmailPath(), args...) sendmail.Stdin = stdin sendmail.Stderr = stderr