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 {
|
2023-08-23 16:17:01 +01:00
|
|
|
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
|
2023-08-23 16:17:01 +01:00
|
|
|
r := bytes.NewReader(mail.Body)
|
2023-08-13 03:04:16 +01:00
|
|
|
|
|
|
|
// send mail
|
2023-08-23 16:17:01 +01:00
|
|
|
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
|
|
|
|
}
|