user12821291
user12821291

Reputation:

Add multiline body to powershell email

I´m trying to add multilines to the body, but can´t find the fix. I tried to do it with html but i don´t know how...

$Email       = "x"
$Internal    = "x"
$Subject     = "New"
$Body        = "Sending new files. Cheers!"

[array]$attachments = Get-ChildItem "\\ip\ftp$\new\Fac" *.pdf

if ([array]$attachments -eq $null) {
}

else {

$Msg = @{
    to          = $Email
    cc          = $Internal
    from        = "to"
    Body        = $Body
    subject     = "$Subject"
    smtpserver  = "server"
    BodyAsHtml  = $True
    Attachments = $attachments.fullname
}

Send-MailMessage @Msg

}

Upvotes: 0

Views: 811

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

If you want a multi-line string literal, use here-strings:

$Body = @'
Multiple
lines

go

here
'@

You can also construct a multi-line string from multiple individual strings with the -join operator:

$Body = 'Line1','Line2','Line3' -join [Environment]::NewLine

But since you want HTML, better join with <br /> instead:

$Body = 'Line1','Line2','Line3' -join '<br />'

Upvotes: 2

Related Questions