-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.go
58 lines (49 loc) · 1.32 KB
/
send_email.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
package msgraph
import (
"fmt"
)
// type to get the response from the API SendEmail that just returns a 202 status code or an error
type Http202 struct {
Content string `json:"content"`
}
// SendEmail sends an email using Microsoft Graph API
func (c *Client) SendEmail(subject, body string, contentType ContentType, saveSentItems bool, toRecipients, ccRecipients, bccRecipients []string, attachs []Attachment) error {
if c.Token == nil {
return fmt.Errorf("missing access token. Please obtain one first")
}
if !c.Token.Valid() {
err := c.OAuthRefreshToken()
if err != nil {
return err
}
}
emailData := SendMailRequest{
Message: Message{
Subject: subject,
Body: Body{
ContentType: contentType.String(),
Content: body,
},
ToRecipients: SetRecipients(toRecipients),
CcRecipients: SetRecipients(ccRecipients),
BccRecipients: SetRecipients(bccRecipients),
Attachments: attachs,
},
SaveToSentItems: saveSentItems,
}
var response Http202
err := c.Post("/me/sendMail", emailData, nil, &response)
return err
}
func SetRecipients(recipients []string) []Recipient {
result := make([]Recipient, 0)
for _, recipient := range recipients {
if recipient != "" {
result = append(result, Recipient{
EmailAddress: EmailAddress{
Address: recipient,
}})
}
}
return result
}