~bigbes/core-go

ref: cc7fd1d7d60de614649d14fbf5e3fe04ece8f80b core-go/email/send.go -rw-r--r-- 3.0 KiB
cc7fd1d7 — Simon Martin model: support "unset" RID values 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package email

import (
	"context"
	"fmt"
	"io"
	"strconv"

	_ "github.com/emersion/go-message/charset"
	"github.com/emersion/go-message/mail"
	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"

	"git.sr.ht/~sircmpwn/core-go/config"
)

type mailConfig struct {
	host string
	port int
	from string

	enctype  string
	authtype string

	user string
	pass string
}

func mailSetup(ctx context.Context) (*smtp.Client, *mail.Address, error) {
	conf := config.ForContext(ctx)
	mailconf := &mailConfig{enctype: "starttls", authtype: "plain"}
	var err error

	portStr, ok := conf.Get("mail", "smtp-port")
	if !ok {
		panic(fmt.Errorf("[mail]smtp-port unset"))
	}
	mailconf.port, err = strconv.Atoi(portStr)
	if err != nil {
		panic(fmt.Errorf("unable to parse [mail]smtp-port (must be integer)"))
	}

	if mailconf.host, ok = conf.Get("mail", "smtp-host"); !ok {
		panic(fmt.Errorf("missing SMTP configuration options [smtp-host]"))
	}
	if mailconf.from, ok = conf.Get("mail", "smtp-from"); !ok {
		panic(fmt.Errorf("missing SMTP configuration options [smtp-from]"))
	}

	sender, err := mail.ParseAddress(mailconf.from)
	if err != nil {
		panic(err)
	}

	if enctype, ok := conf.Get("mail", "smtp-encryption"); ok {
		switch enctype {
		case "starttls", "tls", "insecure":
			mailconf.enctype = enctype
		default:
			panic(fmt.Errorf("invalid SMTP configuration value for [smtp-encryption]"))
		}
	}

	if authtype, ok := conf.Get("mail", "smtp-auth"); ok {
		switch authtype {
		case "none", "plain":
			mailconf.authtype = authtype
		default:
			panic(fmt.Errorf("invalid SMTP configuration value for [smtp-auth]"))
		}
	}
	if mailconf.authtype == "plain" {
		if mailconf.user, ok = conf.Get("mail", "smtp-user"); !ok {
			panic(fmt.Errorf("missing SMTP configuration options [smtp-user]"))
		}
		if mailconf.pass, ok = conf.Get("mail", "smtp-password"); !ok {
			panic(fmt.Errorf("missing SMTP configuration options [smtp-password]"))
		}
	}

	var c *smtp.Client
	addr := fmt.Sprintf("%s:%d", mailconf.host, mailconf.port)

	switch mailconf.enctype {
	case "tls":
		c, err = smtp.DialTLS(addr, nil)
	case "starttls":
		c, err = smtp.DialStartTLS(addr, nil)
	default:
		c, err = smtp.Dial(addr)
	}
	if err != nil {
		return nil, nil, err
	}

	if err = c.Hello("localhost"); err != nil {
		return nil, nil, err
	}

	if mailconf.authtype == "plain" {
		auth := sasl.NewPlainClient("", mailconf.user, mailconf.pass)
		if ok, _ := c.Extension("AUTH"); !ok {
			panic(fmt.Errorf("smtp: server doesn't support AUTH"))
		}
		if err = c.Auth(auth); err != nil {
			return nil, nil, err
		}
	}

	return c, sender, nil
}

// Sends an email. Blocks until it's sent or an error occurs.
func Send(ctx context.Context, msg io.Reader, rcpts []string) error {
	worker := ForContext(ctx)

	if worker.client == nil || worker.client.Noop() != nil {
		worker.client = nil

		c, sender, err := mailSetup(ctx)
		if err != nil {
			return err
		}
		worker.client = c
		worker.sender = sender
	}

	err := worker.client.SendMail(worker.sender.Address, rcpts, msg)
	if err != nil {
		worker.client.Close()
		worker.client = nil
	}
	return err
}