Staying on top of important emails is crucial for effective communication and productivity. Sometimes critical emails may go unnoticed, leading to missed deadlines or tasks. For administrators, automating follow-up emails ensures that users receive reminders for emails that require their attention. Using Microsoft Graph PowerShell, administrators can streamline this process by sending automated follow-up messages based on specific criteria such as email subjects or content.
Here’s the PowerShell script that automates sending follow-up emails based on specific criteria:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.ReadWrite"
# Define the user and the criteria for the follow-up email
$UserId = "user@yourdomain.com"
$searchSubject = "Project Deadline"
# Retrieve emails that match the specified subject from the user's mailbox
$emailsToFollowUp = Get-MgUserMessage -UserId $UserId -Filter "contains(subject, '$searchSubject')" -Property Subject ReceivedDateTime
# Define the follow-up email details
$subject = "Follow-Up: Important Reminder Regarding Project Deadline"
$bodyContent = "Dear User,
We noticed an important email regarding a project deadline. Please review it as soon as possible.
Best Regards,
IT Team"
# Loop through each retrieved email and send a follow-up message
foreach ($email in $emailsToFollowUp) {
$followUpEmail = @{
Subject = $subject
Body = @{
ContentType = "HTML"
Content = $bodyContent
}
ToRecipients = @(@{ EmailAddress = @{ Address = $UserId } })
}
# Create the follow-up email as a draft
$draftEmail = New-MgUserMessage -UserId $UserId -BodyParameter $followUpEmail
# Send the draft message
Send-MgUserMessage -UserId $UserId -MessageId $draftEmail.Id
Write-Host "Follow-up email sent for: $($email.Subject)"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Mail.ReadWrite
scope, allowing the script to read and send emails.$UserId
is the email address of the user whose mailbox will be searched. In this case, emails containing the subject "Project Deadline" are targeted for follow-up.Get-MgUserMessage
cmdlet retrieves emails that match the filter criteria (in this case, subject containing "Project Deadline").New-MgUserMessage
cmdlet. The draft is then sent using the Send-MgUserMessage
cmdlet.ToRecipients = @(
@{ EmailAddress = @{ Address = $UserId } }
@{ EmailAddress = @{ Address = "manager@domain.com" } }
)
$bodyContent = "Dear User,
Please review the following email regarding '$($email.Subject)' from $($email.Sender.EmailAddress.Address)."
$users = @("user1@domain.com", "user2@domain.com")
foreach ($user in $users) {
# Logic for retrieving emails and sending follow-ups for each user
}
Error | Cause | Solution |
Missing an argument for parameter 'Sender' | The New-MgUserMessage cmdlet requires proper structuring to avoid missing arguments. |
Create the email as a draft first using New-MgUserMessage , and then send it using Send-MgUserMessage as shown in the script. |
Insufficient privileges | The connected account does not have the necessary permissions to send emails. | Ensure that the account has been granted the Mail.ReadWrite permission in Azure AD, and admin consent is provided if needed. |
No emails matching the criteria | The search criteria may not match any existing emails. | Adjust the search criteria to ensure that the correct emails are targeted, or verify that such emails exist in the mailbox. |
The term 'Get-MgUserMessage' is not recognized | The Microsoft Graph PowerShell module might not be installed or updated. | Install or update the Microsoft Graph PowerShell module by running: |
Automating follow-up emails using Microsoft Graph PowerShell provides an effective way to ensure important communications don’t go unnoticed. This script allows administrators to target specific emails and send follow-up reminders, improving response rates and keeping users informed.
With a few enhancements, this script can be extended to include more dynamic content, support multiple recipients, or even monitor multiple mailboxes. By automating follow-up emails, administrators can significantly reduce the risk of critical messages being overlooked, keeping teams on track with deadlines and tasks.
Start using this script today to automate email follow-ups and ensure important communications are always handled promptly!
© m365corner.com. All Rights Reserved. Design by HTML Codex