-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendemail.go
159 lines (133 loc) · 3.13 KB
/
sendemail.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
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"html"
"log"
"mime/quotedprintable"
"net/smtp"
"strings"
"text/template"
"github.com/mmcdole/gofeed"
)
const feedSubject = "[rss-email] feed"
const mailTemplate = `To: {{.To}}
Subject: {{.Subject}}
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
{{.Body}}
`
type templateParams struct {
To string
Subject string
Body string
}
func sendSubscription(config *emailConfig) error {
for to, userUrls := range userSubscriptions.m {
param := &bodyParam{}
var urlHashMap = make(map[string]string)
for url, userURLInfo := range *userUrls {
urlInfo, ok := subscription.m[url]
if !ok {
return errors.New("feed url not in map")
}
// make sure we have populated feed
if urlInfo.lastUpdate.IsZero() {
continue
}
filteredFeed, nextHash, err := filterFeed(urlInfo.feed, userURLInfo)
if err != nil {
log.Print(err)
continue
}
param.Feeds = append(param.Feeds, filteredFeed)
urlHashMap[url] = nextHash
}
// make sure we have contents to send
var itemNum int
for _, feed := range param.Feeds {
itemNum += len(feed.Items)
}
if itemNum == 0 {
return nil
}
param.Expect = len(*userUrls)
param.Actual = len(param.Feeds)
param.ShowErr = param.Expect != param.Actual
parsedFeed, err := parsefeed(param)
if err != nil {
log.Print(err)
continue
}
body, err := toQuotedPrintable(html.UnescapeString(parsedFeed))
if err != nil {
return err
}
if err := sendemail(config, to, feedSubject, body); err != nil {
return err
}
// Update visited hash
for url, hash := range urlHashMap {
(*userUrls)[url].LastHash = hash
}
}
return nil
}
func sendemail(config *emailConfig, to, subject, body string) error {
src := strings.ReplaceAll(mailTemplate, "\n", "\r\n")
t := template.Must(template.New("mailTemplate").Parse(src))
msg := &bytes.Buffer{}
params := templateParams{to, subject, body}
err := t.Execute(msg, params)
if err != nil {
return err
}
auth := LOGINAuth(config.username, config.password)
err = smtp.SendMail(config.smtpServer, auth, config.from, []string{to}, msg.Bytes())
if err != nil {
return err
}
log.Printf("one email sent to %s", to)
return nil
}
func toQuotedPrintable(s string) (string, error) {
var buf bytes.Buffer
w := quotedprintable.NewWriter(&buf)
_, err := w.Write([]byte(s))
if err != nil {
return "", err
}
err = w.Close()
if err != nil {
return "", err
}
return buf.String(), nil
}
func filterFeed(feed *gofeed.Feed, userURLInfo *userURLInfo) (*gofeed.Feed, string, error) {
var res = new(gofeed.Feed)
*res = *feed
nextHash := ""
for index, item := range feed.Items {
hasher := sha1.New()
_, err := hasher.Write([]byte(item.Title))
if err != nil {
return nil, "", nil
}
hasher.Write([]byte(item.Published))
if err != nil {
return nil, "", nil
}
hashBytes := hasher.Sum(nil)
hexSha1 := hex.EncodeToString(hashBytes)
if index == 0 {
nextHash = hexSha1
}
if hexSha1 == userURLInfo.LastHash {
res.Items = res.Items[:index]
break
}
}
return res, nextHash, nil
}