by jgk-ps at 2013-03-21 15:58:57
Hello guysby ps_gregg at 2013-03-21 20:58:41
im having some problem with the output from the following:$forwardboxs = (Get-Mailbox | where {$.forwardingaddress -like "contacts"})
$body = @()
$body += foreach ($box in $forwardboxs){
"name: " + $box.Name
"email: " + $box.windowsemailaddress
"forwards to: " + ($box.forwardingaddress | Get-MailContact).windowsemailaddress
"—"
}
Send-MailMessage -To dude -From anotherdude -Subject "forwarding mailboxes" -Encoding UTF8 -Body "$body" -SmtpServer server
in the shell, the output from the $body variable looks like this:
name: dude
email: dude@dude.com
forwards to: anotherdude@dude.com
—
name: anotherdude
email: anotherdude@dude.com
forwards to: dude@dude.com
—
which is the way i want it, however the email i receive just contains one long line, like so:
name: dude email: dude@dude.com forwards to: anotherdude@dude.com — name: anotherdude email: anotherdude@dude.com forwards to: dude@dude.com —
is there anyway to prevent this?
Hi JGK,by mjolinor at 2013-03-22 03:25:25
It looks like you need to add a carriage return/line feed to the end of each line. Also when I am sending emails like this I always declare the body variable as a string, I add each line individually to the variable instead of at the beginning of the foreach loop (the way you had it might contain unwanted spaces) and I don’t need to use the Encoding in the Send-MailMessage cmdlet.
$forwardboxs = (Get-Mailbox | where {$.forwardingaddress -like "contacts"})
[string]$body = @()
foreach ($box in $forwardboxs){
$body += "name: " + $box.Name + "rn"
$body += "email: " + $box.windowsemailaddress + "rn"
$body += "forwards to: " + ($box.forwardingaddress | Get-MailContact).windowsemailaddress + "rn"
$body += "—rn"
}
Send-MailMessage -To dude -From anotherdude -Subject "forwarding mailboxes" -Body "$body" -SmtpServer server
Hope that helps.
-Gregg
An easy way to fix that is to run $body through out-string before you use it as the email body:by jgk-ps at 2013-03-22 10:41:59$body = $body | out-string
Send-MailMessage -To dude -From anotherdude -Subject "forwarding mailboxes" -Body "$body" -SmtpServer server
how awkward, of course out-string would have worked perfectly.
thanks a bunch guys