-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
105 lines (95 loc) · 2.64 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
package main
import (
"net/smtp"
"bytes"
"html/template"
)
// Struct to hold the information for a email message.
type EmailRequest struct {
from string
to []string
subject string
body string
server string
}
func NewEmailRequest(to []string, from string, subject, server, body string) *EmailRequest {
return &EmailRequest{
to: to,
from: from,
subject: subject,
body: body,
server: server,
}
}
// Send an email based on the EmailRequest.
func (r *EmailRequest) SendEmail() error {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := "Subject: " + r.subject + "\n"
addr := r.server
emailFrom := r.from
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
c.Mail(emailFrom)
for _, recipient := range r.to {
c.Rcpt(recipient)
}
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
buf := bytes.NewBufferString(subject + mime + "\n" + r.body)
if _, err = buf.WriteTo(wc); err != nil {
return err
}
return nil
}
// Parse and execute the html/template. There is a function called "zebra" that will handle the
// alternate row colors for each stat line. This stores the output of the template execution into the
// EmailRequest body field.
func (r *EmailRequest) ParseTemplate(templateFileName string, data interface{}) error {
t := template.New("")
t.Funcs(template.FuncMap{"zebra": func(i int) bool { return i%2 == 0 }})
t.Parse(templateFileName)
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
return err
}
r.body = buf.String()
return nil
}
// Run the email summary. This will get the latest stats, then execute the template and finally send
// the email.
func DoEmailSummary(env *Env) error {
stats, err := env.db.GetAllToonLatestQuickSummary()
if err != nil {
return err
}
// The email template laying out the HTML email.
const tpl = `
<table border="0" cellspacing="0" cellpadding="5">
<caption>WoW Stats</caption>
<thead>
<tr><th>Name</th><th>Level</th><th>Item Level</th><th>Last Modified</th><th>Last Recorded Date</th></tr>
</thead>
<tbody>
{{range $idx, $b := .}}
{{if zebra $idx}}<tr bgcolor="#C4C2C2">{{else}}<tr bgcolor="#DBDBDB">{{end}}
<td>{{$b.Toon.Name}}</td><td>{{$b.Level}}</td><td>{{$b.ItemLevel}}</td><td>{{$b.LastModifiedAsDateTime}}</td><td>{{$b.CreateDate.Format "2006-01-02"}}</td></tr>
{{end}}
</tbody></table><p>
`
r := NewEmailRequest(env.config.Email.ToAddress, env.config.Email.FromAddress, "WoW Stats", env.config.Email.Server, "")
err = r.ParseTemplate(tpl, stats)
if err != nil {
return err
}
err = r.SendEmail()
if err != nil {
return err
}
return nil
}