mirror of
https://github.com/1f349/lotus.git
synced 2024-11-09 22:52:53 +00:00
43 lines
723 B
Go
43 lines
723 B
Go
package sendmail
|
|
|
|
import (
|
|
"github.com/emersion/go-message/mail"
|
|
"os/exec"
|
|
)
|
|
|
|
type SendMail struct {
|
|
SendMailCommand string `json:"send_mail_command"`
|
|
}
|
|
|
|
type Mail struct {
|
|
From *mail.Address
|
|
Body []byte
|
|
}
|
|
|
|
var execCommand = exec.Command
|
|
|
|
func (s *SendMail) Send(mail *Mail) error {
|
|
// start sendmail caller
|
|
if s.SendMailCommand == "" {
|
|
s.SendMailCommand = "/usr/sbin/sendmail"
|
|
}
|
|
sendMail := execCommand(s.SendMailCommand, "-f", mail.From.Address, "-t")
|
|
inPipe, err := sendMail.StdinPipe()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// write message body
|
|
_, err = inPipe.Write(mail.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = inPipe.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// run command
|
|
return sendMail.Run()
|
|
}
|