Edit email using powershell and retain the HTML format - powershell

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)

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

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.

Related

HTML email body truncation while sending email from outlook using PowerShell

I have one HTML file which i want to send from PowerShell using outlook. I have used below code however it is truncating the email body after 603 chars, hence only upper half of the html page is going as body in outllook email.
$body = Get-Content -Path .\\Output.html #this have around 1500 chars.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "xyz#abc.com"
$Mail.Subject = "Daily Dump Status"
$body.ToString()
$Mail.HTMLBody = $body # while coping the data only 603 char are going into body.
$Mail.Send()
[System.Runtime.Interopservices.Marshal\]::ReleaseComObject($Outlook) | Out-Null
I have also tried options like Out-String while coping but didnt work
There is a limit of data you could set for a property. In the Outlook object model string properties are limited in size depending on the information store type.
To bridge the gap you need to use a low-level API on which Outlook is based on - Extended MAPI which allows opening properties with stream for writing huge amount of data. The IMAPIProp::OpenProperty method provides access to a property through a particular interface. OpenProperty is an alternative to the IMAPIProp::GetProps and IMAPIProp::SetProps methods. When either GetProps or SetProps fails because the property is too large or too complex, call OpenProperty.
You may also take a look at the famous and popular wrapper around Extended MAPI - the Redemption library.
After spending hours, finally got the root cause of this issue. The html page which i was trying to sent as email body was the output of another powershell command using ConvertTo-Html. And the output of PowerShell command had "NUL" in it.
$Mail.HTMLBody = $body
Hence above command was coping the data till first "NUL". I identified this by opening the html page in notepad++. Issue got resolved after replacing NUL with " "

Add sensitivity label to email via powershell

I've got a need to send email within PowerShell and have all the components except the sensitivity label. For example:
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = 'someone#co.com'
$Mail.Subject = 'Special subject'
$SigString=Get-Content "C:\Users\someone\AppData\Roaming\Microsoft\Signatures\sig.htm"
$Mail.HTMLBody = 'blah blah blah.'
$Mail.HTMLBody += $SigString
$Mail.Send()
Unfortunately when this runs I'm prompted for the sensitivity label. I thought "Sensitivity = " might work and it does allow values 0 thru 3 but that doesn't help.
Is this possible to do?
I also had this problem and I could not find any way to do this "outside" of an Office program as this seems to be a built-in enterprise function allowing only a given set of sensitivity labels set by your comany.
If Python can be an alternative, you this answer can automate sensitivity label setting. Alternatively, having a pre-stored template that can be copied and then edited would also work with PS.

Send emails with Powershell mailto with formated text

My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
I appreciate any insights on this.
My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
Additional information:
Code:
$outPut = 'Start-Process'
$outPut+= '"mailto:'+$userMail+"?Subject=Password Reset"+"&Body=Hi, your password is $Password"
$outPut+= '";'
$mailFile = "Path" + $user.SAM + ".ps1"
$outPut | Out-File $mailFile
So, this takes the information this way and stored it in a ps1 file, then executed, opening a new Gmail tab with proper data.
I need that some words has format, bold for the password or hyper-link for guideness link...
Regards!
You haven't provided any indication of what you are doing. But the way to send emails via PowerShell is with the Send-MailMessage cmdlet. If you are using Send-MailMessage and formatting the body of the message with HTML, you just need to make sure you are using the -BodyAsHtml argument.
Here's an example:
$html = "<body><h1>Heading</h1><p>paragraph.</p></body>"
Send-MailMessage -To "bob#fake.com" -From "me#fake.com" -Subject "Test" -Body $html -BodyAsHtml

How can I use Powershell to extract mail headers from .msg file?

I'm trying to write a script that reads the mail headers from a directory full of .msg files so I can later parse them via regex. I tried $MSG = Get-Content .\message.msg, which could work, but it's a pretty dirty output. Has anyone tried this? I can't seem to find a working example online.
You have a few options depending on your environment. If you are on a computer with Outlook installed you can easily do this with an Outlook com object. The problem is that the headers are not exposed by default so you have to dig for them.
$ol = New-Object -ComObject Outlook.Application
$msg = $ol.CreateItemFromTemplate("SOME\PATH\TO\A\MSG\FILE.msg")
$headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
$headers
At this point you have a text block with all of the header information in it. If you want a specific header you will need to write a regex to extract it.
You could also write a class that reads the raw content based on the specification. Or read in the raw content with powershell and write a regex to attempt to extract it.

Powershell - Get value of text box on web page and save as txt file

I am currently scripting something that will go to a certain web page. Once on the web page it will click the run button which will calculate a value on the webpage and put it in a read only text area with the id="result Console". The code that does this is below:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("https://example.com/example.aspx")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.className -eq "runButton"}
$Link.click()
After it does the above I need to get the text that is in the text area and save it to a txt file.
I was wondering if this was possible and if so how I would go about doing so?
I presume I could get the text from the text area in to a variable and then export this using the below command:
$example | out-file -filepath C:\temp\example.txt
I would appreciate any help you can give on this.
Thanks in advance,
SG
If the textarea has an id value set, I guess you can use the getElementById with the id as parameter:
$example = $ie.Document.getElementById("id of the textarea")
$example.value #should have the text you want to write to the txt file
It's been a while I wrote Powershell script but I remember doing it this way last time I needed the text from a textarea.
Hope this helps, good luck.