3

I'm restricted in a project, and have to use PowerShell to generate and send emails from Outlook.

param([string]$address, [string]$subject);
$Outlook = New-Object -ComObject Outlook.Application;
$Mail = $Outlook.CreateItemFromTemplate("D:\Users\mark\test.oft");
$Mail.To = "$address";
$Mail.Subject = "$subject";
$Mail.Send();

I'm using an oft template file, as I have to include a company logo embedded image.

However my problem is I need to change some of the body of the email, but retain the embedded graphics and formatting.

Is there a way in PowerShell of passing in a (for example) Reference ID parameter - and replacing in the body of the OFT file, #RefID# with "Reference ID" eg:

param([string]$address, [string]$subject, [string]$RefID);
$Outlook = New-Object -ComObject Outlook.Application;
$Mail = $Outlook.CreateItemFromTemplate("D:\Users\mark\test.oft");
****
$Mail.Body = $Mail.Body.Replace("#RefID#", $RefID)
****
$Mail.To = "$address";
$Mail.Subject = "$subject";
$Mail.Send();

the .Replace above seems to remove formatting, and replaces the logo, with "CID:...."

What it should look like:

What it looks like after using Replace:

How it looks after using Replace

Thanks for any help,

Mark

1
  • Did you try it? Did it work or were there any errors? I don't know anything about Outlook, but $Mail|Get-Member should give you a list of properties and methods, this might help. Commented May 11, 2015 at 7:57

2 Answers 2

3

I'm not quite sure I understand what you're trying to do. Do you want to replace a literal string #RefID# in the body of the mail with the value of a variable $RefID? If so, that can be done, but you need to do it in the right place (the body of the mail):

$Mail.Body = $Mail.Body.Replace("#RefID#", $RefID)

In case of HTML e-mail you may need to modify the HTMLBody property instead of the Body property:

$Mail.HTMLBody = $Mail.HTMLBody.Replace("#RefID#", $RefID)
Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly what I'm trying to do - but using $Mail.Body.Replace as above, seems to lose the formatting of the email, and turns the company logo into a CID:xxxxxx reference. Is there a way to use Replace without that happening? Thank you Ansgar, Mark
@Mark You may need to modify HTMLBody instead of Body. Not much else I can tell you without sitting in front of your computer.
@AnsgarWiechers - Just. Thank You!!! No other words. I must have searched through 15-20 other StackOverflow threads before I stumbled across this perfect question and your perfect answer. Will re-share your answer where possible on other threads I come across on this topic.
3

A couple of options for you to try. They should both work to replace text in a file.

1)

$file = "D:\users\mark\test.oft"
(Get-Content $file).Replace("#RefID","$RefID") | Set-Content $file

2)

   (Get-Content $file) | 
    Foreach-Object {$_ -replace "#RefID","$RefID"}  | 
    Out-File $file

2 Comments

Outlook templates are binary files and should not be manipulated like this.
Hi Ansgar - do you have any other recommendations for achieving this? Kind regards, Mark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.