Using Internet Message Headers in New-MgUserMessage Cmdlet

When creating email messages using the New-MgUserMessage cmdlet, you might encounter the internetMessageHeaders parameter in usage examples. This parameter allows you to add custom headers to your email messages, which can be useful for various purposes such as tracking, categorizing, or processing emails in specific ways.


What are Internet Message Headers?

Internet message headers are metadata elements included in email messages, providing additional information about the message. These headers can include standard fields like From, To, Subject, and Date, as well as custom fields defined by the user or application. Custom headers can be used to add extra information that might be useful for email processing systems or for specific business needs.


Example of Using Internet Message Headers

Here is an example from the Microsoft documentation demonstrating how to include custom internet message headers in an email message created with the New-MgUserMessage cmdlet:

Import-Module Microsoft.Graph.Mail

$params = @{
    subject = "9/8/2018: concert"
    body = @{
        contentType = "HTML"
        content = "The group represents Washington."
    }
    toRecipients = @(
        @{
            emailAddress = @{
                address = "AlexW@contoso.OnMicrosoft.com"
            }
        }
    )
    internetMessageHeaders = @(
        @{
            name = "x-custom-header-group-name"
            value = "Washington"
        }
        @{
            name = "x-custom-header-group-id"
            value = "WA001"
        }
    )
}

# A UPN can also be used as -UserId.
New-MgUserMessage -UserId $userId -BodyParameter $params

In this example, two custom headers are added to the email message:

  • x-custom-header-group-name with the value Washington
  • x-custom-header-group-id with the value WA001

Adding Custom Headers to Your Messages

To include custom headers in your email messages, follow these steps:

  • Define the Headers: Create a hashtable with the name and value for each custom header.

  • Include the Headers in the Parameters: Add the internetMessageHeaders parameter to the hashtable passed to the -BodyParameter parameter of New-MgUserMessage


Example: Custom Email Message with Headers

Below is a complete example that demonstrates creating an email message with custom headers:

Import-Module Microsoft.Graph.Mail

# Define the custom headers
$customHeaders = @(
    @{
        name = "x-project-id"
        value = "12345"
    }
    @{
        name = "x-client-name"
        value = "Contoso"
    }
)

# Define the email parameters
$emailParams = @{
    subject = "Project Update"
    body = @{
        contentType = "HTML"
        content = "<p>Dear Team</p><p>Please find the latest updates on the project.</p><p>Best Regards<br/>Jane</p>"
    }
    toRecipients = @(
        @{
            emailAddress = @{
                address = "team@contoso.com"
            }
        }
    )
    internetMessageHeaders = $customHeaders
}

# Create the email message
New-MgUserMessage -UserId "jane.doe@example.com" -BodyParameter $emailParams

Benefits of Using Custom Headers

Custom headers can be beneficial for:

  • Tracking: Add identifiers for tracking purposes in internal systems.
  • Categorization: Categorize emails based on project, department, or other criteria.
  • Automation: Facilitate automated email processing by including metadata that systems can read and act upon.

Conclusion

The New-MgUserMessage cmdlet, when used with the internetMessageHeaders parameter, allows for the creation of richly informative email messages. By leveraging custom headers, administrators can add valuable metadata to emails, enhancing tracking, categorization, and automation processes. Understanding and utilizing internet message headers can significantly improve how emails are managed and processed within an organization.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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