• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

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?
0
NicksonKoh
Asked:
NicksonKoh
  • 2
  • 2
1 Solution
 
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
 
Rainer JeschorCommented:
Hi,
can you try this:
$to = @("user1@xxx.com.sg", "user2@xxx.com.sg")
0
 
NicksonKohAuthor Commented:
Thx it worked! Does the @() means parameters?
0
 
Rainer JeschorCommented:
Nope, it means an array of strings (or objects)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now