~gjabell/mfn

ref: 0e591347646ee327a47c86a36c2dddc2bf5af305 mfn/email.go -rw-r--r-- 2.8 KiB
0e591347Galen Abell Fix email date header not being set 1 year, 5 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
package main

import (
	"bytes"
	"crypto/tls"
	"errors"
	"io"
	gomail "net/mail"
	"strings"
	"time"

	"github.com/emersion/go-message/mail"
	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"
	mf "miniflux.app/client"
)

type Email struct {
	subjectTemplate *Template
	bodyTemplate    *Template
	emailTo         string
	emailFrom       string
	username        string
	password        string
	server          string
	startTLS        bool
}

func NewEmail(conf *Config) *Email {
	ec := conf.EmailConfig

	return &Email{
		subjectTemplate: ec.SubjectTemplate,
		bodyTemplate:    ec.BodyTemplate,
		emailTo:         ec.EmailTo,
		emailFrom:       ec.EmailFrom,
		username:        ec.Username,
		password:        ec.Password,
		server:          ec.Server,
		startTLS:        ec.StartTLS,
	}
}

func (e *Email) Notify(entry *mf.Entry) error {
	var subject, body bytes.Buffer
	if err := e.subjectTemplate.Execute(&subject, entry); err != nil {
		return err
	}

	if err := e.bodyTemplate.Execute(&body, entry); err != nil {
		return err
	}

	host := e.server
	serverName := e.server
	if !strings.ContainsRune(host, ':') {
		host = host + ":587" // Default to submission port
	} else {
		serverName = host[:strings.IndexRune(host, ':')]
	}

	auth := sasl.NewPlainClient("", e.username, e.password)

	toAddr, err := gomail.ParseAddress(e.emailTo)
	if err != nil {
		return err
	}

	fromAddr, err := gomail.ParseAddress(e.emailFrom)
	if err != nil {
		return err
	}

	conn, err := smtp.Dial(host)
	if err != nil {
		return err
	}
	defer conn.Close()
	if sup, _ := conn.Extension("STARTTLS"); sup {
		if !e.startTLS {
			return errors.New("STARTTLS is supported by this server, " +
				"but is not set in config. " +
				"Add starttls=true under the email configuration.")
		}
		if err = conn.StartTLS(&tls.Config{
			ServerName: serverName,
		}); err != nil {
			return err
		}
	} else {
		if e.startTLS {
			return errors.New("STARTTLS requested, but not supported " +
				"by this SMTP server. Is someone tampering with your " +
				"connection?")
		}
	}
	if err := conn.Auth(auth); err != nil {
		return err
	}

	if err := conn.Mail(fromAddr.Address, nil); err != nil {
		return err
	}
	if err := conn.Rcpt(toAddr.Address); err != nil {
		return err
	}

	wc, err := conn.Data()
	if err != nil {
		return err
	}
	defer wc.Close()

	var header mail.Header
	header.SetAddressList("To", []*mail.Address{(*mail.Address)(toAddr)})
	header.SetAddressList("From", []*mail.Address{(*mail.Address)(fromAddr)})
	header.SetSubject(subject.String())
	header.SetDate(time.Now())
	header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})

	w, err := mail.CreateSingleInlineWriter(wc, header)
	if err != nil {
		return err
	}
	defer w.Close()

	if _, err := io.Copy(w, &body); err != nil {
		return err
	}

	return nil
}