-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.go
142 lines (126 loc) · 3.86 KB
/
mail.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
package mail
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)
func GetClient(config *oauth2.Config) (*http.Client, error) {
tok, err := tokenFromFile()
if err != nil {
tok, err = getTokenFromWeb(config)
if err != nil {
return nil, err
}
if err := saveToken(tok); err != nil {
return nil, err
}
}
return config.Client(context.Background(), tok), nil
}
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\ninput > ", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, errors.New(fmt.Sprintf("\nUnable to read authorization code: %v", err))
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
return nil, errors.New(fmt.Sprintf("\nUnable to retrieve token from web: %v", err))
}
return tok, nil
}
func saveToken(token *oauth2.Token) error {
confhome, _ := os.UserHomeDir()
path := filepath.Join(confhome, ".aip/gtoken.json")
fmt.Printf("\n\nSaving 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 errors.New(fmt.Sprintf("Unable to cache oauth token: %v", err))
}
defer f.Close()
json.NewEncoder(f).Encode(token)
return nil
}
func tokenFromFile() (*oauth2.Token, error) {
homedir, _ := os.UserHomeDir()
confdir := filepath.Join(homedir, ".aip")
// check if the dir/file exists
if _, err := os.Stat(confdir); os.IsNotExist(err) {
return nil, errors.New(fmt.Sprintf("Failed to open config directory at %v.\n%v", confdir, err.Error()))
}
conffile := filepath.Join(confdir, "gtoken.json")
if stat, err := os.Stat(conffile); os.IsNotExist(err) {
return nil, errors.New(fmt.Sprintf("Failed to open config file at %v.\n%v\n", conffile, err.Error()))
} else {
if stat.Mode() != 0600 {
return nil, errors.New(fmt.Sprintf("Invalid permission for config file(%v): %v", conffile, stat.Mode()))
}
}
f, err := os.Open(conffile)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
func SendMail(toaddr string, subject string, body string) error {
ctx := context.Background()
homedir, _ := os.UserHomeDir()
confdir := filepath.Join(homedir, ".aip")
// check if the dir/file exists
if _, err := os.Stat(confdir); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("Failed to open config directory at %v.\n%v", confdir, err.Error()))
}
conffile := filepath.Join(confdir, "gcred.json")
if _, err := os.Stat(conffile); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("Failed to open config file at %v.\n%v\n", conffile, err.Error()))
}
b, err := ioutil.ReadFile(conffile)
if err != nil {
return err
}
config, err := google.ConfigFromJSON(b, gmail.GmailSendScope)
if err != nil {
return err
}
client, err := GetClient(config)
if err != nil {
return err
}
srv, err := gmail.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return err
}
var message gmail.Message
msg := bytes.NewBuffer([]byte(""))
msg.WriteString("From: 'me'\r\n")
msg.WriteString(fmt.Sprintf("To: %v\r\n", toaddr))
msg.WriteString(fmt.Sprintf("Subject: AIP: %v\r\n", subject))
msg.WriteString("\r\n")
msg.WriteString(body)
message.Raw = base64.StdEncoding.EncodeToString(msg.Bytes())
message.Raw = strings.Replace(message.Raw, "/", "_", -1)
message.Raw = strings.Replace(message.Raw, "+", "-", -1)
message.Raw = strings.Replace(message.Raw, "=", "", -1)
_, err = srv.Users.Messages.Send("me", &message).Do()
if err != nil {
return err
}
return nil
}