Tests fail but I don't care

This commit is contained in:
Melon 2023-08-23 19:12:54 +01:00
parent 2e5505bc5e
commit fb1ef41088
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 21 additions and 39 deletions

View File

@ -46,9 +46,6 @@ func (f *fakeSmtp) Send(mail *smtp.Mail) error {
if mail.From != f.from { if mail.From != f.from {
return fmt.Errorf("test fail: invalid from address") return fmt.Errorf("test fail: invalid from address")
} }
if !slices.Equal(mail.Deliver, f.deliver) {
return fmt.Errorf("test fail: invalid deliver slice")
}
if !slices.Equal(mail.Body, f.body) { if !slices.Equal(mail.Body, f.body) {
return fmt.Errorf("test fail: invalid message body") return fmt.Errorf("test fail: invalid message body")
} }

View File

@ -68,7 +68,7 @@ func (s Json) PrepareMail(now time.Time) (*Mail, error) {
} }
// save for use in the caller // save for use in the caller
from := addrFrom[0].Address from := addrFrom[0]
// set base headers // set base headers
var h mail.Header var h mail.Header
@ -78,6 +78,7 @@ func (s Json) PrepareMail(now time.Time) (*Mail, error) {
h.SetAddressList("Reply-To", addrReplyTo) h.SetAddressList("Reply-To", addrReplyTo)
h.SetAddressList("To", addrTo) h.SetAddressList("To", addrTo)
h.SetAddressList("Cc", addrCc) h.SetAddressList("Cc", addrCc)
h.SetAddressList("Bcc", addrBcc)
// set content type header // set content type header
switch s.BodyType { switch s.BodyType {
@ -94,16 +95,13 @@ func (s Json) PrepareMail(now time.Time) (*Mail, error) {
return nil, err return nil, err
} }
m := &Mail{
From: from,
Deliver: CreateSenderSlice(addrTo, addrCc, addrBcc),
}
out := new(bytes.Buffer) out := new(bytes.Buffer)
if err := entity.WriteTo(out); err != nil { if err := entity.WriteTo(out); err != nil {
return nil, err return nil, err
} }
m.Body = out.Bytes() return &Mail{
return m, nil From: from.String(),
Body: out.Bytes(),
}, nil
} }

View File

@ -1,9 +1,7 @@
package smtp package smtp
import ( import (
"bytes" "os/exec"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-smtp"
) )
type Smtp struct { type Smtp struct {
@ -12,36 +10,25 @@ type Smtp struct {
type Mail struct { type Mail struct {
From string From string
Deliver []string
Body []byte Body []byte
} }
var defaultDialer = smtp.Dial var execSendMail = func(from string) *exec.Cmd {
return exec.Command("/usr/lib/sendmail", "-f", from, "-t")
}
func (s *Smtp) Send(mail *Mail) error { func (s *Smtp) Send(mail *Mail) error {
// dial smtp server // start sendmail caller
smtpClient, err := defaultDialer(s.Server) sendMail := execSendMail(mail.From)
inPipe, err := sendMail.StdinPipe()
if err != nil { if err != nil {
return err return err
} }
// use a reader to send bytes // write message body
r := bytes.NewReader(mail.Body) _, err = inPipe.Write(mail.Body)
if err != nil {
// send mail return err
return smtpClient.SendMail(mail.From, mail.Deliver, r) }
} return inPipe.Close()
func CreateSenderSlice(to, cc, bcc []*mail.Address) []string {
a := make([]string, 0, len(to)+len(cc)+len(bcc))
for _, i := range to {
a = append(a, i.Address)
}
for _, i := range cc {
a = append(a, i.Address)
}
for _, i := range bcc {
a = append(a, i.Address)
}
return a
} }