mirror of https://git.sr.ht/~rjarry/aerc
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package send
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"os/exec"
|
|
|
|
"git.sr.ht/~rjarry/go-opt/v2"
|
|
"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
|
|
}
|