0

I'm trying to send email to multiple recipients in PowerShell.

I know about the PowerShell 1.0 way but I do not want to use it.

My email function looks like this

function Send-Mail() {

    Param ( 
            # email attributes    
            [string]$smtpServer,
            [string]$from,
            [string[]]$to,
            [string]$subject,
            [string]$body
    )

    Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from 
}

I can achieve what I want doing this:

$smtpServer = "email.server.local"
$from = "[email protected]"
$subject = "Subject"
$body = "Test"

Send-Mail $smtpServer $from ("[email protected]", "[email protected]") $subject $body

..but if I put

$to = "[email protected]", "[email protected]"
Send-Mail $smtpServer $from $to $subject $body

Mail only goes to the second recipient

If I set $to locally in the function, this also works correctly, so the problem is passing the parameter to the function.

9
  • What does I know about the PowerShell 1.0 way but I do not want to use it. mean? Commented Apr 1, 2016 at 13:22
  • What version of PowerShell are you using? Get-Host. I don't see and issue here with your second example and this is working for me. Commented Apr 1, 2016 at 13:24
  • 1
    Did you have $to strongly cast in your current session at some point? $to.GetType().fullname Commented Apr 1, 2016 at 13:37
  • 2
    AHA..... that would be an issue then. It should be a system.object[] or system.string[]. Save your code and reopen ISE / PS with a new session. Does it work then? If you strongly cast the variable you would not be able to change it later without first removing it. Remove-Variable to Commented Apr 1, 2016 at 13:40
  • 1
    Your comment before this prompted me to do a rm variable:to . Have just re-run the code and it's working. Thanks a lot as this has bugged me out since last night. Phew! Cheers. Commented Apr 1, 2016 at 13:44

3 Answers 3

1

The code you have listed here should have worked just fine. PowerShell is rather forgiving when it comes to typing and it will make the best selection for you. However you are able to cast yourself as [string] for example which I suspect your did at some point in your session before you hosted. Consider the following examples

PS C:\Users\matt> $to = "[email protected]", "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.Object[]

PS C:\Users\matt> [string]$to = "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.String

PS C:\Users\matt> $to = "[email protected]", "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.String

Note that in the last set you might have expected System.Object[] but since it was strongly cast in an earlier line that it remains that type until it is removed.

You can also see this when examining the variable

PS C:\Users\mcameron> Get-Variable to | fl

Name        : to
.... output truncated ...
Attributes  : {System.Management.Automation.ArgumentTypeConverterAttribute}

Key point here is the Attribute System.Management.Automation.ArgumentTypeConverterAttribute which showed up after the strong cast. During a session or during code execution you can simple remove the variable with Remove-Variable to which would let you start over.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the above explanation. It would have been more useful if you put the console output under your inputs in the initial section so it was easier to follow EDIT: Just seen that you did do that but the highlighting confuses things (at least to me). Thanks.
0

I sent it like this and it worked like a charm. PS v4, Windows Server 2012

Send-MailMessage -To "[email protected]", "[email protected]" -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com

for your function purposes, I would submit the emails as an array. This also worked for me.

$array = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-MailMessage -To $array -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com

...and ultimately, this worked for me just fine using your function.

function Send-Mail() {
    Param ( 
        # email attributes    
        [string]$smtpServer,
        [string]$from,
        [string[]]$to,
        [string]$subject,
        [string]$body
    )
    Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from 
}
$array  = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-Mail -smtpServer "smarthost.domain.com" `
          -from       "[email protected]"      `
          -to         $array                 `
          -subject    "Test"                 `
          -body       "TEST"

Comments

0

$to = @("[email protected]", "[email protected]") will set $to to be an array

4 Comments

That still doesn't work unfortunately. I should have mentioned that I had tried that.
What version of powershell? Is Set-StrictMode active? Any other attempts you have tried?
Version is PowerShell 4.0 and Strict Mode is active
In this context ("[email protected]", "[email protected]").GetType().fullname is the same as @("[email protected]", "[email protected]").GetType().fullname. PowerShell is smart enough to figure this one out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.