From b957791a5fdb485acf13f3ab1e19c467756fd825 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Sun, 29 Oct 2023 08:10:49 -0400 Subject: [PATCH] Add a helper function --- monitor/mailutils.go | 9 +++++++++ monitor/notify.go | 7 +------ 2 files changed, 10 insertions(+), 6 deletions(-) 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