- June 22, 2016 at 7:43 pm #44449
I feel I have tried every which way to set the body of an email with the contents of two files, but not getting the expected result.
I retrieve the content of each file and set the content of this data to a file called body.txt:
get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_US.txt" | Add-Content c:\scripts\output\body.txt
get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_MEX.txt" | Add-content c:\scripts\output\body.txt
The file body.txt, if I open it in notepad, clearly shows CRLF at the end of each line. I then set the variable $body to a string by doing the following:
$body = (get-content -path c:\scripts\output\body.txt) | out-string
When I then try to email using send-mailmessage -body $body .... the body of the email contains all of my lines in one long string. I know the out-string does this, but the send-mailmessage requires the info to be a string.
I will bet I am doing WAY too much manipulation of the data 🙂 ...
Would someone please give me a suggestion?
Thanks!June 22, 2016 at 8:26 pm #44458
This works fine for me as long as the "-BodyAsHtml" parameter is not included with Send-MailMessage. If HTML is required, then some extra formatting steps may be needed.June 22, 2016 at 10:07 pm #44473
Sorry I was not clear – the lines all come out in one string but I'd like them to appear in the body of the message as the original lines with CRLF for easy reading.
Thx!June 23, 2016 at 1:37 am #44489
The body parameter requires a string value. Get-Content, by default, returs an Array of lines. These two methods worked for me:
Adding the -Raw switch:
$body = Get-Content C:\Users\Rob\Desktop\test.txt -Raw
or using FileSystemObject, vbScript ole skool style:
$body = (New-Object -Com Scripting.FileSystemObject).OpenTextFile("C:\Users\Rob\Desktop\test.txt",1).ReadAll()
You must be logged in to reply to this topic.