[英]Sending a multiple line body email from batch file using a PowerShell command
我正在嘗試編寫一個批處理文件,它將使用 PowerShell 命令發送 email。 我唯一的挑戰是向 email 主體添加多行。
我在變量 EmailBody 中聲明消息,然后在 PS 命令中使用它。 問題是當我在 Outlook 2016 收到 email 時,``r `n 僅顯示為純文本。 我嘗試使用 HTML 標簽
但這失敗了,因為即使我在批處理文件中使用 ^ 轉義它們,PowerShell 也會看到 ^ 並且不喜歡它。
@echo off
:: Log File Configuration
SET DateStamp=%date:~-4,4%%date:~-7,2%
SET LogFile=F:\WinSCP_%DateStamp%.log
:: Email Configuration
SET SMTPServer=server.com
SET EmailFrom=%computername%@company.com
SET EmailTo="name1@company.com", "name2@company.com"
SET EmailSubject=Subject
SET EmailAttachment=%LogFile%
SET EmailBody="Line1 Line2 Line3"
Some code
Powershell.exe -executionpolicy remotesigned -Command "& {Send-MailMessage -To '%EmailTo%' -From '%EmailFrom%' -Subject '%EmailSubject%' -SmtpServer '%SMTPServer%' -Body '%EmailBody%' -Attachments '%EmailAttachment%'}"
some code
這是問題代碼的修訂版。 希望這也比單行代碼更具可讀性。 將其全部寫在 PowerShell 中會使它變得更加簡單。
SET "EmailSubject=Subject"
SET "EmailBody=Line1 Line1`r`nLine2 Line2`r`nLine3 Line3"
SET "EmailAttachment=%USERPROFILE%\tp.txt"
powershell.exe -NoLogo -NoProfile -Command ^
"Send-MailMessage" ^
"-To %EmailTo%" ^
"-From '%EmailFrom%'" ^
"-Subject '%EmailSubject'" ^
"-SmtpServer '%SMTPServer%'" ^
"-Body '%EmailBody%'" ^
"-Attachments %EmailAttachment%"
您應該可以在任何需要換行的地方輸入 `r`n,PowerShell 將評估為回車和換行(即 Windows 樣式的新行)。
但是,我同意其他答復,即在純 PowerShell 中執行此操作可能是一個更好的主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.