-
Notifications
You must be signed in to change notification settings - Fork 47
/
createitem.go
71 lines (59 loc) · 1.69 KB
/
createitem.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
// 26 august 2016
package ews
import (
"encoding/xml"
)
// https://msdn.microsoft.com/en-us/library/office/aa563009(v=exchg.140).aspx
type CreateItem struct {
XMLName struct{} `xml:"m:CreateItem"`
MessageDisposition string `xml:"MessageDisposition,attr"`
SavedItemFolderId SavedItemFolderId `xml:"m:SavedItemFolderId"`
Items Messages `xml:"m:Items"`
}
type Messages struct {
Message []Message `xml:"t:Message"`
}
type SavedItemFolderId struct {
DistinguishedFolderId DistinguishedFolderId `xml:"t:DistinguishedFolderId"`
}
type DistinguishedFolderId struct {
Id string `xml:"Id,attr"`
}
type Message struct {
ItemClass string `xml:"t:ItemClass"`
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
Sender OneMailbox `xml:"t:Sender"`
ToRecipients XMailbox `xml:"t:ToRecipients"`
}
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"`
}
func BuildTextEmail(from string, to []string, subject string, body []byte) ([]byte, error) {
c := new(CreateItem)
c.MessageDisposition = "SendAndSaveCopy"
c.SavedItemFolderId.DistinguishedFolderId.Id = "sentitems"
m := new(Message)
m.ItemClass = "IPM.Note"
m.Subject = subject
m.Body.BodyType = "Text"
m.Body.Body = body
m.Sender.Mailbox.EmailAddress = from
mb := make([]Mailbox, len(to))
for i, addr := range to {
mb[i].EmailAddress = addr
}
m.ToRecipients.Mailbox = append(m.ToRecipients.Mailbox, mb...)
c.Items.Message = append(c.Items.Message, *m)
return xml.MarshalIndent(c, "", " ")
}