This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
email.go
169 lines (150 loc) · 3.13 KB
/
email.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
package photoshare
import (
"bytes"
"fmt"
"github.com/juju/errgo"
"log"
"net/http"
"net/smtp"
"path"
"strings"
"text/template"
)
type message struct {
subject string
body []byte
to []string
from string
}
func (msg *message) String() string {
return fmt.Sprintf("From:%s\r\nTo:%s\r\nSubject: %s\r\n\r\n%s",
msg.from,
strings.Join(msg.to, ", "),
msg.subject,
string(msg.body),
)
}
type mailer struct {
sender mailSender
cfg *config
defaultFromAddress string
templates map[string]*template.Template
}
func (m *mailer) send(msg *message) error {
return m.sender.send(msg)
}
func (m *mailer) parseTemplate(name string) (*template.Template, error) {
var (
t *template.Template
ok bool
err error
)
t, ok = m.templates[name]
if !ok {
t, err = template.ParseFiles(path.Join(m.cfg.TemplatesDir, name+".tmpl"))
if err != nil {
return nil, errgo.Mask(err)
}
m.templates[name] = t
}
return t, nil
}
// Creates a new message from a template; message body set to rendered template
func (m *mailer) messageFromTemplate(subject string,
to []string,
from string,
templateName string,
data interface{}) (*message, error) {
msg := &message{
subject: subject,
to: to,
from: from,
}
b := &bytes.Buffer{}
t, err := m.parseTemplate(templateName)
if err != nil {
return nil, err
}
if err := t.Execute(b, data); err != nil {
return nil, errgo.Mask(err)
}
msg.body = b.Bytes()
return msg, nil
}
type mailSender interface {
send(*message) error
}
type smtpSender struct {
smtp.Auth
cfg *config
}
func (s *smtpSender) send(msg *message) error {
return errgo.Mask(smtp.SendMail(
fmt.Sprintf("%s:%d", s.cfg.SmtpHost, s.cfg.SmtpPort),
s.Auth,
msg.from,
msg.to,
[]byte(msg.String()),
))
}
type fakeSender struct{}
func (s *fakeSender) send(msg *message) error {
log.Println(msg)
return nil
}
func newSmtpSender(cfg *config) *smtpSender {
s := &smtpSender{cfg: cfg}
s.Auth = smtp.PlainAuth("",
cfg.SmtpName,
cfg.SmtpPassword,
cfg.SmtpHost,
)
return s
}
func newMailer(cfg *config) *mailer {
mailer := &mailer{cfg: cfg}
if cfg.SmtpName == "" {
log.Println("WARNING: using fake mailer, messages will not be sent by SMTP. " +
"Set SMTP_NAME and SMTP_PASSWORD in environment to enable.")
mailer.sender = &fakeSender{}
} else {
mailer.sender = newSmtpSender(cfg)
}
mailer.defaultFromAddress = cfg.SmtpDefaultSender
mailer.templates = make(map[string]*template.Template)
return mailer
}
func (m *mailer) sendResetPasswordMail(user *user, recoveryCode string, r *http.Request) error {
msg, err := m.messageFromTemplate(
"Reset your password",
[]string{user.Email},
m.defaultFromAddress,
"recover_pass",
&struct {
Name string
RecoveryCode string
URL string
}{
user.Name,
recoveryCode,
getBaseURL(r),
},
)
if err != nil {
return err
}
return m.send(msg)
}
func (m *mailer) sendWelcomeMail(user *user) error {
msg, err := m.messageFromTemplate(
"Welcome to photoshare!",
[]string{user.Email},
m.defaultFromAddress,
"signup",
user,
)
if err != nil {
return err
}
return m.send(msg)
}