Send-MailMessage正文中的powershell换行符

时间:2018-08-27 21:33:21

标签: powershell

如何获取Send-MailMessage在邮件正文中添加新行? 一个单独的PowerShell脚本生成一些文件,我将内容读入一个变量并将其作为正文发送,如下所示:

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

$ fileContent当前将具有

`r`n 

个字符。当您打开文件时,它具有新行。但是,当我转换为字符串并将电子邮件全部发送到一行时。我尝试过这样替换:

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

如果有人能帮助我,我将非常感激,谢谢!

1 个答案:

答案 0 :(得分:1)

这些是您可以尝试的一些方法:

  1. 使用-BodyAsHtml开关
  2. 使用<br />(中断)html标记

例如:

powershell -ExecutionPolicy ByPass -Command Send-MailMessage -BodyAsHtml -SmtpServer 'myemail.server.com' -To 'Mr To <to@server.com>' -From 'Mr From <from@server.com>' -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 />'"

PowerShell换行符为`n(反引号-n)