Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell EMail: How to add new line in email Body?

Very new to Powershell. Wrote powershell script which is working perfectly but when it sends mail it does not show new line character in body. Following are configurations:

$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "Disk Space Low: $server"

$Body = "Server Name:  $server, <NEED NEW LINE> Drive: C,  <NEED NEW LINE>  Total Size: $sizeGB,  <NEED NEW LINE> Space Left: $freeSpaceGB"

$SMTPServer = "scan.opinergo.fn"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<From mail ID>", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)   

In Body i have used following in place of ; searched around:

1) %0d%0a - NOT WORKING when user checks mail

2) \n - NOT WORKING when user checks mail

3) < br > - Not Working when user check mail

Could anyone please suggest what else could i use to get new line or any modifications required in script?

like image 698
fatherazrael Avatar asked Nov 17 '15 07:11

fatherazrael


1 Answers

(From my comment)

The PowerShell newline indicator is `n (backtick-n), so:

$Body = "Server Name:  $server, <NEED NEW LINE> Drive: C

becomes:

$Body = "Server Name:  $server, `n Drive: C
like image 193
TessellatingHeckler Avatar answered Sep 26 '22 19:09

TessellatingHeckler