lotus/smtp/smtp.go

35 lines
539 B
Go
Raw Normal View History

2023-08-13 03:04:16 +01:00
package smtp
import (
2023-08-23 19:12:54 +01:00
"os/exec"
2023-08-13 03:04:16 +01:00
)
type Smtp struct {
Server string `yaml:"server"`
}
type Mail struct {
2023-08-23 19:12:54 +01:00
From string
Body []byte
2023-08-13 03:04:16 +01:00
}
2023-08-23 19:12:54 +01:00
var execSendMail = func(from string) *exec.Cmd {
return exec.Command("/usr/lib/sendmail", "-f", from, "-t")
}
2023-08-21 00:26:22 +01:00
2023-08-13 03:04:16 +01:00
func (s *Smtp) Send(mail *Mail) error {
2023-08-23 19:12:54 +01:00
// start sendmail caller
sendMail := execSendMail(mail.From)
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 19:12:54 +01:00
return inPipe.Close()
2023-08-13 03:04:16 +01:00
}