From 97b9479b0c9abbdd33b1cf6a423633e4911fc2c9 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Tue, 21 May 2024 15:07:03 -0400 Subject: [PATCH] Respect $EMAIL when sending emails Envelope sender and RFC5322.From address are set to $EMAIL if it's non-empty. Requested in #87 --- man/certspotter.md | 5 +++++ monitor/notify.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/man/certspotter.md b/man/certspotter.md index e3a9175..880ebfa 100644 --- a/man/certspotter.md +++ b/man/certspotter.md @@ -215,6 +215,11 @@ and non-zero when a serious error occurs. : Directory from which any configuration, such as the watch list, is read. Defaults to `~/.certspotter`. +`EMAIL` + +: Email address from which to send emails. If not set, certspotter lets sendmail pick + the address. + `HTTPS_PROXY` : URL of proxy server for making HTTPS requests. `http://`, `https://`, and diff --git a/monitor/notify.go b/monitor/notify.go index 8ae189e..ade9999 100644 --- a/monitor/notify.go +++ b/monitor/notify.go @@ -77,6 +77,11 @@ func sendEmail(ctx context.Context, to []string, notif *notification) error { stdin := new(bytes.Buffer) stderr := new(bytes.Buffer) + from := os.Getenv("EMAIL") + + if from != "" { + fmt.Fprintf(stdin, "From: %s\n", from) + } 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)) @@ -87,7 +92,11 @@ func sendEmail(ctx context.Context, to []string, notif *notification) error { fmt.Fprintf(stdin, "\n") fmt.Fprint(stdin, notif.text) - args := []string{"-i", "--"} + args := []string{"-i"} + if from != "" { + args = append(args, "-f", from) + } + args = append(args, "--") args = append(args, to...) sendmail := exec.CommandContext(ctx, sendmailPath(), args...)