Sending email to Multiple parties

Hi Powershell Experts,

I use the following code without problem

send-MailMessage -SmtpServer $smtp -To "user1@xxx.com.sg", "user2@xxx.com.sg" -From $from -Subject $subject -Body $body -BodyAsHtml

But using variable fails???
$to = "user1@xxx.com.sg", "user2@xxx.com.sg"
send-MailMessage -SmtpServer $smtp -To $to  -From $from -Subject $subject -Body $body -BodyAsHtml

Can help explain where I go wrong?
LVL 17
NicksonKohAsked:
Who is Participating?
 
Rainer JeschorConnect With a Mentor Commented:
Hi,
can you try this:
$to = @("user1@xxx.com.sg", "user2@xxx.com.sg")
0
 
Meir RivkinFull stack Software EngineerCommented:
that should work:
$recipients = "user1@xxx.com.sg", "user2@xxx.com.sg" 
send-MailMessage -SmtpServer $smtp -To $recipients -From $from -Subject $subject -Body $body -BodyAsHtml

Open in new window

0
 
NicksonKohAuthor Commented:
Hi sedgwick,

I just tested again and while there is no error, it is still not working correctly as the email is only sent to the first person in $recipients. So for the above case, I receive an email with just

To : user1@xxx.com.sg

user2 does not appear in the To list.
0
 
NicksonKohAuthor Commented:
Thx it worked! Does the @() means parameters?
0
 
Rainer JeschorCommented:
Nope, it means an array of strings (or objects)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.