-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (192 loc) · 5.08 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"flag"
"fmt"
"io"
"net/mail"
"net/smtp"
"os"
"strings"
)
var (
fUseTLS = flag.Bool("l", true, "use STARTTLS")
fFrom = flag.String("f", "", "from address")
fTo = flag.String("t", "", "to address list (comma separated)")
fCC = flag.String("cc", "", "CC address list (comma separated)")
fBCC = flag.String("bcc", "", "BCC address list (comma separated)")
fServer = flag.String("s", "smtp.gmail.com:587", "SMTP server")
fMessage = flag.String("m", "", "message body (uses stdin if blank)")
fSubject = flag.String("u", "", "subject")
fAuth = flag.Bool("a", true, "use SMTP authentication")
fAuthUser = flag.String("xu", "", "username for SMTP authentication (env var "+GOMAIL_USER+" if blank)")
fAuthPassword = flag.String("xp", "", "password for SMTP authentication (env var "+GOMAIL_PASS+" if blank)")
fBufferSize = flag.Int("b", 5e6, "buffer size (will fill before connecting)")
)
const (
CRLF = "\r\n"
GOMAIL_USER = "GOMAIL_USER"
GOMAIL_PASS = "GOMAIL_PASS"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: gomail -f=<email> [options]\n")
flag.PrintDefaults()
os.Exit(1)
}
func fatal(f string, args ...interface{}) {
fmt.Fprintf(os.Stderr, f, args...)
os.Exit(1)
}
func main() {
flag.Parse()
mustNotCRLF(*fFrom)
mustNotCRLF(*fTo)
mustNotCRLF(*fSubject)
mustNotBlank(*fFrom)
mustNotBlank(*fServer)
buf := make([]byte, *fBufferSize)
var err error
var in io.Reader
if *fMessage == "" {
n := 0
for n < len(buf) && err == nil {
var nn int
nn, err = os.Stdin.Read(buf)
n += nn
}
rr := bytes.NewBuffer(buf[:n])
if err == nil {
in = io.MultiReader(rr, os.Stdin)
} else {
in = rr
}
} else {
in = bytes.NewBuffer([]byte(*fMessage))
}
fromList, err := parseAddressList(*fFrom)
if len(fromList) != 1 {
fmt.Fprintf(os.Stderr, "%s\n", "Only one from address allowed")
usage()
}
from := fromList[0]
tos, err := parseAddressList(*fTo)
if err != nil {
fatal(`%v: error parsing "to" list\n`, err)
}
ccs, err := parseAddressList(*fCC)
if err != nil {
fatal(`%v: error parsing "cc" list\n`, err)
}
bccs, err := parseAddressList(*fBCC)
if err != nil {
fatal(`%v: error parsing "bcc" list\n`, err)
}
user, pass := getCredentials(*fAuthUser, *fAuthPassword)
client, err := smtp.Dial(*fServer)
if err != nil {
fatal("%v: error connecting to %s\n", err, *fServer)
}
if ok, _ := client.Extension("STARTTLS"); ok {
err = client.StartTLS(nil)
if err != nil {
fatal("%v: error starting TLS\n", err)
}
}
host := (*fServer)[:strings.Index(*fServer, ":")]
if ok, _ := client.Extension("AUTH"); ok {
err = client.Auth(smtp.PlainAuth("", user, pass, host))
if err != nil {
fatal("%v: error authenticating '%s'\n", err, user)
}
} else if user != "" || pass != "" {
fmt.Fprintf(os.Stderr, "WARN: credentials supplied but server does not support authentication")
}
defer func() {
err := client.Quit()
if err != nil {
fmt.Fprintf(os.Stderr, "%v: error during quit", err)
}
}()
err = client.Mail(from.Address)
if err != nil {
fatal("%v: error specifying mail from\n", err)
}
recipientEmails := emailsOnly(tos, ccs, bccs)
for _, to := range recipientEmails {
err = client.Rcpt(to)
if err != nil {
fatal("%v: error specifying recipient\n", err)
}
}
out, err := client.Data()
if err != nil {
fatal("%v: error outputting data\n", err)
}
defer out.Close()
io.WriteString(out, "From: "+from.String()+CRLF)
for _, to := range tos {
io.WriteString(out, "To: "+to.String()+CRLF)
}
for _, cc := range ccs {
io.WriteString(out, "CC: "+cc.String()+CRLF)
}
if *fSubject != "" {
io.WriteString(out, "Subject: "+(*fSubject)+CRLF)
}
io.WriteString(out, CRLF)
_, err = io.Copy(out, in)
if err != nil {
fatal("%v: error outputting data\n", err)
}
}
// Returns as normal if the given string does not contain
// either a CR or LF character. Otherwise prints error message
// and exits.
func mustNotCRLF(s string) {
if strings.IndexAny(s, CRLF) > 0 {
fatal("From, To and Subject must not contain CR or LF")
}
}
// Returns as normal if s is not "". Otherwise prints the
// command usage and exits
func mustNotBlank(s string) {
if s == "" {
usage()
}
}
// Returns values for the username and password. If the passed
// values are "", will look for appropriate values in the environment
// vars "GOMAIL_USER" and "GOMAIL_PASS"
func getCredentials(u, p string) (user, pass string) {
if u == "" {
user = os.Getenv(GOMAIL_USER)
} else {
user = u
}
if p == "" {
pass = os.Getenv(GOMAIL_PASS)
} else {
pass = p
}
return
}
func parseAddressList(l string) ([]*mail.Address, error) {
// TODO: this is a hack, but is the best way to do it until
// this CL is accepted: http://codereview.appspot.com/5676067/
if l == "" {
return []*mail.Address{}, nil
}
key := "_"
htemp := make(mail.Header)
htemp[key] = []string{l}
return htemp.AddressList(key)
}
func emailsOnly(lists ...[]*mail.Address) (emails []string) {
emails = make([]string, 0)
for _, list := range lists {
for _, addr := range list {
emails = append(emails, addr.Address)
}
}
return
}