lotus/sendmail/sendmail.go

43 lines
723 B
Go
Raw Normal View History

2023-09-11 01:33:08 +01:00
package sendmail
2023-08-13 03:04:16 +01:00
import (
2023-08-23 19:14:47 +01:00
"github.com/emersion/go-message/mail"
2023-08-23 19:12:54 +01:00
"os/exec"
2023-08-13 03:04:16 +01:00
)
2023-09-11 01:46:58 +01:00
type SendMail struct {
2023-09-11 01:33:08 +01:00
SendMailCommand string `json:"send_mail_command"`
2023-08-13 03:04:16 +01:00
}
type Mail struct {
2023-08-23 19:14:47 +01:00
From *mail.Address
2023-08-23 19:12:54 +01:00
Body []byte
2023-08-13 03:04:16 +01:00
}
2023-09-11 01:33:08 +01:00
var execCommand = exec.Command
2023-08-21 00:26:22 +01:00
2023-09-11 01:46:58 +01:00
func (s *SendMail) Send(mail *Mail) error {
2023-08-23 19:12:54 +01:00
// start sendmail caller
2023-09-11 01:33:08 +01:00
if s.SendMailCommand == "" {
s.SendMailCommand = "/usr/sbin/sendmail"
}
sendMail := execCommand(s.SendMailCommand, "-f", mail.From.Address, "-t")
2023-08-23 19:12:54 +01:00
inPipe, err := sendMail.StdinPipe()
2023-08-13 03:04:16 +01:00
if err != nil {
return err
}
2023-08-23 19:12:54 +01:00
// write message body
_, err = inPipe.Write(mail.Body)
if err != nil {
return err
2023-08-13 03:04:16 +01:00
}
2023-08-23 21:55:07 +01:00
err = inPipe.Close()
if err != nil {
return err
}
2023-09-11 01:33:08 +01:00
// run command
2023-08-23 21:55:07 +01:00
return sendMail.Run()
2023-08-13 03:04:16 +01:00
}