Search code examples
windowspowershellmsg

How to convert HTML email to MSG file with PowerShell


I have emails as a ZIP file which contains the HTML version of it (1 html file and optional attachments). How can I convert these html emails to a .msg file using PowerShell?

It seems to be possible to at least work with Outlook:

$outlook = New-Object -ComObject Outlook.Application

And unzipping looks like an easy task according to How to unzip a file in Powershell?

On HTML Email to .MSG there is a similar question, but not really a practical answer so far.


Solution

  • Here is a practical (and very simple) answer using powershell to create a .msg with a HTML body.

    $outlook = New-Object -ComObject Outlook.Application
    $mailItem= $outlook.CreateItem([Microsoft.Office.Interop.Outlook.OlItemType]::olMailItem)
    $mailItem.Subject = "My Subject"
    $mailItem.HTMLBody = "<h1>My Title</h1><p>Hello World</p>"
    $mailItem.SaveAs("C:\temp\test.msg",[Microsoft.Office.Interop.Outlook.OlSaveAsType]::olMSG)
    

    Please note:

    1. you have a lot of properties in the $mailItem object to customize the object message (have a look at the variable)
    2. you need Outlook client installed on the machine