-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMgUserMail.ps1
More file actions
167 lines (128 loc) · 4.49 KB
/
MgUserMail.ps1
File metadata and controls
167 lines (128 loc) · 4.49 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<#
.SYNOPSIS
Sends email via Microsoft Graph using Send-MgUserMail with app-only authentication.
.DESCRIPTION
Sends email with the Microsoft Graph PowerShell SDK (Send-MgUserMail),
including HTML body content and file attachments. Uses certificate-based app-only
authentication.
.PARAMETER EmailRecipients
An array of recipient email addresses.
.PARAMETER EmailSender
The sender email address (must have Send.Mail permission in the app registration).
.PARAMETER ClientId
The Application (client) ID of the Entra ID app registration.
.PARAMETER TenantId
The Tenant ID for the Entra ID tenant.
.PARAMETER CertificateThumbprint
The certificate thumbprint for app-only authentication.
.PARAMETER AttachmentDirectory
Directory path containing files to attach. Defaults to 'C:\tmp'.
.PARAMETER HtmlBodyPath
Path to the HTML file used as the email body. Defaults to 'C:\tmp\HelloWorld.htm'.
.EXAMPLE
.\MgUserMail.ps1 -EmailRecipients 'user1@domain.com','user2@domain.biz' -EmailSender 'me@domain.info' -ClientId '<appId>' -TenantId '<tenantId>' -CertificateThumbprint '<thumbprint>'
Sends an email using the default attachment directory (C:\tmp) and HTML body path.
.EXAMPLE
$params = @{
EmailRecipients = 'user1@domain.com', 'user2@domain.biz'
EmailSender = 'me@domain.info'
ClientId = '656d524e-fe4a-407a-9579-7e2be1a74a3c'
TenantId = 'contoso.onmicrosoft.com'
CertificateThumbprint = 'A1B2C3D4E5F6...'
AttachmentDirectory = 'C:\Reports'
HtmlBodyPath = 'C:\Templates\Newsletter.htm'
}
.\MgUserMail.ps1 @params
Sends an email with custom attachment directory and HTML body using splatting.
.NOTES
Author: Mike Crowley
https://mikecrowley.us
Requires: Microsoft.Graph.Users.Actions module
.LINK
https://mikecrowley.us/2021/10/27/sending-email-with-send-mgusermail-microsoft-graph-powershell
.LINK
https://learn.microsoft.com/en-us/graph/api/user-sendmail
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$EmailRecipients,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$EmailSender,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$ClientId,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$TenantId,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$CertificateThumbprint,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$AttachmentDirectory = 'C:\tmp',
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$HtmlBodyPath = 'C:\tmp\HelloWorld.htm'
)
#region 1: Setup
$emailSubject = "Sample Email | " + (Get-Date -UFormat %e%b%Y)
$MgConnectParams = @{
ClientId = $ClientId
TenantId = $TenantId
CertificateThumbprint = $CertificateThumbprint
}
Function ConvertTo-IMicrosoftGraphRecipient {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$SmtpAddresses
)
foreach ($address in $SmtpAddresses) {
@{
emailAddress = @{address = $address }
}
}
}
Function ConvertTo-IMicrosoftGraphAttachment {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$UploadDirectory
)
$directoryContents = Get-ChildItem $UploadDirectory -Attributes !Directory -Recurse
foreach ($file in $directoryContents) {
$encodedAttachment = [convert]::ToBase64String((Get-Content $file.FullName -Encoding byte))
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
name = ($File.FullName -split '\\')[-1]
contentBytes = $encodedAttachment
}
}
}
#endregion 1
#region 2: Run
[array]$toRecipients = ConvertTo-IMicrosoftGraphRecipient -SmtpAddresses $EmailRecipients
$attachments = ConvertTo-IMicrosoftGraphAttachment -UploadDirectory $AttachmentDirectory
$emailBody = @{
ContentType = 'html'
Content = Get-Content $HtmlBodyPath
}
Connect-Graph @MgConnectParams
Select-MgProfile v1.0
$body = @{
subject = $emailSubject
toRecipients = $toRecipients
attachments = $attachments
body = $emailBody
}
$bodyParameter = @{
'message' = $body
'saveToSentItems' = $false
}
Send-MgUserMail -UserId $EmailSender -BodyParameter $bodyParameter
#endregion 2