-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail.go
73 lines (67 loc) · 1.91 KB
/
sendmail.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
package deliver
import (
"bytes"
"log"
"os/exec"
"strings"
"github.com/vodolaz095/msmtpd"
)
// good read - https://man.archlinux.org/man/sendmail.8.en
// SendmailOptions defines how we start sendmail executable
type SendmailOptions struct {
// PathToExecutable
PathToExecutable string
// UseMinusTFlag
UseMinusTFlag bool
}
// ViaSendmail sends email message via sendmail command
func ViaSendmail(opts *SendmailOptions) msmtpd.DataHandler {
if opts.PathToExecutable == "" {
executablePath, lErr := exec.LookPath("sendmail")
if lErr != nil {
log.Fatalf("%s : while finding path to sendmail executable", lErr)
}
opts.PathToExecutable = executablePath
}
return func(tr *msmtpd.Transaction) error {
if tr.IsFlagSet(DiscardFlag) {
tr.LogInfo("Message was discarded, nothing is send via %s", opts.PathToExecutable)
return nil
}
var err error
var args []string
var recipients []string
tr.LogDebug("Preparing to start sendmail executable at %s...", opts.PathToExecutable)
if tr.MailFrom.Name != "" {
args = append(args, "-F "+tr.MailFrom.Name)
}
args = append(args, "-f "+tr.MailFrom.Address)
if opts.UseMinusTFlag {
args = append(args, "-t")
} else {
if len(tr.Aliases) > 0 {
for i := range tr.Aliases {
recipients = append(recipients, tr.Aliases[i].Address)
}
} else {
for i := range tr.RcptTo {
recipients = append(recipients, tr.RcptTo[i].Address)
}
}
args = append(args, strings.Join(recipients, ","))
}
cmd := exec.CommandContext(tr.Context(), opts.PathToExecutable, args...)
tr.LogDebug("Preparing to execute %s...", cmd.String())
cmd.Stdin = bytes.NewBuffer(tr.Body)
output, err := cmd.CombinedOutput()
if err != nil {
tr.LogError(err, "while executing sendmail command")
return TemporaryError
}
tr.LogDebug("Sendmail output is %s", string(output))
if cmd.ProcessState.Success() {
return nil
}
return TemporaryError
}
}