-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Send-GraphMailMessage.ps1
135 lines (132 loc) · 5.3 KB
/
Send-GraphMailMessage.ps1
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
function Send-GraphMailMessage {
[cmdletBinding(SupportsShouldProcess)]
param(
[object] $From,
[Array] $To,
[Array] $Cc,
[Array] $Bcc,
[string] $ReplyTo,
[string] $Subject,
[alias('Body')][string[]] $HTML,
[string[]] $Text,
[alias('Attachments')][string[]] $Attachment,
[PSCredential] $Credential,
[alias('Importance')][ValidateSet('Low', 'Normal', 'High')][string] $Priority,
[switch] $DoNotSaveToSentItems
)
if ($Credential) {
$AuthorizationData = ConvertFrom-GraphCredential -Credential $Credential
} else {
return
}
$Authorization = Connect-O365Graph -ApplicationID $AuthorizationData.ClientID -ApplicationKey $AuthorizationData.ClientSecret -TenantDomain $AuthorizationData.DirectoryID -Resource https://graph.microsoft.com
$Body = @{}
if ($HTML) {
$Body['contentType'] = 'HTML'
$body['content'] = $HTML -join [System.Environment]::NewLine
} elseif ($Text) {
$Body['contentType'] = 'Text'
$body['content'] = $Text -join [System.Environment]::NewLine
} else {
$Body['contentType'] = 'Text'
$body['content'] = ''
}
$Message = [ordered] @{
# https://docs.microsoft.com/en-us/graph/api/resources/message?view=graph-rest-1.0
message = [ordered] @{
subject = $Subject
body = $Body
from = ConvertTo-GraphAddress -From $From
toRecipients = @(
ConvertTo-GraphAddress -MailboxAddress $To
)
ccRecipients = @(
ConvertTo-GraphAddress -MailboxAddress $CC
)
bccRecipients = @(
ConvertTo-GraphAddress -MailboxAddress $BCC
)
#sender = @(
# ConvertTo-GraphAddress -MailboxAddress $From
#)
replyTo = @(
ConvertTo-GraphAddress -MailboxAddress $ReplyTo
)
attachments = @(
foreach ($A in $Attachment) {
$ItemInformation = Get-Item -Path $A
if ($ItemInformation) {
$File = [system.io.file]::ReadAllBytes($A)
$Bytes = [System.Convert]::ToBase64String($File)
@{
'@odata.type' = '#microsoft.graph.fileAttachment'
'name' = $ItemInformation.Name
#'contentType' = 'text/plain'
'contentBytes' = $Bytes
}
}
}
)
importance = $Priority
#isReadReceiptRequested = $true
#isDeliveryReceiptRequested = $true
}
saveToSentItems = -not $DoNotSaveToSentItems.IsPresent
}
$MailSentTo = -join ($To -join ',', $CC -join ', ', $Bcc -join ', ')
Remove-EmptyValue -Hashtable $Message -Recursive -Rerun 2
$Body = $Message | ConvertTo-Json -Depth 5
$FromField = ConvertTo-GraphAddress -From $From -LimitedFrom
Try {
if ($PSCmdlet.ShouldProcess("$MailSentTo", 'Send-EmailMessage')) {
$null = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users/$FromField/sendMail" -Headers $Authorization -Method POST -Body $Body -ContentType 'application/json' -ErrorAction Stop
if (-not $Suppress) {
[PSCustomObject] @{
Status = $True
Error = ''
SentTo = $MailSentTo
SentFrom = $FromField
}
}
}
} catch {
if ($PSBoundParameters.ErrorAction -eq 'Stop') {
Write-Error $_
return
} else {
$ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
Write-Warning "Send-GraphMailMessage - Error: $($_.Exception.Message)"
Write-Warning "Send-GraphMailMessage - Error details: $($ErrorDetails.Error.Message)"
}
if (-not $Suppress) {
[PSCustomObject] @{
Status = $False
Error = -join ( $($_.Exception.Message), ' details: ', $($ErrorDetails.Error.Message))
SentTo = $MailSentTo
SentFrom = $FromField
}
}
}
if ($VerbosePreference) {
if ($Message.message.attachments) {
$Message.message.attachments | ForEach-Object {
if($_.contentBytes.Length -ge 10){
$_.contentBytes = -join ($_.contentBytes.Substring(0, 10), 'ContentIsTrimmed')
}
else {
$_.contentBytes = -join ($_.contentBytes, 'ContentIsTrimmed')
}
}
}
If ($Message.message.body.content) {
if($Message.message.body.content.Length -gt 10){
$Message.message.body.content = -join ($Message.message.body.content.Substring(0, 10), 'ContentIsTrimmed')
}
else {
$Message.message.body.content = -join ($Message.message.body.content, 'ContentIsTrimmed')
}
}
$TrimmedBody = $Message | ConvertTo-Json -Depth 5
Write-Verbose "Message content: $TrimmedBody"
}
}