Respect $EMAIL when sending emails

Envelope sender and RFC5322.From address are set to $EMAIL if it's non-empty.

Requested in #87
This commit is contained in:
Andrew Ayer 2024-05-21 15:07:03 -04:00
parent b5f9a48dc3
commit cd4d796a7c
2 changed files with 15 additions and 1 deletions

View File

@ -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

View File

@ -67,6 +67,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))
@ -77,7 +82,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...)