Last active
May 28, 2022 02:20
-
-
Save IMJLA/82dd373ff9d29822d0cc91088fdc8597 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
First, use Outlook to create an email template. | |
Save it as a .msg file in the same folder as this script. | |
Include placeholder text to find and replace if you want to dynamically use code to update the email. | |
Then, this function will extract the Html and the attachments from the template .msg file | |
#> | |
param ( | |
# Placeholder text and replacement text to insert into the template. | |
# The hashtable keys are the placeholder text and the values are the replacement text | |
[hashtable]$TextToReplace = @{ | |
ReplacementText001 = "Today's cat fact is about cat tongues" | |
ReplacementText002 = "Fact: cats each have one tongue" | |
}, | |
# Parameters to pass to Send-MailMessage | |
$EmailParams = @{ | |
Subject = 'Cat Facts Email' | |
From = 'donotreply@contoso.com' | |
To = 'donotreply@contoso.com' | |
Bcc = 'Everyone@contoso.com' | |
SmtpServer = 'smtp.contoso.com' | |
} | |
) | |
$HtmlFile = Get-ChildItem -LiteralPath $PSScriptRoot -Filter *.htm | Select -First 1 | |
if ($HtmlFile) { | |
# An HTML template exists so we will load it | |
[string]$Body = Get-Content -LiteralPath $HtmlFile.FullName | |
} | |
else { | |
# An HTML template does not exist so we must extract the HTML and attachments from the .msg template | |
$MsgFile = Get-ChildItem -LiteralPath $PSScriptRoot -Filter *.msg | Select -First 1 | |
$OutlookApp = New-Object -comobject outlook.application | |
$MailItem = $OutlookApp.Session.OpenSharedItem($MsgFile.FullName) | |
$Html = $MailItem.htmlbody | |
$null = New-Item -Path "$PSScriptRoot\Attachments" -ItemType Directory | |
@($MailItem.Attachments()) | | |
ForEach-Object { | |
$PSItem.SaveAsFile("$PSScriptRoot\Attachments\$($PSItem.FileName)") | |
} | |
$OutlookApp.Quit() | |
<# Use RegEx to delete the bogus domains from the attachment paths so that this: | |
src="cid:image001.jpg@01D46A23.A9D42800" alt="cid:image001.jpg@01D3D55B.CC23E100" | |
Will look like this: | |
src="cid:image001.jpg" alt="cid:image001.jpg" | |
#> | |
$RegEx = '(?<Result>cid:[^@"\s]*)@[^\s"]*' | |
$AllMatches = $Html | | |
Select-String -Pattern $RegEx -AllMatches | |
$AllMatches.Matches | | |
ForEach-Object { | |
$Html = $Html -replace [regex]::Escape($_.Value),$_.Groups['Result'] | |
} | |
# Save the HTML so we can load it in the future without Outlook | |
$Html | out-file "$PSScriptRoot\EmailTemplate.htm" | |
[string]$Body = $Html | |
} | |
# Replace the placeholder text with the specified replacement text | |
ForEach ($OriginalText in $TextToReplace.Keys) { | |
$Body = $Body -replace $OriginalText,$TextToReplace[$OriginalText] | |
} | |
$Attachments = Get-ChildItem "$PSScriptRoot\Attachments" -ErrorAction SilentlyContinue | |
$EmailParams['BodyAsHtml'] = $true | |
$EmailParams['Body'] = $Body | |
$EmailParams['Attachments'] = $Attachments.FullName | |
Send-MailMessage @EmailParams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment