-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-GraphMailMessage.ps1
More file actions
64 lines (56 loc) · 2.05 KB
/
Send-GraphMailMessage.ps1
File metadata and controls
64 lines (56 loc) · 2.05 KB
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
<#
.SYNOPSIS
This is a function that uses MS Graph API to send a mail message.
This is an alternative to using Send-MgMailMessage via Microsoft Graph PowerShell SDK.
.PARAMETER from
This allows for setting a default from email address, but will also accept an alternative address as well
.PARAMETER subject
THis is a one liner for the subject of the email
.PARAMETER recipient
This is the person, or distribution list that will receive the email
.PARAMETER content
This is what will be in the email. this can be a single line, can be HTML in the form of a variable, or HTML formated text.
Recommend using HTML saved as a variable.
.EXAMPLE
Send-GraphMailMessage -from "me@domain.com" -recipient "You@domain.com" -subject "This is my Subject" -content "<h2>This is a Header 2 HTML line of text</h2>"
.NOTES
This requires that you have already retrieved a $token for MS Graph access, and that your App Registrion has the Mail.Send Application permission granted.
Author: Smitty
Date Updated: 2/15/2023
#>
function Send-GraphMailMessage {
[CmdletBinding()]
Param (
[string]$from = "myEmail@domain.com",
[Parameter(Mandatory)][string]$subject,
[Parameter(Mandatory)][string]$recipient,
[Parameter(Mandatory)]$content
)
begin {
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
$data = @{
message = @{
subject = $subject
body = @{
ContentType = "HTML"
Content = $content
}
toRecipients = @(
@{
EmailAddress = @{
Address = $recipient
}
}
)
}
}
$apiUrl = "https://graph.microsoft.com/v1.0/users/$from/sendMail"
}
process {
$json = $data | ConvertTo-Json -Depth 4
Invoke-RestMethod -Uri $apiURL -Headers $headers -Method POST -Body $json
}
}