-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmail.go
301 lines (241 loc) · 6.83 KB
/
sendmail.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
//
// Author: Henrique Dias
// Last Modification: 2024-03-05 18:57:22
//
// References:
// https://developers.google.com/gmail/api/quickstart/go
//
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/mail"
"os"
"path/filepath"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)
type EmailAddress struct {
Name string
Address string
}
type SendMail struct {
GmailHttpClient *http.Client
}
func chunkSplit(body string, limit int) string {
var charSlice []rune
// push characters to slice
for _, char := range body {
charSlice = append(charSlice, char)
}
var result = ""
for len(charSlice) >= 1 {
// convert slice/array back to string
// but insert end at specified limit
result = result + string(charSlice[:limit])
// discard the elements that were copied over to result
charSlice = charSlice[limit:]
// change the limit
// to cater for the last few words in
if len(charSlice) < limit {
limit = len(charSlice)
}
}
return result
}
func randStr(strSize int, randType string) string {
var dictionary string
if randType == "alphanum" {
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
var strBytes = make([]byte, strSize)
_, _ = rand.Read(strBytes)
for k, v := range strBytes {
strBytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(strBytes)
}
func encodeRFC2047(s string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{
Name: s,
Address: "",
}
return strings.Trim(addr.String(), " <@>")
}
func addHeaders(headers [][2]string) string {
var msg string
for _, header := range headers {
msg += fmt.Sprintf("%s: %s\r\n", header[0], header[1])
}
return msg
}
// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
token := &oauth2.Token{}
err = json.NewDecoder(f).Decode(token)
return token, err
}
// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) error {
log.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
return nil
}
// Request a token from the web, then returns the retrieved token.
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("%s:\n%v\n%s\n%s\n%s\n",
"Copy and paste the following link into your browser:",
authURL,
"Look for the code when you get the URL:",
"http://localhost/?state=state-token&code=<THIS-CODE>&scope=https://www.googleapis.com/auth/gmail.send",
"Type the authorization code in this terminal:")
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, fmt.Errorf("unable to read authorization code: %v", err)
}
token, err := config.Exchange(context.TODO(), authCode)
if err != nil {
return nil, fmt.Errorf("unable to retrieve token from web: %v", err)
}
return token, nil
}
func composeMessage(
fromField EmailAddress,
toField []EmailAddress,
subject, content string,
attachments []*multipart.FileHeader) (*gmail.Message, error) {
from := mail.Address{
Name: fromField.Name,
Address: fromField.Address,
}
toAdresses := []string{}
for _, toAddress := range toField {
to := mail.Address{
Name: toAddress.Name,
Address: toAddress.Address,
}
toAdresses = append(toAdresses, to.String())
}
boundary := randStr(32, "alphanum")
messageBody := addHeaders([][2]string{
{"From", from.String()},
{"To", strings.Join(toAdresses, ", ")},
{"Subject", encodeRFC2047(subject)},
{"MIME-Version", "1.0"},
{"Content-Type", fmt.Sprintf("multipart/mixed; boundary=\"%s\"", boundary)},
})
messageBody += fmt.Sprintf("\r\n--%s\r\n", boundary)
messageBody += addHeaders([][2]string{
{"MIME-Version", "1.0"},
{"Content-Transfer-Encoding", "7bit"},
{"Content-Type", "text/plain; charset=\"UTF-8\""},
})
messageBody += fmt.Sprintf("\r\n%s\r\n\r\n--%s", content, boundary)
for _, fh := range attachments {
fmt.Printf("Uploaded File: %+v\n", fh.Filename)
fmt.Printf("File Size: %+v\n", fh.Size)
fmt.Printf("MIME Header: %+v\n", fh.Header)
messageBody += "\r\n"
file, err := fh.Open()
if err != nil {
return nil, err
}
bytesReaded, err := io.ReadAll(file)
if err != nil {
return nil, err
}
fileMimeType := http.DetectContentType(bytesReaded)
fileData := base64.StdEncoding.EncodeToString(bytesReaded)
messageBody += addHeaders([][2]string{
{"MIME-Version", "1.0"},
{"Content-Type", fmt.Sprintf("%s; name=\"%s\"", fileMimeType, fh.Filename)},
{"Content-Transfer-Encoding", "base64"},
{"Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fh.Filename)},
})
messageBody += fmt.Sprintf("\r\n%s\r\n\r\n--%s", chunkSplit(fileData, 76), boundary)
file.Close()
}
messageBody += "--"
// fmt.Println(messageBody)
return &gmail.Message{
Raw: base64.RawURLEncoding.EncodeToString([]byte(messageBody)),
}, nil
}
func (sm SendMail) sendMail(
from EmailAddress,
to []EmailAddress,
subject, content string,
attachments []*multipart.FileHeader) error {
ctx := context.Background()
srv, err := gmail.NewService(
ctx,
option.WithHTTPClient(sm.GmailHttpClient),
option.WithScopes("https://www.googleapis.com/auth/gmail.send"),
)
if err != nil {
return fmt.Errorf("unable to retrieve gmail client: %v", err)
}
// Create message
gMessage, err := composeMessage(from, to, subject, content, attachments)
if err != nil {
panic(err)
}
if _, err := srv.Users.Messages.Send("me", gMessage).Do(); err != nil {
return fmt.Errorf("could not send mail: %v", err)
}
return nil
}
func initGMailClient(cacheDir string) (*http.Client, error) {
content, err := os.ReadFile("credentials.json")
if err != nil {
return nil, fmt.Errorf("unable to read credentials file: %v", err)
}
config, err := google.ConfigFromJSON(content, gmail.GmailSendScope)
if err != nil {
return nil, fmt.Errorf("unable to parse credentials file config: %v", err)
}
tokenFilePath := filepath.Join(cacheDir, "token.json")
token, err := tokenFromFile(tokenFilePath)
if err != nil {
token, err = getTokenFromWeb(config)
if err != nil {
return nil, err
}
if err := saveToken(tokenFilePath, token); err != nil {
return nil, err
}
}
// fmt.Println("Token:", token)
return config.Client(context.Background(), token), nil
}
func NewSendMail() SendMail {
sendmail := new(SendMail)
client, err := initGMailClient("cache")
if err != nil {
log.Fatalf("%v\r\n", err)
}
sendmail.GmailHttpClient = client
return *sendmail
}