-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMailWithGraph.ps1
More file actions
124 lines (113 loc) · 4.06 KB
/
SendMailWithGraph.ps1
File metadata and controls
124 lines (113 loc) · 4.06 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
#requires -module Microsoft.Graph.Authentication, Microsoft.Graph.Users.Actions
function Send-MailMessageWithGraph {
<#
.SYNOPSIS
Use the Graph to send an email from your account!
.DESCRIPTION
It acts like send-mailmessage, but behind the scenes, it is using the PowerShell MgGraph module
Simply add to/cc, subject, body as text/html, etc. and your email will send
Uses your current authentication to send
.NOTES
Author: Roger P Seekell
Date: 6-28-21
.PARAMETER Subject
The subject line of your email, defaults to "Graph Test Message"
.PARAMETER BodyAsHtml
If you want to use HTML formatting in your message, use this to write the body of your email.
.PARAMETER BodyAsText
If you want a text-only message, use this to write the body of your email.
.PARAMETER To
A comma-separated list of email addresses - can use either TO or CC or both
.PARAMETER Cc
A comma-separated list of email addresses - can use either TO or CC or both
.PARAMETER attachmentPath
The file path to the attachment you want to include on the email
.EXAMPLE
Send-MailMessageWithGraph -Subject "First Test Message" -BodyAsText "Hello there" -To "roger.seekell@something.com"
.EXAMPLE
Send-MailMessageWithGraph -Subject "Second Test Message" -BodyAsHTML "This is a <i>formatted</i> email <em>test</em> by <br /><b>Yours Truly</b>" `
-To "roger.seekell@something.com" -Importance Low
.EXAMPLE
Send-MailMessageWithGraph -Subject "Important Test Message" -BodyAsText "Hello there" -To "roger.seekell@something.com" -Cc "roger.seekell@something.com" -Importance High
.EXAMPLE
Send-MailMessageWithGraph -Subject "Test attachment" -BodyAsText "Behold, the attachment!" -To "roger.Seekell@something.com" -attachmentPath 'C:\users\rps\Downloads\ApplicationSignIns_2020-11-09_2020-11-16.csv'
#>
[CmdletBinding()]
param (
[Parameter()] [string] $Subject = "Graph Test Message",
[Parameter()] [string] $BodyAsText,
[Parameter()] [string] $BodyAsHTML,
[Parameter()] [string[]] $To,
[Parameter()] [string[]] $Cc,
[Parameter()] [string] $attachmentPath = "",
[Parameter()] [ValidateSet("High","Normal","Low")] [string] $Importance
)
#connect to MS Graph first
Connect-MgGraph -Scopes "mail.send"
$currentUser = (Get-MgContext).Account
#not sure how to send as someone else
#build out recipients
$ToRecipientTable = @()
foreach ($rep in $To) {
$ToRecipientTable += @{
emailAddress = @{
name = "User"; #this name doesn't apparently matter
address = $rep
}
}
}#end foreach
#build out recipients
$CcRecipientTable = @()
foreach ($rep in $Cc) {
$CcRecipientTable += @{
emailAddress = @{
name = "User"; #this name doesn't apparently matter
address = $rep
}
}
}#end foreach
#build out the body of message
if ($BodyAsText -ne "") {
$BodyTable = @{
contentType = "Text";
content = $BodyAsText
}
}
elseif ($BodyAsHtml -ne "") {
$BodyTable = @{
contentType = "HTML";
content = $BodyAsHtml
}
}
#build out the message
$Message = @{
subject = $Subject;
toRecipients = $ToRecipientTable;
ccRecipients = $CcRecipientTable;
body = $BodyTable;
}
if ($Importance -eq "High" -or $Importance -eq "Low") {
$Message += @{Importance = $Importance}
}
#handle attachments (whoa!)
if ($attachmentPath -ne "") {
$attachmentCollection = @()
#Get File Name and Base64 string
$FileName=(Get-Item -Path $attachmentPath).name
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($attachmentPath))
<#
[byte[]]$contentBytes = [System.IO.File]::ReadAllBytes($attachmentPath)
[string]$contentType = "text"
#>
$attachmentCollection += @{
"@odata.type" = "#microsoft.graph.fileAttachment";
contentBytes = $base64string;
contentType = "text/plain";
name = "$FileName"
}
$Message += @{attachments = $attachmentCollection}
#>
}#end attachment section
#this is the goal...
Send-MgUserMail -userid $currentUser -BodyParameter @{message = $Message}
}#end function