diff options
Diffstat (limited to 'lib/send/sendmail.go')
-rw-r--r-- | lib/send/sendmail.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/send/sendmail.go b/lib/send/sendmail.go new file mode 100644 index 00000000..b267721e --- /dev/null +++ b/lib/send/sendmail.go @@ -0,0 +1,55 @@ +package send + +import ( + "fmt" + "io" + "net/url" + "os/exec" + + "git.sr.ht/~rjarry/go-opt" + "github.com/emersion/go-message/mail" + "github.com/pkg/errors" +) + +type sendmailSender struct { + cmd *exec.Cmd + stdin io.WriteCloser +} + +func (s *sendmailSender) Write(p []byte) (int, error) { + return s.stdin.Write(p) +} + +func (s *sendmailSender) Close() error { + se := s.stdin.Close() + ce := s.cmd.Wait() + if se != nil { + return se + } + return ce +} + +func newSendmailSender(uri *url.URL, rcpts []*mail.Address) (io.WriteCloser, error) { + args := opt.SplitArgs(uri.Path) + if len(args) == 0 { + return nil, fmt.Errorf("no command specified") + } + bin := args[0] + rs := make([]string, len(rcpts)) + for i := range rcpts { + rs[i] = rcpts[i].Address + } + args = append(args[1:], rs...) + cmd := exec.Command(bin, args...) + s := &sendmailSender{cmd: cmd} + var err error + s.stdin, err = s.cmd.StdinPipe() + if err != nil { + return nil, errors.Wrap(err, "cmd.StdinPipe") + } + err = s.cmd.Start() + if err != nil { + return nil, errors.Wrap(err, "cmd.Start") + } + return s, nil +} |