-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
196 lines (162 loc) · 4.37 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/zachlatta/postman/mail"
"gopkg.in/jordan-wright/email.v2"
)
type Recipient map[string]string
var (
htmlTemplatePath, textTemplatePath string
csvPath string
smtpURL, smtpUser, smtpPassword, smtpPort string
sender, subject string
attach string
files []string
debug bool
skipCertValidation bool
workerCount int
)
var flags, requiredFlags []*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.BoolVar(&debug, "debug", false, "print emails to stdout instead of sending")
flag.BoolVar(&skipCertValidation, "skipCertValidation", false, "disable tls certificate validation")
flag.StringVar(&attach, "attach", "", "attach a list of comma separated files")
flag.IntVar(&workerCount, "c", 8, "number of concurrent requests to have")
requiredFlagNames := []string{
"text",
"csv",
"server",
"port",
"sender",
"subject",
}
flag.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
for _, name := range requiredFlagNames {
if name == f.Name {
requiredFlags = append(requiredFlags, f)
}
}
})
flag.Usage = usage
flag.Parse()
if attach != "" {
files = strings.Split(attach, ",")
} else {
files = []string{}
}
checkAndHandleMissingFlags(requiredFlags)
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,
skipCertValidation,
)
jobs := make(chan Recipient, len(*recipients))
success := make(chan *email.Email)
fail := make(chan error)
// Start workers
for i := 0; i < workerCount; i++ {
go func() {
for recipient := range jobs {
sendMail(recipient, *emailField, &mailer, debug, success, fail)
}
}()
}
// Send jobs to workers
for _, recipient := range *recipients {
jobs <- recipient
}
close(jobs)
for i := 0; i < len(*recipients); i++ {
select {
case msg := <-success:
if !debug {
fmt.Printf("\rEmailed recipient %d of %d...", i+1, len(*recipients))
} else {
bytes, err := msg.Bytes()
if err != nil {
fmt.Printf("Error parsing email: %v", err)
}
fmt.Printf("%s\n\n\n", string(bytes))
}
case err := <-fail:
fmt.Fprintln(os.Stderr, "\nError sending email:", err.Error())
os.Exit(2)
}
}
fmt.Println()
}
func checkAndHandleMissingFlags(requiredFlags []*flag.Flag) {
var flagsMissing []*flag.Flag
for _, f := range requiredFlags {
if f.Value.String() == "" {
flagsMissing = append(flagsMissing, f)
}
}
missingCount := len(flagsMissing)
if missingCount > 0 {
if missingCount == len(requiredFlags) {
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)
}