-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.go
210 lines (189 loc) · 4.98 KB
/
mail.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
package main
import (
"fmt"
"hash/fnv"
"io"
"net/url"
"os/exec"
"strconv"
"strings"
"time"
"github.com/c-14/grue/config"
"github.com/jaytaylor/html2text"
"github.com/mmcdole/gofeed"
"gopkg.in/gomail.v2"
)
type Email struct {
FromName string
SenderName string
FromAddress string
Recipient string
Date time.Time
Subject string
UserAgent string
ListId string
FeedURL string
ItemURI string
Body string
}
func (email *Email) setFrom(feedName string, feed *gofeed.Feed, item *gofeed.Item, account config.AccountConfig, conf *config.GrueConfig) {
var author gofeed.Person
if item.Author != nil {
author = *item.Author
} else if feed.Author != nil {
author = *feed.Author
}
if author.Name == "" {
author.Name = feedName
}
r := strings.NewReplacer("{name}", feedName, "{title}", feed.Title,
"{author}", author.Name)
if account.NameFormat != nil {
email.FromName = r.Replace(*account.NameFormat)
} else {
email.FromName = r.Replace(conf.NameFormat)
}
if author.Email == "" {
email.FromAddress = conf.FromAddress
} else {
email.FromAddress = author.Email
email.SenderName = conf.FromAddress
}
}
func (email *Email) setUserAgent(conf *config.GrueConfig) {
if conf.UserAgent != "" {
r := strings.NewReplacer("{version}", version)
email.UserAgent = r.Replace(conf.UserAgent)
}
}
// hash is a utility function to create a string representation of the
// hash of the input string.
func hash(s string) string {
h := fnv.New32a()
h.Write([]byte(s))
return fmt.Sprintf("%d", h.Sum32())
}
// See RFC2919 for details on List-Id header
func (email *Email) setListId(feedName, feedURI string, conf *config.GrueConfig) {
if conf.ListIdFormat == "" {
return
}
var host string
if u, err := url.Parse(email.FeedURL); err == nil {
host = u.Hostname()
}
r := strings.NewReplacer("{name}", feedName, "{urihash}", hash(feedURI),
"{namehash}", hash(feedName), "{host}", host)
email.ListId = r.Replace(conf.ListIdFormat)
}
func (email *Email) Send(sender gomail.Sender) error {
m := email.format()
return gomail.Send(sender, m)
}
func (email *Email) format() *gomail.Message {
var err error
m := gomail.NewMessage()
m.SetAddressHeader("From", email.FromAddress, email.FromName)
if email.SenderName != "" {
// if UserAgent is "", name portion of address is omitted
m.SetAddressHeader("Sender", email.FromAddress, email.UserAgent)
}
m.SetHeader("To", email.Recipient)
m.SetHeader("Subject", email.Subject)
m.SetDateHeader("Date", email.Date)
m.SetDateHeader("X-Date", time.Now())
if email.UserAgent != "" {
m.SetHeader("User-Agent", email.UserAgent)
}
if email.ListId != "" {
m.SetHeader("List-Id", email.ListId)
}
m.SetHeader("X-RSS-Feed", email.FeedURL)
m.SetHeader("X-RSS-URI", email.ItemURI)
bodyPlain, err := html2text.FromString(email.Body)
if err != nil {
fmt.Printf("Failed to parse text as HTML: %v", email.Subject)
m.SetBody("text/html", email.Body)
} else {
m.SetBody("text/plain", bodyPlain)
}
return m
}
func createEmail(feedName string, feed *gofeed.Feed, item *gofeed.Item, date time.Time, account config.AccountConfig, conf *config.GrueConfig) *Email {
email := new(Email)
email.setFrom(feedName, feed, item, account, conf)
email.Recipient = conf.Recipient
email.Subject = item.Title
email.Date = date
email.setUserAgent(conf)
email.FeedURL = account.URI
email.ItemURI = item.Link
if item.Content != "" {
email.Body = item.Content
} else {
email.Body = item.Description
}
email.setListId(feedName, account.URI, conf)
return email
}
type SendmailSender struct{}
func (sender SendmailSender) Send(from string, to []string, msg io.WriterTo) error {
cmd := exec.Command("sendmail", "-oi", "-t")
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
_, err = msg.WriteTo(stdin)
if err != nil {
stdin.Close()
return err
}
stdin.Close()
return cmd.Wait()
}
type SmtpSender struct {
dialer *gomail.Dialer
}
func setupDialer(server string, user, pass *string) (gomail.Sender, error) {
var sender SmtpSender
var hostname string
var port int
var err error
parts := strings.Split(server, ":")
if len(parts) > 2 {
return nil, fmt.Errorf("%s not a valid hostname\n", server)
} else if len(parts) == 1 {
hostname = parts[0]
port = 587
} else {
hostname = parts[0]
port, err = strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("Failed to parse port: %v\n", err)
}
}
if user != nil {
sender.dialer = gomail.NewDialer(hostname, port, *user, *pass)
} else {
sender.dialer = gomail.NewDialer(hostname, port, "", "")
}
return sender, nil
}
func (sender SmtpSender) Send(from string, to []string, msg io.WriterTo) error {
s, err := sender.dialer.Dial()
if err != nil {
return err
}
defer s.Close()
return s.Send(from, to, msg)
}
func setupMailer(conf *config.GrueConfig) (gomail.Sender, error) {
if conf.SmtpServer != nil {
return setupDialer(*conf.SmtpServer, conf.SmtpUser, conf.SmtpPass)
}
return SendmailSender{}, nil
}