/
mailing.go
67 lines (55 loc) · 1.44 KB
/
mailing.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
package main
import (
"github.com/asdine/storm"
"github.com/ddliu/go-httpclient"
"log"
"os"
)
type (
MailTask struct {
Email string
Key string
}
)
var BasicAuthHeader string = "Basic " + os.Getenv("MUT_BASIC_AUTH")
var MailGunUrl string = os.Getenv("MUT_MAILGUN_URL")
func sendMail(email string, text string) {
response, responseError := httpclient.WithHeader("Authorization", BasicAuthHeader).Post(MailGunUrl,
map[string]string{
"from": "Mailgun Sandbox <postmaster@sandbox4ebeef9e81ca4130885ef51fa4b9729f.mailgun.org>",
"to": email,
"subject": "How is your mood today?",
"html": text,
})
if responseError != nil {
log.Printf("%s", responseError)
}
defer response.Body.Close()
}
func triggerMail(database *storm.DB) func() {
return func() {
log.Println("Triggered mail sending!")
subscriptions, triggerError := getAllSubscribers(database)
if triggerError != nil {
log.Printf("%s", triggerError)
}
mailTasks, triggerError := saveFeedbackIdentifierAndCreateMailTasks(subscriptions, database)
if triggerError != nil {
log.Printf("%s", triggerError)
}
sendMails(mailTasks)
}
}
func sendMails(tasks []MailTask) {
for _, task := range tasks {
sendMail(task.Email, getHtmlText(task.Key))
}
}
func getHtmlText(key string) string {
return `<html>
<body>
<h1>Select your mood</h1>
<a href="http://mut-musca.rhcloud.com/moods/` + key + `">Take me to the mood selection!</a>
</body>
</html>`
}