-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
65 lines (56 loc) · 1.31 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
package main
import (
"bytes"
"log"
"os"
"strings"
"text/template"
"github.com/mattbaird/gochimp"
)
type Mailer struct {
From string
To string
Key string
}
func sendMail(body string) {
mailer := &Mailer{
From: os.Getenv("FROM_ADDR"),
To: os.Getenv("TO_ADDR"),
Key: os.Getenv("MANDRILL_KEY"),
}
mandrillApi, err := gochimp.NewMandrill(mailer.Key)
if err != nil {
log.Fatalf("Error starting client %s", err)
}
// todo: improve this logic
recipients := make([]gochimp.Recipient, 0)
for _, email := range strings.Split(mailer.To, ",") {
recipients = append(recipients, gochimp.Recipient{Email: email})
}
message := gochimp.Message{
Html: body,
Subject: "Daily Annoying Mail v0.1",
FromEmail: "[email protected]",
FromName: "Annoying brah",
To: recipients,
}
_, err = mandrillApi.MessageSend(message, false)
if err != nil {
log.Fatalf("Error sending message %s", err)
}
}
func generateEmailBody(posts []Post) string {
var output bytes.Buffer
output.WriteString("<h1>Daily Annoying Diego Email</h1><br/>")
for _, post := range posts {
tmpl, err := template.New("test").Parse(`<a href="{{.URL}}">{{.Title}}</a><br/>`)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(&output, post)
if err != nil {
log.Fatal(err)
}
}
return output.String()
}