lotus/smtp/smtp.go

48 lines
826 B
Go
Raw Normal View History

2023-08-13 03:04:16 +01:00
package smtp
import (
"bytes"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-smtp"
)
type Smtp struct {
Server string `yaml:"server"`
}
type Mail struct {
From string
Deliver []string
Body []byte
2023-08-13 03:04:16 +01:00
}
2023-08-21 00:26:22 +01:00
var defaultDialer = smtp.Dial
2023-08-13 03:04:16 +01:00
func (s *Smtp) Send(mail *Mail) error {
// dial smtp server
2023-08-21 00:26:22 +01:00
smtpClient, err := defaultDialer(s.Server)
2023-08-13 03:04:16 +01:00
if err != nil {
return err
}
// use a reader to send bytes
r := bytes.NewReader(mail.Body)
2023-08-13 03:04:16 +01:00
// send mail
return smtpClient.SendMail(mail.From, mail.Deliver, r)
2023-08-13 03:04:16 +01:00
}
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
}