Automate Follow-Up Emails with Graph PowerShell

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.

The Script: Automatically Send Follow-Up Emails

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

How the Script Works

  • Connect to Microsoft Graph: The script begins by connecting to Microsoft Graph using the Mail.ReadWrite scope, allowing the script to read and send emails.
  • Define Criteria: The $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.
  • Retrieve Emails: The Get-MgUserMessage cmdlet retrieves emails that match the filter criteria (in this case, subject containing "Project Deadline").
  • Create Follow-Up Email: For each retrieved email, the script creates a follow-up message with a custom subject and body content, reminding the user about the important email.
  • Send the Email: The follow-up email is first created as a draft using the New-MgUserMessage cmdlet. The draft is then sent using the Send-MgUserMessage cmdlet.
  • Completion: The script logs each follow-up email that has been sent, confirming successful execution.

Further Enhancements

  • Add Multiple Recipients: Modify the script to include additional recipients in the follow-up email, such as managers or team leads.
  • ToRecipients = @(
        @{ EmailAddress = @{ Address = $UserId } }
        @{ EmailAddress = @{ Address = "manager@domain.com" } }
    )
  • Customize the Email Body: Include more dynamic information in the body content, such as the original email's subject or sender details, making the follow-up message more specific and relevant.
  • $bodyContent = "Dear User,

    Please review the following email regarding '$($email.Subject)' from $($email.Sender.EmailAddress.Address)."
  • Monitor Multiple Users: Implement a loop to monitor multiple users and send follow-up emails across a wider audience.
  • $users = @("user1@domain.com", "user2@domain.com")
    foreach ($user in $users) {
        # Logic for retrieving emails and sending follow-ups for each user
    }
  • Automate with Task Scheduler or Azure Automation: Set up a scheduled task or Azure Automation runbook to automatically trigger the script at regular intervals, ensuring that follow-up emails are sent continuously.

Possible Errors & Solutions

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:

Conclusion

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!

Suggested Reading

© m365corner.com. All Rights Reserved. Design by HTML Codex