none
Insert line break in -Body field when sending Powershell email

    General discussion

  • Hi,

    I've got a script that sends an email using Send-MailMessage with a couple of attachments but I'd like to put a line break (enter) in the body of the email. I'm sure this must be possible, but can't find how to do it.

    Current output:

    'The quick brown fox jumps over the lazy dog'

    Desired output:

    'The quick brown fox

    jumps over the lazy dog'

    Thanks in advance.

    Chris


    • Changed type Tiger Li Wednesday, August 03, 2011 10:36 AM
    Wednesday, July 27, 2011 1:14 PM

All replies

  • One solution would be to use <br /> to break lines and specify the switch -BodyAsHtml so that body would be treated as html and email client would display it correctly, alternatie approach would be to use `n for line breaks and keep body as text
    With kind regards
    Krystian Zieja
    http://www.projectenvision.com
    Follow me on twitter
    My Blog
    Need help with your systems?
    Wednesday, July 27, 2011 1:25 PM
  • I think the best way to do this is to use a here-string for the email body:

    $body = @"
    The quick brown fox
    jumps over the lazy dog
    "@

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Robert Little Wednesday, July 27, 2011 2:46 PM
    Wednesday, July 27, 2011 1:41 PM
  • I have tended to use the <br /> approach, though, mjolinor's here Strings are cleaner and easier than copying and pasting <br /> fifty times for long emails.  If you use <br /> in the body, make sure you switch the -BodyAsHtml flag on, otherwise, you get a lot of <br /> interspersed between text in the body of your email.

    $body = @"
    "here
    strings
    rock"
    "@
    Send-MailMessage -Body $body -From "you@youmail.com" -Subject "break it down" -To "me@memail.com"
    

    Here-strings:

    http://technet.microsoft.com/en-us/library/ee692792.aspx

    Wednesday, July 27, 2011 1:50 PM
  • This will work:

    -Body "The quick brown fox `njumps over the lazy dog"

    However, I like Rob's here string idea better.

    Karl


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, July 27, 2011 1:56 PM
  • in a more general sense, you typically end up with an array of strings more so than you do a single large string. if this is the case you can do the following

    [string]::join("<br/>",$body)


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, July 27, 2011 2:14 PM
  • Or the "old school" way:

    $body = "The quick brown fox","jumps over the lazy dog"
    $ofs = '<\br>'
    [string]$body


    The quick brown fox<\br>jumps over the lazy dog


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, July 27, 2011 2:29 PM
  • yes, the automatic variable $ofs
    http://blogs.msdn.com/b/powershell/archive/2006/07/15/what-is-ofs.aspx

    I find that to be a bit more dangerous since it impacts the entire script...


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, July 27, 2011 2:32 PM
  • I use it occasionally.  It's safer used in a fuction or scriptblock:

    $body_strings = "The quick brown fox","jumps over the lazy dog"

    $body = &{$ofs='<\br>';[string]$body_strings}
    $body

    The quick brown fox<\br>jumps over the lazy dog

    $body = [string]$body_strings
    $body

    The quick brown fox jumps over the lazy dog



    The script or function scope protects $ofs in the parent scope.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, July 27, 2011 2:48 PM