Add a helper function

This commit is contained in:
Andrew Ayer 2023-10-29 08:10:49 -04:00
parent 07bf0cfe2f
commit b957791a5f
2 changed files with 10 additions and 6 deletions

View File

@ -12,6 +12,7 @@ package monitor
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"os"
) )
const mailDateFormat = "Mon, 2 Jan 2006 15:04:05 -0700" const mailDateFormat = "Mon, 2 Jan 2006 15:04:05 -0700"
@ -23,3 +24,11 @@ func generateMessageID() string {
} }
return hex.EncodeToString(randomBytes[:]) + "@selfhosted.certspotter.org" return hex.EncodeToString(randomBytes[:]) + "@selfhosted.certspotter.org"
} }
func sendmailPath() string {
if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" {
return envVar
} else {
return "/usr/sbin/sendmail"
}
}

View File

@ -67,11 +67,6 @@ func sendEmail(ctx context.Context, to []string, notif notification) error {
stdin := new(bytes.Buffer) stdin := new(bytes.Buffer)
stderr := 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, "To: %s\n", strings.Join(to, ", "))
fmt.Fprintf(stdin, "Subject: [certspotter] %s\n", notif.Summary()) fmt.Fprintf(stdin, "Subject: [certspotter] %s\n", notif.Summary())
fmt.Fprintf(stdin, "Date: %s\n", time.Now().Format(mailDateFormat)) 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 := []string{"-i", "--"}
args = append(args, to...) args = append(args, to...)
sendmail := exec.CommandContext(ctx, sendmailPath, args...) sendmail := exec.CommandContext(ctx, sendmailPath(), args...)
sendmail.Stdin = stdin sendmail.Stdin = stdin
sendmail.Stderr = stderr sendmail.Stderr = stderr