This repository was archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (66 loc) · 1.41 KB
/
main.go
File metadata and controls
85 lines (66 loc) · 1.41 KB
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/smtp"
"os"
"text/template"
)
type Person struct {
Name string
Email string
}
type Credential struct {
Email string
Password string
}
var personsFile string
var templateFile string
var persons []Person
var credential Credential
const subject = "Initiating project newsletter"
func initPersonsAndCreds() {
jsonBytes, _ := ioutil.ReadFile(personsFile)
json.Unmarshal(jsonBytes, &persons)
jsonBytes, _ = ioutil.ReadFile("./secrets/credentials.json")
err := json.Unmarshal(jsonBytes, &credential)
if err != nil {
log.Panic(err)
}
}
func send(email string, body string) {
from := credential.Email
pass := credential.Password
to := email
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: " + subject + "\n\n" +
body
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))
if err != nil {
log.Panic("smtp error: %s", err)
return
}
}
func main() {
personsFile = os.Args[1]
templateFile = os.Args[2]
initPersonsAndCreds()
txtFile, _ := ioutil.ReadFile(templateFile)
tmpl, err := template.New("test").Parse(string(txtFile))
if err != nil {
log.Panic(err)
}
for _, person := range persons {
var body bytes.Buffer
err = tmpl.Execute(&body, person)
send(person.Email, body.String())
}
if err != nil {
log.Panic(err)
}
}