-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
221 lines (177 loc) · 5.52 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
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
package outlook
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"log"
"net/http"
"github.com/news-ai/tabulae/models"
"golang.org/x/net/context"
)
type EmailAddress struct {
EmailAddress struct {
Address string `json:"Address"`
} `json:"EmailAddress"`
}
type Attachment struct {
OdataType string `json:"@odata.type"`
Name string `json:"Name"`
ContentBytes string `json:"ContentBytes"`
}
type EmailRequestError struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
type EmailRequest struct {
Message struct {
Subject string `json:"Subject"`
Body struct {
ContentType string `json:"ContentType"`
Content string `json:"Content"`
} `json:"Body"`
ToRecipients []EmailAddress `json:"ToRecipients"`
} `json:"Message"`
}
type EmailRequestAttachment struct {
Message struct {
Subject string `json:"Subject"`
Body struct {
ContentType string `json:"ContentType"`
Content string `json:"Content"`
} `json:"Body"`
ToRecipients []EmailAddress `json:"ToRecipients"`
Attachments []Attachment `json:"Attachments"`
} `json:"Message"`
}
type GetEmailRequest struct {
OdataContext string `json:"@odata.context"`
Value []struct {
OdataID string `json:"@odata.id"`
OdataEtag string `json:"@odata.etag"`
ID string `json:"Id"`
Subject string `json:"Subject"`
Sender struct {
EmailAddress struct {
Name string `json:"Name"`
Address string `json:"Address"`
} `json:"EmailAddress"`
} `json:"Sender"`
OdataType string `json:"@odata.type,omitempty"`
} `json:"value"`
}
func (o *Outlook) SendEmail(c context.Context, from string, to string, subject string, body string, email models.Email) error {
if len(o.AccessToken) > 0 {
toEmail := EmailAddress{}
toEmail.EmailAddress.Address = to
var message EmailRequest
message.Message.Subject = subject
message.Message.ToRecipients = append(message.Message.ToRecipients, toEmail)
message.Message.Body.ContentType = "HTML"
message.Message.Body.Content = body
messageJson, err := json.Marshal(message)
if err != nil {
log.Printf("%v", err)
return err
}
messageQuery := bytes.NewReader(messageJson)
URL := BASEURL + "api/v2.0/me/sendmail"
req, _ := http.NewRequest("POST", URL, messageQuery)
req.Header.Add("Authorization", "Bearer "+o.AccessToken)
req.Header.Add("Content-Type", "application/json")
client := http.Client{}
response, err := client.Do(req)
if err != nil {
log.Printf("%v", err)
return err
}
defer response.Body.Close()
if response.StatusCode == 200 || response.StatusCode == 202 {
log.Printf("%v", response.Body)
return nil
}
// Decode JSON from Google
decoder := json.NewDecoder(response.Body)
var emailRequestError EmailRequestError
err = decoder.Decode(&emailRequestError)
if err != nil {
log.Printf("%v", err)
return err
}
log.Printf("%v", emailRequestError.Error)
return errors.New("Email could not be sent")
}
return errors.New("No access token supplied")
}
func (o *Outlook) SendEmailWithAttachments(c context.Context, from string, to string, subject string, body string, email models.Email, files []models.File, bytesArray [][]byte, attachmentType []string, fileNames []string) error {
if len(o.AccessToken) > 0 {
toEmail := EmailAddress{}
toEmail.EmailAddress.Address = to
attachments := []Attachment{}
if len(files) > 0 {
for x := 0; x < len(bytesArray); x++ {
str := base64.StdEncoding.EncodeToString(bytesArray[x])
singleAttachment := Attachment{}
singleAttachment.Name = fileNames[x]
singleAttachment.OdataType = "#Microsoft.OutlookServices.FileAttachment"
singleAttachment.ContentBytes = str
attachments = append(attachments, singleAttachment)
}
}
var message EmailRequestAttachment
message.Message.Subject = subject
message.Message.ToRecipients = append(message.Message.ToRecipients, toEmail)
message.Message.Body.ContentType = "HTML"
message.Message.Body.Content = body
message.Message.Attachments = attachments
messageJson, err := json.Marshal(message)
if err != nil {
log.Printf("%v", err)
return err
}
messageQuery := bytes.NewReader(messageJson)
URL := BASEURL + "api/v2.0/me/sendmail"
req, _ := http.NewRequest("POST", URL, messageQuery)
req.Header.Add("Authorization", "Bearer "+o.AccessToken)
req.Header.Add("Content-Type", "application/json")
client := http.Client{}
response, err := client.Do(req)
if err != nil {
log.Printf("%v", err)
return err
}
defer response.Body.Close()
if response.StatusCode == 200 || response.StatusCode == 202 {
return nil
}
// Decode JSON from Google
decoder := json.NewDecoder(response.Body)
var emailRequestError EmailRequestError
err = decoder.Decode(&emailRequestError)
if err != nil {
log.Printf("%v", err)
return err
}
log.Printf("%v", emailRequestError.Error)
return errors.New("Email could not be sent")
}
return errors.New("No access token supplied")
}
func (o *Outlook) GetEmail(c context.Context, to string, subject string) error {
if len(o.AccessToken) > 0 {
URL := BASEURL + "api/v2.0/me/MailFolders/sentitems/messages/?$select=Sender,Subject&$search=\"subject:" + subject + "\""
req, _ := http.NewRequest("GET", URL, nil)
req.Header.Add("Authorization", "Bearer "+o.AccessToken)
req.Header.Add("Content-Type", "application/json")
client := http.Client{}
response, err := client.Do(req)
if err != nil {
log.Printf("%v", err)
return err
}
defer response.Body.Close()
}
return nil
}