internal/submission/client.go (view raw)
1package submission
2
3import (
4 "crypto/tls"
5 "errors"
6 "io"
7 "net"
8 "strconv"
9
10 "postern/internal/policy"
11
12 "github.com/emersion/go-sasl"
13 "github.com/emersion/go-smtp"
14)
15
16func shipMessage(from string, to []string, r io.Reader, relayConfig policy.RelayConfig) error {
17 if relayConfig.Host == "" {
18 return errors.New("no relay configured")
19 }
20 conn, err := net.Dial("tcp", net.JoinHostPort(relayConfig.Host, strconv.Itoa(relayConfig.Port)))
21 if err != nil {
22 return err
23 }
24
25 c, err := smtp.NewClientStartTLS(conn, &tls.Config{
26 ServerName: relayConfig.Host,
27 })
28 if err != nil {
29 return err
30 }
31 defer func(c *smtp.Client) {
32 _ = c.Close()
33 }(c)
34
35 err = c.Auth(sasl.NewPlainClient("", relayConfig.Username, relayConfig.Password))
36 if err != nil {
37 return err
38 }
39
40 return c.SendMail(from, to, r)
41}