Reputation: 5987
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?
Upvotes: 10
Views: 69483
Reputation: 11
Just putting it in your script the way you want it to appear worked for me, although I am on version 4.0. Not sure what version you were on or if that makes a difference.
$Body = "Server Name: $server
Drive: C
Total Size: $sizeGB
Space Left: $freeSpaceGB"
Upvotes: 1
Reputation: 7
The PowerShell newline indicator is n, and can only be used in double quotes. Single quotes will print the actual "
n".
$Body = "Server Name: $server, <NEED NEW LINE> Drive: C"
becomes:
$Body = "Server Name: $server,`nDrive: C"
Upvotes: -1
Reputation: 21
technically it needs to be
$body = "first line" + "`n" + "second line"
otherwise the the first line is created with a trailing and the second line with a leading space.
Upvotes: 2
Reputation: 4955
I'm not sure how these other users were able to make the `n create a new line but I never could get it to work...
Instead I ended up doing the following...
-BodyAsHtml
switch<br />
(break) html tagFor example...
powershell -ExecutionPolicy ByPass -Command Send-MailMessage -BodyAsHtml -SmtpServer 'myemail.server.com' -To 'Mr To <[email protected]>' -From 'Mr From <[email protected]>' -Subject 'Reports From Daily SQL Backup Jobs' -Body 'There was an issue backing up the databases and sending them to the virtual drive.<br />Test Line 2.<br />Test Line 3.-more on line 3<br />Test Line 4.<br />EOF<br />'"
Upvotes: 13
Reputation: 29048
(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
Upvotes: 14