forked from jessevdk/webgl-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailer.go
222 lines (175 loc) · 5.11 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
/*
* Copyright (c) 2014 Jesse van den Kieboom. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"crypto/tls"
"fmt"
"log"
"net/smtp"
"strings"
"text/template"
"time"
)
const EmailBatchSize = 20
const EmailTemplateBody = `Date: {{.Date}}
To: {{.To.Name}} <{{.To.Address}}>
From: {{.From.Name}} <{{.From.Address}}>
Subject: New Publishing Token for '{{.Title}}'
Hi {{.To.Name}}!
You have requested a new token to publish a WebGL Playground document
on the gallery at {{.PublicHost}}, titled '{{.Title}}':
{{.Token}}
Please copy this token and use it to publish your playground document. The
token will expire within a few hours of not being used. Otherwise, note that
this token uniquely identifies your document and can be reused to make
modifications to the document at any time after its first Gallery.
With kind regards,
The WebGL Playground ({{.PublicHost}})
`
type EmailAddress struct {
Name string
Address string
}
type EmailInfo struct {
Date string
Title string
To EmailAddress
From EmailAddress
Token string
PublicHost string
}
type Email struct {
Info EmailInfo
Message []byte
}
type Emailer struct {
Template *template.Template
Emails chan Email
}
var emailer = Emailer{
Emails: make(chan Email, 4096),
}
func (e Emailer) handleResettableError(c *smtp.Client, msg string) bool {
log.Print(msg)
if err := c.Reset(); err != nil {
log.Printf("Failed to reset SMTP after failure, aborting: %v", err)
return false
}
return true
}
func (e Emailer) SMTPHost() string {
i := strings.LastIndex(options.SMTPAddress, ":")
if i >= 0 {
return options.SMTPAddress[:i]
}
return options.SMTPAddress
}
func (e Emailer) send(emails []Email) {
c, err := smtp.Dial(options.SMTPAddress)
if err != nil {
log.Printf("Failed to connect to SMTP host: %v", err)
return
}
defer c.Close()
if !options.SMTPDisableTLS {
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: e.SMTPHost()}
if err := c.StartTLS(config); err != nil {
log.Printf("Failed to STARTTLS SMTP: %v", err)
return
}
}
}
for _, email := range emails {
from := email.Info.From.Address
if err := c.Mail(from); err != nil {
log.Printf("Failed to MAIL SMTP as <%s>: %v", from, err)
continue
}
to := email.Info.To.Address
if err := c.Rcpt(to); err != nil {
if e.handleResettableError(c, fmt.Sprintf("Failed to RCPT SMTP as <%s>: %v", to, err)) {
continue
} else {
return
}
}
w, err := c.Data()
if err != nil {
if e.handleResettableError(c, fmt.Sprintf("Failed to open SMTP data channel: %v", err)) {
continue
} else {
return
}
}
if _, err := w.Write(email.Message); err != nil {
if e.handleResettableError(c, fmt.Sprintf("Failed to write to SMTP data channel: %v", err)) {
continue
} else {
return
}
}
if err := w.Close(); err != nil {
if e.handleResettableError(c, fmt.Sprintf("Failed to close SMTP data channel: %v", err)) {
continue
} else {
return
}
}
}
c.Quit()
}
func (e Emailer) run() {
for {
batch := make([]Email, 0, EmailBatchSize)
// Wait for at least one email to arrive
batch = append(batch, <-e.Emails)
// Continue to collect emails until batch is full, or N seconds have passed
timeout := time.NewTimer(5 * time.Second)
CollectionLoop:
for len(batch) < EmailBatchSize {
select {
case e := <-e.Emails:
batch = append(batch, e)
case <-timeout.C:
break CollectionLoop
}
}
timeout.Stop()
e.send(batch)
}
}
func init() {
var err error
emailer.Template, err = template.New("email").Parse(EmailTemplateBody)
if err != nil {
panic(err)
}
go emailer.run()
}