2

I try to do a script to send mail to multiple recipients. I trace to a file the -To parameter and it looks OK, but when I send the mail only the second recipient receives the mail.

My script:

$mailAddress = $DDSTab2.$clef
$date = Get-Date
Add-Content -Path $LogFile -Value "$date - $Fichier - $mailAddress"

Here is example output from the log file:

08/29/2017 12:02:13 - PV_00049521_2841_DGFIP_93.pdf - < test@live.com>,< test2@live.com>

This doesn't work:

Send-MailMessage -From "dgfip@toto.com" -To $mailAddress -Subject "PV $Fichier" -SmtpServer "192.168.40.252" -Body "Veuillez trouver ci-joint le PV de raccordement. Cordialement" -Attachments $PV

But when I put the literal recipients it does work:

Send-MailMessage -From "dgfip@toto.com" -To <test1@live.com>,<test2@live.com> -Subject "PV $Fichier" -SmtpServer "192.168.40.252" -Body "Veuillez trouver ci-joint le PV de raccordement. Cordialement" -Attachments $PV

And it works. I can't understand the problem!

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
maisonneuve
  • 21
  • 1
  • 1
  • 3
  • 2
    `-To` expects an array of strings (one string per recipient) - looks like you're supplying a single comma-delimited string with all the recipients – Mathias R. Jessen Aug 29 '17 at 10:27

1 Answers1

4

You have pass it as String array. Try this:

$recipients = "test1@live.com", "test2@live.com"

Send-MailMessage -From "dgfip@toto.com" -To $recipients -Subject "PV $Fichier" -SmtpServer "192.168.40.252" -Body "Veuillez trouver ci-joint le PV de raccordement. Cordialement" -Attachments $PV

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • 1
    alternative to the string array: `$recipients = @("test1@live.com", "test2@live.com");` Use whichever suits you – aleksandar Jul 16 '21 at 17:09