Skip to content

Instantly share code, notes, and snippets.

@SMSAgentSoftware
Last active November 22, 2023 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SMSAgentSoftware/c74d582464e7764550bd251bf1c84813 to your computer and use it in GitHub Desktop.
Save SMSAgentSoftware/c74d582464e7764550bd251bf1c84813 to your computer and use it in GitHub Desktop.
Wrapper function to simplify generating a mail message for Send-MgUserMail with Microsoft Graph
#Requires -Modules Microsoft.Graph.Authentication, Microsoft.Graph.Users.Actions
function New-MgMailMessage {
param (
[Parameter(Mandatory=$true)]
[string]$Subject,
[Parameter(Mandatory=$true)]
[string]$Body,
[Parameter(Mandatory=$false)]
[ValidateSet("Text", "Html")]
[string]$BodyType = "Text",
[Parameter(Mandatory=$true)]
[string[]]$To,
[Parameter(Mandatory=$false)]
[string[]]$Cc,
[Parameter(Mandatory=$false)]
[string[]]$Bcc,
[Parameter(Mandatory=$false)]
[string[]]$Attachments,
[Parameter(Mandatory=$false)]
[ValidateSet("High", "Low")]
[string[]]$Importance,
[Parameter(Mandatory=$false)]
[string[]]$ReplyTo
)
$Message = @{}
$Message.Subject = $Subject
If ($Importance)
{
$Message.Importance = "$Importance"
}
$Message.Body = @{
ContentType = $BodyType
Content = $Body
}
$Message.ToRecipients = @()
foreach ($Address in $To)
{
$Message.ToRecipients += @{
EmailAddress = @{
Address = $Address
}
}
}
If ($Cc)
{
$Message.CcRecipients = @()
foreach ($Address in $Cc)
{
$Message.CcRecipients += @{
EmailAddress = @{
Address = $Address
}
}
}
}
If ($Bcc)
{
$Message.BccRecipients = @()
foreach ($Address in $Bcc)
{
$Message.BccRecipients += @{
EmailAddress = @{
Address = $Address
}
}
}
}
If ($Attachments)
{
$Message.Attachments = @()
foreach ($Attachment in $Attachments)
{
$attachementBytes = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($Attachment))
$attachmentName = Split-Path -Path $Attachment -Leaf -ErrorAction Stop
$Message.Attachments += @{
"@odata.type" = "#microsoft.graph.fileAttachment"
Name = $attachmentName
ContentBytes = $attachementBytes
}
}
}
If ($ReplyTo)
{
$Message.ReplyTo = @()
foreach ($Address in $ReplyTo)
{
$Message.ReplyTo += @{
EmailAddress = @{
Address = $Address
}
}
}
}
return $Message
}
##############
## Examples ##
##############
# Simple message
$Message = New-MgMailMessage -Subject "Hi there" -Body "How ya doin?" -To "bill.gates@contoso.com"
# Message with Bcc, attachments, importance and reply to
$Message = New-MgMailMessage -Subject "Hi there" -Body "How ya doin?" -To "bill.gates@contoso.com" -Bcc "satya.nadella@contoso.com" -Attachments "C:\Users\me\Downloads\0023970129-1.PDF","C:\Temp\MyCSVfile.csv" -Importance High -ReplyTo "noreply@contoso.com"
# Html message
$html = Get-Process | Select -Property Name,Id,Path | ConvertTo-Html -PreContent "<h2>Running processes on my system</h2>" | Out-String
$Message = New-MgMailMessage -Subject "Running processes" -Body $html -BodyType Html -To "bill.gates@contoso.com"
# Send the message
Connect-MgGraph -Scopes Mail.Send
$Result = Send-MgUserMail -UserId 'sender@contoso.com' -Message $Message -SaveToSentItems -PassThru
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment