forked from zachlatta/postman
/
main.go
146 lines (115 loc) · 2.99 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"os"
"text/template"
"github.com/zachlatta/postman/mail"
)
type Recipient map[string]string
var (
htmlTemplatePath, textTemplatePath string
csvPath string
smtpURL, smtpUser, smtpPassword, smtpPort string
sender, subject string
)
var flags []*flag.Flag
func main() {
flag.StringVar(&htmlTemplatePath, "html", "", "html template path")
flag.StringVar(&textTemplatePath, "text", "", "text template path")
flag.StringVar(&csvPath, "csv", "", "path to csv of contact list")
flag.StringVar(&smtpURL, "server", "", "url of smtp server")
flag.StringVar(&smtpPort, "port", "", "port of smtp server")
flag.StringVar(&smtpUser, "user", "", "smtp username")
flag.StringVar(&smtpPassword, "password", "", "smtp password")
flag.StringVar(&sender, "sender", "", "email to send from")
flag.StringVar(&subject, "subject", "", "subject of email")
flag.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
})
flag.Usage = usage
flag.Parse()
checkAndHandleMissingFlags(flags)
csv, err := os.Open(csvPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening CSV:", err.Error())
os.Exit(2)
}
defer csv.Close()
recipients, emailField, err := readCSV(csvPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading CSV:", err.Error())
os.Exit(2)
}
mailer := mail.NewMailer(
smtpUser,
smtpPassword,
smtpURL,
smtpPort,
)
success := make(chan Recipient)
fail := make(chan error)
go func() {
for _, recipient := range *recipients {
go sendMail(recipient, *emailField, &mailer, success, fail)
}
}()
for i := 0; i < len(*recipients); i++ {
select {
case <-success:
fmt.Printf("\rEmailed recipient %d of %d...", i+1, len(*recipients))
case err := <-fail:
fmt.Fprintln(os.Stderr, "\nError sending email:", err.Error())
os.Exit(2)
}
}
fmt.Println()
}
func checkAndHandleMissingFlags(flags []*flag.Flag) {
var flagsMissing []*flag.Flag
for _, f := range flags {
if f.Value.String() == "" {
flagsMissing = append(flagsMissing, f)
}
}
missingCount := len(flagsMissing)
if missingCount > 0 {
if missingCount == len(flags) {
usage()
}
missingFlags(flagsMissing)
}
}
const usageTemplate = `Postman is a utility for sending batch emails.
Usage:
postman [flags]
Flags:
{{range .}}
-{{.Name | printf "%-11s"}} {{.Usage}}{{end}}
`
const missingFlagsTemplate = `Missing required flags:
{{range .}}
-{{.Name | printf "%-11s"}} {{.Usage}}{{end}}
`
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
func printUsage(w io.Writer) {
tmpl(w, usageTemplate, flags)
}
func usage() {
printUsage(os.Stderr)
os.Exit(2)
}
func printMissingFlags(w io.Writer, missingFlags []*flag.Flag) {
tmpl(w, missingFlagsTemplate, missingFlags)
}
func missingFlags(missingFlags []*flag.Flag) {
printMissingFlags(os.Stderr, missingFlags)
os.Exit(2)
}