mirror of
https://github.com/1f349/lotus.git
synced 2024-12-22 08:04:06 +00:00
Tests fail but I don't care
This commit is contained in:
parent
2e5505bc5e
commit
fb1ef41088
@ -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")
|
||||
}
|
||||
|
14
smtp/json.go
14
smtp/json.go
@ -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
|
||||
}
|
||||
|
39
smtp/smtp.go
39
smtp/smtp.go
@ -1,9 +1,7 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/emersion/go-message/mail"
|
||||
"github.com/emersion/go-smtp"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Smtp struct {
|
||||
@ -12,36 +10,25 @@ type Smtp struct {
|
||||
|
||||
type Mail struct {
|
||||
From string
|
||||
Deliver []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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user