beehive
beehive

Reputation: 93

powershell newline in Send-MailMessage body

how do I get Send-MailMessage to put new lines in the body of the mail? A separate PowerShell script generates some files, I read the content into a variable and send it as the body like so:

$fileContent = Get-Content -Path $file | Out-String
Send-MailMessage -BodyAsHtml -Body $fileContent -From $f -SmtpServer $s -To $t -Subject $s -Verbose

$fileContent would currently have the

`r`n 

characters. When you open the file it has new lines. But when I convert to string and send the email its all on one line. I have tried replacing like this:

$fileContent -replace  "`r`n","<br />"
$fileContent -replace  '`r`n','`r`n`r`n`r`n'

If anyone can help I would be very appreciative, thanks!

Upvotes: 1

Views: 6249

Answers (1)

Hasan Patel
Hasan Patel

Reputation: 412

These are some of the approaches you can try:

  1. Using the -BodyAsHtml switch
  2. Using the <br /> (break) html tag

for example:

powershell -ExecutionPolicy ByPass -Command Send-MailMessage -BodyAsHtml -SmtpServer 'myemail.server.com' -To 'Mr To <[email protected]>' -From 'Mr From <[email protected]>' -Subject 'Powershell BodyAsHtml' -Body 'I am just adding some random words here<br />and here also Line 2.<br />And also here Line 3.<br />and one more time Line 4.<br />EOF<br />'"

The PowerShell newline indicator is `n (backtick-n)

Upvotes: 2

Related Questions