-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
85 lines (79 loc) · 2.64 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// +build !windows
package alert
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
const EnvAlertMailRecipient = "ALERT_MAIL_RECIPIENT"
const EnvAlertMailSender = "ALERT_MAIL_SENDER"
const EnvAlertMailTitlePrefix = "ALERT_MAIL_TITLE_PREFIX"
type mail struct {
title string
message string
}
// Mail sends a mail to os.Getenv(alert.EnvAlertMailRecipient)
// from os.Getenv(alert.EnvAlertMailRecipient) using /usr/sbin/sendmail
// with a title prefix of os.Getenv(alert.EnvAlertMailTitlePrefix) + " " (if set).
func Mail(title string, format string, args ...interface{}) {
recipient, sender := os.Getenv(EnvAlertMailRecipient), os.Getenv(EnvAlertMailSender)
if strings.Count(recipient, "@") != 1 {
log.Printf("alert.Mail(): cannot send mail: recipient from env var %s is malformed.", EnvAlertMailRecipient)
return
}
if strings.Count(sender, "@") != 1 {
log.Printf("alert.Mail(): cannot send mail: sender from env var %s is malformed.", EnvAlertMailSender)
return
}
fullTitle := strings.TrimSpace(os.Getenv(EnvAlertMailTitlePrefix) + " " + title)
err := sendMail(&mail{title: fullTitle, message: fmt.Sprintf(format, args...)}, recipient, sender)
if err != nil {
log.Printf("alert.Mail(): cannot send mail: %v", err)
}
}
func sendMail(m *mail, recipient, sender string) (retErr error) {
sendmail := exec.Command("/usr/sbin/sendmail", "-t", recipient)
stdin, err := sendmail.StdinPipe()
if err != nil {
return fmt.Errorf("could not get StdinPipe: %w", err)
}
defer stdin.Close()
stdout, err := sendmail.StdoutPipe()
if err != nil {
return fmt.Errorf("could not get StdoutPipe: %w", err)
}
defer stdout.Close()
err = sendmail.Start()
if err != nil {
return fmt.Errorf("could not start /usr/sbin/sendmail: %w", err)
}
defer func() {
output, stdoutErr := ioutil.ReadAll(stdout)
err = sendmail.Wait()
if retErr != nil {
return
}
if err != nil {
retErr = fmt.Errorf("could not wait for process /usr/sbin/sendmail to finish: %w", err)
return
}
if !sendmail.ProcessState.Success() {
if stdoutErr == nil {
retErr = fmt.Errorf("sendmail failed with exit code %d: %v", sendmail.ProcessState.ExitCode(), string(output))
} else {
retErr = fmt.Errorf("sendmail failed with exit code %d; also couldn't read its stdout: %v", sendmail.ProcessState.ExitCode(), stdoutErr)
}
return
}
}()
if _, err = stdin.Write([]byte("Subject: " + m.title + "\nFrom: " + sender + "\n\n\n" + m.message)); err != nil {
return fmt.Errorf("could not write in stdin of /usr/sbin/sendmail: %w", err)
}
if err = stdin.Close(); err != nil {
return fmt.Errorf("could not close stdin of /usr/sbin/sendmail: %w", err)
}
return nil
}