Need to send emails programmatically from a Microsoft 365 account using PowerShell? Whether it's for automation, alerts, or sending reports with attachments, the Send-MgUserMail cmdlet from Microsoft Graph PowerShell is your go-to solution.
This guide shows you how to use Send-MgUserMail with real-world examples to send emails—including CCs, attachments, and even custom headers.
Send-MgUserMail is a Microsoft Graph PowerShell cmdlet that allows you to send emails from a specified Microsoft 365 user account. It uses the Microsoft Graph API and supports text/HTML content, attachments, CC/BCC recipients, and internet message headers.
Here are just a few reasons to consider this cmdlet:
Send-MgUserMail -UserId <String> -BodyParameter <IMicrosoftGraphMessage>
$params = @{
Message = @{
Subject = "Project Update"
Body = @{
ContentType = "Text"
Content = "Please find the latest update on the project."
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "recipient@example.com"
}
}
)
CcRecipients = @(
@{
EmailAddress = @{
Address = "ccrecipient@example.com"
}
}
)
}
SaveToSentItems = $true
}
Send-MgUserMail -UserId "user@example.com" -BodyParameter $params
$params = @{
Message = @{
Subject = "Project Documents"
Body = @{
ContentType = "Text"
Content = "Please find the attached documents related to the project."
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "recipient@example.com"
}
}
)
Attachments = @(
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
Name = "ProjectPlan.docx"
ContentBytes = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\Path\To\ProjectPlan.docx"))
}
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
Name = "Budget.xlsx"
ContentBytes = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\Path\To\Budget.xlsx"))
}
)
}
SaveToSentItems = $true
}
Send-MgUserMail -UserId "user@example.com" -BodyParameter $params
$params = @{
Message = @{
Subject = "Special Announcement"
Body = @{
ContentType = "Text"
Content = "Here is an important announcement."
}
ToRecipients = @(
@{
EmailAddress = @{
Address = "recipient@example.com"
}
}
)
InternetMessageHeaders = @(
@{
Name = "X-Custom-Header-1"
Value = "Custom Value 1"
}
@{
Name = "X-Custom-Header-2"
Value = "Custom Value 2"
}
)
}
SaveToSentItems = $true
}
Send-MgUserMail -UserId "user@example.com" -BodyParameter $params
The Send-MgUserMail cmdlet is a powerful tool for administrators and developers who want to automate Microsoft 365 communications. It supports everything from simple alerts to rich, attachment-filled messages and is perfect for integrating into scheduled scripts and workflows.
Whether you're sending a daily report or a compliance alert—Send-MgUserMail gets the message across!
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex