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

View File

@ -1,9 +1,7 @@
package smtp
import (
"bytes"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-smtp"
"os/exec"
)
type Smtp struct {
@ -11,37 +9,26 @@ type Smtp struct {
}
type Mail struct {
From string
Deliver []string
Body []byte
From string
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 {
// dial smtp server
smtpClient, err := defaultDialer(s.Server)
// start sendmail caller
sendMail := execSendMail(mail.From)
inPipe, err := sendMail.StdinPipe()
if err != nil {
return err
}
// use a reader to send bytes
r := bytes.NewReader(mail.Body)
// send mail
return smtpClient.SendMail(mail.From, mail.Deliver, r)
}
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
// write message body
_, err = inPipe.Write(mail.Body)
if err != nil {
return err
}
return inPipe.Close()
}