Skip to content

Commit

Permalink
Create the ability to send email messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidArchibald committed Sep 1, 2021
1 parent ae385fc commit 84313f4
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package msgraph

import "time"

// Attachment represents content related to a event, message, or group post.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/Attachment
type Attachment struct {
ContentType string `json:"contentType,omitempty"`
ID string `json:"id,omitempty"`
IsInline bool `json:"isInline,omitempty"`
LastModifiedDateTime time.Time `json:"lastModifiedDateTime,omitempty"`
Name string `json:"name,omitempty"`
Size int32 `json:"size,omitempty"`
}
11 changes: 11 additions & 0 deletions DateTimeTimeZone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package msgraph

import "time"

// DateTimeTimeZone represents the date, time, and timezone for a given time.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/DateTimeTimeZone?view=graph-rest-1.0
type DateTimeTimeZone struct {
DateTime time.Time `json:"dateTime,omitempty"`
TimeZone *time.Location `json:"timeZone,omitempty"`
}
8 changes: 8 additions & 0 deletions Extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package msgraph

// Extension is an abstract type to represent an open type extension.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/Extension
type Extension struct {
ID string `json:"id,omitempty"`
}
11 changes: 11 additions & 0 deletions FollowupFlag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package msgraph

// FollowupFlag are flags on a message that display how it should be followed up on.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/FollowupFlag
type FollowupFlag struct {
CompletedDateTime DateTimeTimeZone `json:"completedDateTime,omitempty"`
DueDateTime DateTimeTimeZone `json:"dueDateTime,omitempty"`
FlagStatus string `json:"flagStatus,omitempty"`
StartDateTime DateTimeTimeZone `json:"startDateTime,omitempty"`
}
9 changes: 9 additions & 0 deletions ForwardMessageOptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// Structure to reply to messages.
//
// See https://docs.microsoft.com/en-us/graph/api/message-reply
type ForwardMessageOptions struct {
Comment string `json:"comment,omitempty"` // A comment to add.
ToRecipients []Recipient `json:"toRecipients,omitempty"` // Properties to be updated when repling.
}
9 changes: 9 additions & 0 deletions Importance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

type MessageImportance string

var (
MessageImportanceLow MessageImportance = "low"
MessageImportanceNormal MessageImportance = "normal"
MessageImportanceHigh MessageImportance = "high"
)
9 changes: 9 additions & 0 deletions InternetMessageHeader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// InternetMessageHeader is a key-value pair that represents an Internet message header that provides details of the network path taken from the message to the recipient. See RFC5322 for the definition.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/InternetMessageHeader
type InternetMessageHeader struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
9 changes: 9 additions & 0 deletions ItemBody.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// ItemBody represents the body of an item, either a message, event, or group post.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/ItemBody
type ItemBody struct {
Content string `json:"content,omitempty"`
ContentType string `json:"contentType,omitempty"`
}
73 changes: 73 additions & 0 deletions Message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package msgraph

import (
"time"
)

