Powershell Trick: Convert TXT to HTML and Send Email
Posted by Brajesh Panda on May 17, 2016
If you want to get contents from a txt file and send its content in email body, this trick will do the job. My txt file has multiple separate lines, like general error files.
So this trick joins all lines with <BR> HTML encode to introduce line breaks. If you do not use this encode and just do get-content, all lines will be messed like a paragraph and will be hard to read.
$Body = (Get-Content <File Path>) -join ‘<BR>’
After you encode the html, make sure use BodyAsHtml to set outgoing email format. So email client will convert html page back to readable format.
Send-MailMessage -From <> -To <> -Subject <Message> -Body $Body -BodyAsHtml -SmtpServer smtp.server