-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendgmail.go
More file actions
249 lines (230 loc) · 6.05 KB
/
Copy pathsendgmail.go
File metadata and controls
249 lines (230 loc) · 6.05 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// sendgmail.go
// last updated : 2015-12-31
package main
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/smtp"
"os"
"strings"
)
const (
//version = ""
default_smtpauthuser = "" // Default(Your) Gmail account
default_smtpauthpass = "" // Default(Your) Gmail password
)
func setMailfrom(from string, smtpauthuser string) string {
if from != "" {
//fmt.Println("setting from from CLI value") // debug
return from
} else {
//fmt.Println("setting from from smtpauthuser") // debug
return smtpauthuser
}
}
func setMaildest(to string, cc string) []string {
if cc == "" {
maildest := []string{to}
return maildest
} else {
maildest := []string{to, cc}
return maildest
}
}
// ref : http://qiita.com/yamasaki-masahide/items/a9f8b43eeeaddbfb6b44
func add76crlf(msg string) string {
var buffer bytes.Buffer
for k, c := range strings.Split(msg, "") {
buffer.WriteString(c)
if k%76 == 75 {
buffer.WriteString("\r\n")
}
}
return buffer.String()
}
func utf8Split(utf8string string, length int) []string {
resultString := []string{}
var buffer bytes.Buffer
for k, c := range strings.Split(utf8string, "") {
buffer.WriteString(c)
if k%length == length-1 {
resultString = append(resultString, buffer.String())
buffer.Reset()
}
}
if buffer.Len() > 0 {
resultString = append(resultString, buffer.String())
}
return resultString
}
func encodeSubject(subject string) string {
if subject != "" {
var buffer bytes.Buffer
buffer.WriteString("Subject: ")
for _, line := range utf8Split(subject, 13) {
buffer.WriteString(" =?utf-8?B?")
buffer.WriteString(base64.StdEncoding.EncodeToString([]byte(line)))
buffer.WriteString("?=\r\n")
}
return buffer.String()
} else {
// \r\n is required to avoid the next line set as subject,
// when the password is empty
return "Subject: \r\n"
}
}
type Config struct {
Gmailuser string
Gmailpass string
}
func main() {
// Get options CLI
to := flag.String("t", "", "-t 'mail to'")
cc := flag.String("c", "", "-c 'mail cc'")
from := flag.String("f", "", "-f 'mail from'")
subject := flag.String("s", "", "-s 'subject'")
smtpauthuser := flag.String("g", "", "-g 'Gmail account email is sent from'")
smtpauthpass := flag.String("p", "", "-p 'Gmail password'")
//quiet := flag.Bool("q", false, "-q Quiet")
verbose := flag.Bool("v", false, "-v Verbose")
rawmode := flag.Bool("rm", false, "-rm Raw mode")
//version := flag.Bool("v", false, "-v Show version")
//debug := flag.Bool("d", false, "-d Debug mode")
flag.Parse()
// Default values
smtphost := "smtp.gmail.com"
smtpport := "587"
smtpserver := smtphost + ":" + smtpport
//maildest := []string{*to}
maildest := setMaildest(*to, *cc)
mailfrom := setMailfrom(*from, *smtpauthuser)
// Load conf
var conf Config
var file []byte
file, err := ioutil.ReadFile("sendgmail.conf")
if err != nil {
//fmt.Println("sendgmail.conf not found")
if *verbose == true {
fmt.Println("sendgmail.conf not found")
}
//panic(err)
}
if len(file) > 0 {
jsonerr := json.Unmarshal(file, &conf)
if jsonerr != nil {
fmt.Println("sendgmail.conf found but cannot be read")
/* if *quiet != true {
fmt.Println("sendgmail.conf found but cannot be read")
} */
panic(jsonerr)
}
}
// Check/set smtp auth-user/password
if *smtpauthuser != "" && *smtpauthpass != "" {
if *verbose == true {
fmt.Println("gmail account from cli used")
}
} else if conf.Gmailuser != "" {
*smtpauthuser = conf.Gmailuser
*smtpauthpass = conf.Gmailpass
if *verbose == true {
fmt.Println("Gmail account from conf used")
}
} else if *smtpauthuser == "" && default_smtpauthuser != "" && default_smtpauthpass != "" {
*smtpauthuser = default_smtpauthuser
*smtpauthpass = default_smtpauthpass
if *verbose == true {
fmt.Println("Hardcoded Gmail account/password used")
}
}
// Error checks
if *to == "" {
fmt.Println("Mail-to address is empty. Set it with -t option: -t 'account@example.com'")
fmt.Println("Exiting...")
os.Exit(0)
}
if *smtpauthuser == "" || *smtpauthpass == "" {
fmt.Println("Your Gmail user and/or password is empty..\nSet -g 'Gmail account' & -p 'smtp auth password'")
fmt.Println("Exiting...")
os.Exit(0)
}
if *subject == "" {
if *verbose == true {
fmt.Println("Mail subject is empty, but sending it, anyway.\nSet -s 'subject' next time! ")
}
}
// Set mail header
mailheader := ""
if *rawmode == false {
mailheader = "" +
"From: " + *smtpauthuser + "\r\n" +
"To: " + *to + "\r\n" +
"Cc: " + *cc + "\r\n" +
//"Subject: " + *subject + "\r\n" +
encodeSubject(*subject) +
"MIME-Version: 1.0\r\n" +
"Content-Type: text/plain; charset=\"utf-8\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n"
}
// Get mail body or set default message
mailbody := ""
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
mailbody = mailbody + scanner.Text() + "\r\n"
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
} else {
//fmt.Println("Scanned successfullyl!") // debug
}
// Set mail head + "encoded" mail body
mailmsg := ""
if *rawmode == false {
mailmsg = mailheader + add76crlf(base64.StdEncoding.EncodeToString([]byte(mailbody)))
} else {
mailmsg = mailbody
}
// Version, debug, etc.
/* if *debug == true {
fmt.Println("Debug mode")
fmt.Println("From : " + *to)
fmt.Println("CC : " + *cc)
fmt.Println("Subject : " + *subject)
fmt.Println("Auth user : " + *smtpauthuser)
fmt.Println("Auth pass : " + *smtpauthpass)
fmt.Println("Mail message : \n" + mailmsg)
os.Exit(0)
} else if *version == true {
fmt.Println("Version : " + version)
os.Exit(0)
} */
// Set up authentication information.
auth := smtp.PlainAuth(
"",
*smtpauthuser,
*smtpauthpass,
smtphost,
)
// Connect to the server, authenticate, set the sender and recipient,
smtperr := smtp.SendMail(
smtpserver,
auth,
mailfrom,
maildest,
[]byte(mailmsg),
)
if smtperr != nil {
log.Fatal(smtperr)
} else {
if *verbose == true {
fmt.Println("Mail sent successfully")
}
}
}