~bigbes/core-go

ref: 78cfc5d87a7dcb762467aaa82fcc80cb4e24974b core-go/email/send.go -rw-r--r-- 3.9 KiB
78cfc5d8 — Robin Jarry email: allow sending raw emails 1 year, 8 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package email

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

	"github.com/emersion/go-message"
	_ "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)

	if mailconf.enctype == "tls" {
		c, err = smtp.DialTLS(addr, nil)
	} else if mailconf.enctype == "starttls" {
		c, err = smtp.DialStartTLS(addr, nil)
	} else {
		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
}

// Sends a raw email. Blocks until it's sent or an error occurs.
func SendRaw(ctx context.Context, email *message.Entity, to []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
	}

	var writer io.WriteCloser
	var err error

	err = worker.client.Mail(worker.sender.Address, nil)
	if err != nil {
		goto end
	}
	for _, rcpt := range to {
		if err := worker.client.Rcpt(rcpt, nil); err != nil {
			return err
		}
	}
	writer, err = worker.client.Data()
	if err != nil {
		goto end
	}
	err = email.WriteTo(writer)
	if err != nil {
		writer.Close()
		goto end
	}
	err = writer.Close()
end:
	if err != nil {
		worker.client.Close()
		worker.client = nil
	}
	return err
}