// Message represents a single email message.
//
// See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/Message
type Message struct {
BCCRecipients []Recipient `json:"bccRecipients,omitempty"`
Body *ItemBody `json:"body,omitempty"`
BodyPreview string `json:"bodyPreview,omitempty"`
Categories []string `json:"categories,omitempty"`
CCRecipients []Recipient `json:"ccRecipients,omitempty"`
ChangeKey string `json:"changeKey,omitempty"`
ConversationID string `json:"conversationId,omitempty"`
ConversationIndex string `json:"conversationIndex,omitempty"`
CreatedDateTime time.Time `json:"createdDateTime,omitempty"`
Flag *FollowupFlag `json:"flag,omitempty"`
From *Recipient `json:"from,omitempty"`
HasAttachments *bool `json:"hasAttachments,omitempty"`
ID string `json:"id,omitempty"`
Importance MessageImportance `json:"importance,omitempty"`
InferenceClassification MessageClassification `json:"inferenceClassification,omitempty"`
InternetMessageHeaders []InternetMessageHeader `json:"internetMessageHeaders,omitempty"`
InternetMessageID string `json:"internetMessageId,omitempty"`
IsDeliveryReceiptRequested *bool `json:"isDeliveryReceiptRequested,omitempty"`
IsDraft *bool `json:"isDraft,omitempty"`
IsRead *bool `json:"isRead,omitempty"`
IsReadReceiptRequested *bool `json:"isReadReceiptRequested,omitempty"`
LastModifiedDateTime time.Time `json:"lastModifiedDateTime,omitempty"`
ParentFolderId string `json:"parentFolderId,omitempty"`
ReceivedDateTime time.Time `json:"receivedDateTime,omitempty"`
ReplyTo []Recipient `json:"replyTo,omitempty"`
Sender *Recipient `json:"sender,omitempty"`
SentDateTime time.Time `json:"sentDateTime,omitempty"`
Subject string `json:"subject,omitempty"`
ToRecipients []Recipient `json:"toRecipients,omitempty"`
UniqueBody *ItemBody `json:"uniqueBody,omitempty"`
WebLink string `json:"webLink,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
Extensions []Extension `json:"extensions,omitempty"`
MultiValueExtendedProperties []MultiValueLegacyExtendedProperty `json:"multiValueExtendedProperties,omitempty"`
SingleValueExtendedProperties []SingleValueLegacyExtendedProperty `json:"singleValueExtendedProperties,omitempty"`

graphClient *GraphClient
}

// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (m *Message) setGraphClient(graphClient *GraphClient) {
m.graphClient = graphClient

for _, bccRecipient := range m.BCCRecipients {
bccRecipient.setGraphClient(graphClient)
}

for _, ccRecipient := range m.CCRecipients {
ccRecipient.setGraphClient(graphClient)
}

m.From.setGraphClient(graphClient)

for _, replyToRecipient := range m.ReplyTo {
replyToRecipient.setGraphClient(graphClient)
}

m.Sender.setGraphClient(graphClient)

for _, toRecipient := range m.ToRecipients {
toRecipient.setGraphClient(graphClient)
}
}
9 changes: 9 additions & 0 deletions MessageClassification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// MessageClassification is the importance of a user's message either inferred or overridden.
type MessageClassification string

var (
MessageClassificationFocused MessageClassification = "focused"
MessageClassificationOther MessageClassification = "other"
)
9 changes: 9 additions & 0 deletions MessageReply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// Structure to reply to messages.
//
// See https://docs.microsoft.com/en-us/graph/api/message-reply
type MessageReply struct {
Comment string `json:"comment,omitempty"` // A comment to add.
Message Message `json:"message,omitempty"` // Properties to be updated when repling.
}
9 changes: 9 additions & 0 deletions Messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

type Messages []Message

func (m Messages) setGraphClient(graphClient *GraphClient) {
for _, message := range m {
message.setGraphClient(graphClient)
}
}
7 changes: 7 additions & 0 deletions MultiValueLegacyExtendedProperty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package msgraph

// MultiValueLegacyExtendedProperty is an extended value that contains a slice of values.
type MultiValueLegacyExtendedProperty struct {
ID string `json:"id,omitempty"`
Value []string `json:"value,omitempty"`
}
8 changes: 8 additions & 0 deletions Recipient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package msgraph

// Represents a user that has sent or been sent an event, message, or group post.
//
// See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/Recipient
type Recipient struct {
EmailAddress `json:"emailAddress,omitempty"`
}
9 changes: 9 additions & 0 deletions SingleValueLegacyExtendedProperty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msgraph

// SingleValueLegacyExtendedProperty is an extended that contains a single value.
//
// See https://docs.microsoft.com/en-us/graph/api/resources/SingleValueLegacyExtendedProperty
type SingleValueLegacyExtendedProperty struct {
ID string `json:"id,omitempty"`
Value string `json:"value,omitempty"`
}
26 changes: 26 additions & 0 deletions User.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ func (u User) ListCalendarView(startDateTime, endDateTime time.Time, opts ...Lis
return calendarEvents, u.graphClient.makeGETAPICall(resource, reqOpt, &calendarEvents)
}

// SendMail sends a message.
//
// See https://docs.microsoft.com/en-us/graph/api/user-sendmail
func (u User) SendMail(message Message, saveToSentItems bool) error {
if u.graphClient == nil {
return ErrNotGraphClientSourced
}

bodyBytes, err := json.Marshal(struct {
Message Message `json:"message,omitempty"`
SaveToSentItems bool `json:"saveToSentItems,omitempty"`
}{
message,
saveToSentItems,
})
if err != nil {
return fmt.Errorf("could not marshal message body: %w", err)
}

reader := bytes.NewReader(bodyBytes)

resource := fmt.Sprintf("/users/%v/sendMail", u.ID)

return u.graphClient.makePOSTAPICall(resource, nil, reader, nil)
}

// getTimeZoneChoices grabs all supported time zones from microsoft for this user.
// This should actually be the same for every user. Only used internally by this
// msgraph package.
Expand Down

0 comments on commit 84313f4

Please sign in to comment.