-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
emailer.go
442 lines (358 loc) · 9.64 KB
/
emailer.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Package emailer is responsible for sending out a feed
// item via email.
//
// There are two ways emails are sent:
//
// 1. Via spawning /usr/sbin/sendmail.
//
// 2. Via SMTP.
//
// The choice is made based upon the presence of environmental
// variables.
package emailer
import (
"bytes"
"errors"
"fmt"
"html"
"io"
"log/slog"
"mime/quotedprintable"
"net/smtp"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/mmcdole/gofeed"
"github.com/skx/rss2email/configfile"
"github.com/skx/rss2email/state"
emailtemplate "github.com/skx/rss2email/template"
"github.com/skx/rss2email/withstate"
)
// Emailer stores our state
type Emailer struct {
// Feed is the source feed from which this item came
feed *gofeed.Feed
// Item is the feed item itself
item withstate.FeedItem
// Config options for the feed.
opts []configfile.Option
// logger contains a dedicated logging object
logger *slog.Logger
}
// New creates a new Emailer object.
//
// The arguments are the source feed, the feed item which is being notified,
// and any associated configuration values from the source feed.
func New(feed *gofeed.Feed, item withstate.FeedItem, opts []configfile.Option, log *slog.Logger) *Emailer {
// Default options
obj := &Emailer{feed: feed, item: item, opts: opts}
// Create a new logger
obj.logger = log.With(
slog.Group("email",
slog.String("link", item.Link),
slog.String("title", item.Title)))
return obj
}
// env returns the contents of an environmental variable.
//
// This function exists to be used by our email-template.
func env(s string) string {
return (os.Getenv(s))
}
// split converts a string to an array.
//
// This function exists to be used by our email-template.
func split(in string, delim string) []string {
return strings.Split(in, delim)
}
// loadTemplate loads the template used for sending the email notification.
func (e *Emailer) loadTemplate() (*template.Template, error) {
// Load the default template from the embedded resource.
content := emailtemplate.EmailTemplate()
// The directory within which we maintain state
stateDir := state.Directory()
// The path to the overridden template
override := filepath.Join(stateDir, "email.tmpl")
// If a per feed template was set, get it here.
for _, opt := range e.opts {
if opt.Name == "template" {
override = filepath.Join(stateDir, opt.Value)
}
}
// If the file exists, use it.
_, err := os.Stat(override)
if !os.IsNotExist(err) {
content, err = os.ReadFile(override)
if err != nil {
e.logger.Debug("could not load template override file",
slog.String("file", override),
slog.String("error", err.Error()))
return nil, fmt.Errorf("failed to read %s: %s", override, err.Error())
}
}
//
// Function map allows exporting functions to the template
//
funcMap := template.FuncMap{
"env": env,
"quoteprintable": toQuotedPrintable,
"split": split,
"encodeHeader": encodeHeader,
}
tmpl := template.Must(template.New("email.tmpl").Funcs(funcMap).Parse(string(content)))
return tmpl, nil
}
// toQuotedPrintable will convert the given input-string to a
// quoted-printable format. This is required for our MIME-part
// body.
//
// NOTE: We use this function both directly, and from within our template.
func toQuotedPrintable(s string) (string, error) {
var ac bytes.Buffer
w := quotedprintable.NewWriter(&ac)
_, err := w.Write([]byte(s))
if err != nil {
return "", err
}
err = w.Close()
if err != nil {
return "", err
}
return ac.String(), nil
}
// Encode email header entries to comply with the 7bit ASCII restriction
// of RFC 5322 according to RFC 2047.
//
// We use quotedprintable encoding only if necessary.
func encodeHeader(s string) string {
se, err := toQuotedPrintable(s)
if (err != nil) || (len(se) == len(s)) {
return s
}
se = strings.Replace(strings.Replace(se, "?", "=3F", -1), " ", "=20", -1)
se = strings.Replace(se, "=\r\n", "", -1) // remove soft line breaks
return "=?utf-8?Q?" + se + "?="
}
// Sendmail is a simple function that emails the given address.
//
// We send a MIME message with both a plain-text and a HTML-version of the
// message. This should be nicer for users.
func (e *Emailer) Sendmail(addresses []string, textstr string, htmlstr string) error {
var err error
//
// Ensure we have a recipient.
//
if len(addresses) < 1 {
e.logger.Error("missing recipient address")
e := errors.New("empty recipient address, did you not setup a recipient?")
return e
}
//
// Process each address
//
for _, addr := range addresses {
//
// Here is a temporary structure we'll use to popular our email
// template.
//
type TemplateParms struct {
Feed string
FeedTitle string
From string
HTML string
Link string
Subject string
Tag string
Text string
To string
// In case people need access to fields
// we've not wrapped/exported explicitly
RSSFeed *gofeed.Feed
RSSItem withstate.FeedItem
}
//
// Populate it appropriately.
//
var x TemplateParms
x.Feed = e.feed.Link
x.FeedTitle = e.feed.Title
x.From = addr
x.Link = e.item.Link
x.Subject = e.item.Title
x.To = addr
x.RSSFeed = e.feed
x.RSSItem = e.item
x.Tag = e.item.Tag
// The real meat of the mail is the text & HTML
// parts. They need to be encoded, unconditionally.
x.Text, err = toQuotedPrintable(textstr)
if err != nil {
return err
}
x.HTML, err = toQuotedPrintable(html.UnescapeString(htmlstr))
if err != nil {
return err
}
//
// Load the template we're going to render.
//
var t *template.Template
t, err = e.loadTemplate()
if err != nil {
return err
}
//
// Render the template into the buffer.
//
buf := &bytes.Buffer{}
err = t.Execute(buf, x)
if err != nil {
return err
}
//
// Are we sending via SMTP?
//
if e.isSMTP() {
e.logger.Debug("preparing to send email",
slog.String("recipient", addr),
slog.String("method", "smtp"))
err := e.sendSMTP(addr, buf.Bytes())
if err != nil {
e.logger.Warn("error sending email",
slog.String("recipient", addr),
slog.String("method", "smtp"),
slog.String("error", err.Error()))
return err
}
e.logger.Debug("email sent",
slog.String("recipient", addr),
slog.String("method", "smtp"))
} else {
e.logger.Debug("preparing to send email",
slog.String("recipient", addr),
slog.String("method", "sendmail"))
err := e.sendSendmail(addr, buf.Bytes())
if err != nil {
e.logger.Warn("error sending email",
slog.String("recipient", addr),
slog.String("method", "sendmail"),
slog.String("error", err.Error()))
return err
}
e.logger.Debug("email sent",
slog.String("recipient", addr),
slog.String("method", "sendmail"))
}
}
e.logger.Debug("emails sent",
slog.Int("recipients", len(addresses)))
return nil
}
// isSMTP determines whether we should use SMTP to send the email.
//
// We just check to see that the obvious mandatory parameters are set in the
// environment. If they're wrong we'll get an error at delivery time, as
// expected.
func (e *Emailer) isSMTP() bool {
// Mandatory environmental variables
vars := []string{"SMTP_HOST", "SMTP_USERNAME", "SMTP_PASSWORD"}
for _, name := range vars {
if os.Getenv(name) == "" {
return false
}
}
return true
}
// sendSMTP sends the content of the email to the destination address
// via SMTP.
func (e *Emailer) sendSMTP(to string, content []byte) error {
// basics
host := os.Getenv("SMTP_HOST")
port := os.Getenv("SMTP_PORT")
p := 587
if port != "" {
n, err := strconv.Atoi(port)
if err != nil {
e.logger.Warn("error converting SMTP_PORT to integer",
slog.String("port", port),
slog.String("error", err.Error()))
return err
}
p = n
}
// auth
user := os.Getenv("SMTP_USERNAME")
pass := os.Getenv("SMTP_PASSWORD")
// Authenticate
auth := smtp.PlainAuth("", user, pass, host)
// Get the mailserver
addr := fmt.Sprintf("%s:%d", host, p)
// Send the mail
err := smtp.SendMail(addr, auth, to, []string{to}, content)
return err
}
// sendSendmail sends the content of the email to the destination address
// via /usr/sbin/sendmail
func (e *Emailer) sendSendmail(addr string, content []byte) error {
// Get the command to run.
sendmail := exec.Command("/usr/sbin/sendmail", "-i", "-f", addr, addr)
stdin, err := sendmail.StdinPipe()
if err != nil {
e.logger.Warn("error creating STDIN pipe to sendmail",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
//
// Get the output pipe.
//
stdout, err := sendmail.StdoutPipe()
if err != nil {
e.logger.Warn("error creating STDOUT pipe to sendmail",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
//
// Run the command, and pipe in the rendered template-result
//
err = sendmail.Start()
if err != nil {
e.logger.Warn("error starting sendmail",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
_, err = stdin.Write(content)
if err != nil {
e.logger.Warn("error writing to sendmail pipe",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
stdin.Close()
//
// Read the output of Sendmail.
//
_, err = io.ReadAll(stdout)
if err != nil {
e.logger.Warn("error reading from sendmail pipe",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
//
// Wait for the command to complete.
//
err = sendmail.Wait()
if err != nil {
e.logger.Warn("error awaiting sendmail completion",
slog.String("recipient", addr),
slog.String("error", err.Error()))
return err
}
return err
}