2023-08-13 03:04:16 +01:00
|
|
|
package smtp
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type Smtp struct {
|
|
|
|
Server string `yaml:"server"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
2023-08-23 19:14:47 +01:00
|
|
|
sendMail := execSendMail(mail.From.String())
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendMail.Run()
|
2023-08-13 03:04:16 +01:00
|
|
|
}
|