forked from mhewedy/ews
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_item.go
298 lines (255 loc) · 7.98 KB
/
create_item.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
package ews
import (
"encoding/base64"
"encoding/xml"
"errors"
"io"
"log"
"mime"
"os"
"path/filepath"
"time"
)
type InlineAttachment map[string]string
type CreateItem struct {
XMLName struct{} `xml:"m:CreateItem"`
MessageDisposition string `xml:"MessageDisposition,attr"`
SendMeetingInvitations string `xml:"SendMeetingInvitations,attr,omitempty"`
SavedItemFolderId *SavedItemFolderId `xml:"m:SavedItemFolderId,omitempty"`
Items Items `xml:"m:Items"`
}
type Items struct {
Message []Message `xml:"t:Message"`
CalendarItem []CalendarItem `xml:"t:CalendarItem"`
}
type SavedItemFolderId struct {
DistinguishedFolderId DistinguishedFolderId `xml:"t:DistinguishedFolderId"`
}
type Message struct {
ItemClass string `xml:"t:ItemClass,omitempty"`
ItemId *ItemId `xml:"t:ItemId,omitempty"`
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
Attachments *Attachments `xml:"t:Attachments,omitempty"`
Sender *OneMailbox `xml:"t:Sender,omitempty"`
ToRecipients *XMailbox `xml:"t:ToRecipients"`
CcRecipients *XMailbox `xml:"t:CcRecipients,omitempty"`
BccRecipients *XMailbox `xml:"t:BccRecipients,omitempty"`
}
type Attachments struct {
FileAttachment []FileAttachment `xml:"t:FileAttachment"`
}
type FileAttachment struct {
Content string `xml:"t:Content"`
Name string `xml:"t:Name"`
ContentType string `xml:"t:ContentType,omitempty"`
IsInline bool `xml:"t:IsInline"`
ContentId string `xml:"t:ContentId,omitempty"`
IsContactPhoto bool `xml:"t:IsContactPhoto"`
}
type CalendarItem struct {
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
ReminderIsSet bool `xml:"t:ReminderIsSet"`
ReminderMinutesBeforeStart int `xml:"t:ReminderMinutesBeforeStart"`
Start time.Time `xml:"t:Start"`
End time.Time `xml:"t:End"`
IsAllDayEvent bool `xml:"t:IsAllDayEvent"`
LegacyFreeBusyStatus string `xml:"t:LegacyFreeBusyStatus"`
Location string `xml:"t:Location"`
RequiredAttendees []Attendees `xml:"t:RequiredAttendees"`
OptionalAttendees []Attendees `xml:"t:OptionalAttendees"`
Resources []Attendees `xml:"t:Resources"`
}
type Body struct {
BodyType string `xml:"BodyType,attr"`
Body []byte `xml:",chardata"`
}
type OneMailbox struct {
Mailbox Mailbox `xml:"t:Mailbox"`
}
type XMailbox struct {
Mailbox []Mailbox `xml:"t:Mailbox"`
}
type Mailbox struct {
EmailAddress string `xml:"t:EmailAddress"`
}
type Attendee struct {
Mailbox Mailbox `xml:"t:Mailbox"`
}
type Attendees struct {
Attendee []Attendee `xml:"t:Attendee"`
}
type createItemResponseBodyEnvelop struct {
XMLName struct{} `xml:"Envelope"`
Body createItemResponseBody `xml:"Body"`
}
type createItemResponseBody struct {
CreateItemResponse CreateItemResponse `xml:"CreateItemResponse"`
}
type CreateItemResponse struct {
ResponseMessages ResponseMessages `xml:"ResponseMessages"`
}
type ResponseMessages struct {
CreateItemResponseMessage Response `xml:"CreateItemResponseMessage"`
}
// Create Attachments By Paths
func CreateAttachmentsByPaths(paths ...string) *Attachments {
var attachments Attachments
for _, path := range paths {
attachments.FileAttachment = append(attachments.FileAttachment, CreateFileAttachmentByPath(path))
}
return &attachments
}
func CreateInlineAttachments(attachmentInfos map[string]string) *Attachments {
var attachments Attachments
for name, path := range attachmentInfos {
attachments.FileAttachment = append(attachments.FileAttachment, createFileAttachment(name, path, true))
}
return &attachments
}
// Create FileAttachment By Name and Path and Inline
func createFileAttachment(name string, path string, inline bool) FileAttachment {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err2 := io.ReadAll(f)
if err2 != nil {
log.Fatal(err2)
}
if name == "" {
_, name = filepath.Split(path)
}
ext := filepath.Ext(path)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "application/octet-stream"
}
mimeType = mime.FormatMediaType(mimeType, map[string]string{"charset": "UTF-8"})
fileAttachment := FileAttachment{
Content: base64.StdEncoding.EncodeToString(b),
Name: name,
ContentType: mimeType,
IsInline: inline,
IsContactPhoto: false,
}
if inline {
fileAttachment.ContentId = name
}
return fileAttachment
}
func CreateFileAttachmentByPath(path string) FileAttachment {
return createFileAttachment("", path, false)
}
func createMessageItemWithAttachment(c Client, m ...Message) error {
// 1 - Save the message without attachments
item := &CreateItem{
MessageDisposition: "SaveOnly",
}
item.Items.Message = append(item.Items.Message, m...)
attachmentList := make([]FileAttachment, 0)
for i := range item.Items.Message {
attachmentList = append(attachmentList, item.Items.Message[i].Attachments.FileAttachment...)
item.Items.Message[i].Attachments = nil
item.Items.Message[i].ItemClass = ""
}
xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}
bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
resp, err := checkCreateItemResponse(bb)
if err != nil {
return err
}
// 2 - Save the attachments
if len(*resp.Items.Message) <= 0 {
return errors.New("do not have ParentId")
}
attachments := &Attachments{
FileAttachment: attachmentList,
}
messages := *resp.Items.Message
if len(messages) > 0 {
messages[0].ItemId.ChangeKey = ""
}
attachmentResp, err := SaveCreateAttachment(c, attachments, messages[0].ItemId)
if err != nil {
return err
}
// 3 - Send the mail
if len(*attachmentResp) > 0 {
messages[0].ItemId.ChangeKey = (*attachmentResp)[0].FileAttachmentId.RootItemChangeKey
}
err = SendSavedItem(c, &ItemIds{[]ItemId{*messages[0].ItemId}})
if err != nil {
return err
}
return nil
}
func createMessageItem(c Client, m ...Message) error {
item := &CreateItem{
MessageDisposition: "SendAndSaveCopy",
SavedItemFolderId: &SavedItemFolderId{DistinguishedFolderId{Id: "sentitems"}},
}
item.Items.Message = append(item.Items.Message, m...)
xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}
bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
if _, err := checkCreateItemResponse(bb); err != nil {
return err
}
return nil
}
// CreateMessageItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-email-message
func CreateMessageItem(c Client, m ...Message) error {
for i := range m {
if m[i].Attachments != nil && len(m[i].Attachments.FileAttachment) > 0 {
return createMessageItemWithAttachment(c, m...)
}
}
return createMessageItem(c, m...)
}
// CreateCalendarItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-calendar-item
func CreateCalendarItem(c Client, ci ...CalendarItem) error {
item := &CreateItem{
SendMeetingInvitations: "SendToAllAndSaveCopy",
SavedItemFolderId: &SavedItemFolderId{DistinguishedFolderId{Id: "calendar"}},
}
item.Items.CalendarItem = append(item.Items.CalendarItem, ci...)
xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}
bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
if _, err := checkCreateItemResponse(bb); err != nil {
return err
}
return nil
}
func checkCreateItemResponse(bb []byte) (*Response, error) {
var soapResp createItemResponseBodyEnvelop
if err := xml.Unmarshal(bb, &soapResp); err != nil {
return nil, err
}
resp := soapResp.Body.CreateItemResponse.ResponseMessages.CreateItemResponseMessage
if resp.ResponseClass == ResponseClassError {
return nil, errors.New(resp.MessageText)
}
return &resp, nil
}