Pavan S
Pavan S

Reputation: 11

Edit email using powershell and retain the HTML format

Hi I am trying to edit the HTML email template using powershell which has some pictures and colours. I need to edit and replace some content in email and retain the html body. When i try the below script it's converting to text format

$file= 'FILE PATH '
$outlook= New-Object -ComObject outlook.application
$msg= $outlook.createitemfromtemplate($file)
$msg.body= $msg.body -replace "December\d*", "TEST$a"
$msg.saveas($file)

Upvotes: 0

Views: 2155

Answers (2)

CHARLES BAUER
CHARLES BAUER

Reputation: 1

I was having the same issue than I used $mail.Save() after replacing de part I want on html body than everything worked. Without saving before sending I got an text message rather than html.

Upvotes: 0

Kristian Kanchev
Kristian Kanchev

Reputation: 229

You need to use HTMLBody and not body. Because you are telling it to use plane text in the email rather than HTML. Hope this helps.

$file= 'C:\Temp\template.msg'
$outlook= New-Object -ComObject outlook.application
$msg= $outlook.createitemfromtemplate($file)
$msg.HTMLbody = $msg.HTMLbody.Replace("color2", "color")
$msg.saveas($file)
#$file | ConvertTo-Html #if needed

Test scenario:

Before

Code was ran:

Code

After:

After

Upvotes: 1

Related Questions