Share via


Send email to multiple recipients from text file

Question

Tuesday, May 30, 2017 3:28 PM

I did a membership dump of a number of AD groups (over 200 groups), now I have 200 plus file that I would like to send a message to

The text in the file it looks like this
"mail"
"[email protected]"
"[email protected]"

Here is what I have for the script so far
get-childitem "C:\Temp\Mail\ | foreach{
  $user = get-content $_.fullname
  Send-MailMessage -To $user -From "[email protected]" -SmtpServer SmtpServer.abc123.com -Subject "Test"
      
  }

I getting "Send-MailMessage : An invalid character was found in the mail header: '"'.", looks to be pulling the names from the like "mail" "[email protected]" "[email protected]". I thinking the problem is it is pilling in the header "mail" and its also needs a , after each address. If i change get-content to import-csv the addresses come out looking like @{[email protected]} but header is gone.

All replies (5)

Tuesday, May 30, 2017 3:49 PM

 $user = Import-Csv $_.fullname
  Send-MailMessage -To ($user.Mail -join ',') -From "[email protected]" -SmtpServer SmtpServer.abc123.com -Subject "Test"

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


Tuesday, May 30, 2017 4:42 PM

Thanks, still getting Send-MailMessage : An invalid character was found in the mail header: ','.


Tuesday, May 30, 2017 4:55 PM

Run the commands

$user = Import-Csv <pathToFile>

$user.Mail -join ','

To verify the output is correct, possible your file is not setup properly. I just simply created a csv file with the header of mail, and added two lines, ran the statements above and it looked good on my end

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


Tuesday, May 30, 2017 5:49 PM

Looks like if the file only has one address in it, it works but if it has more than one address it kick back "An invalid character was found in the mail header: ','."

But the on-screen out looks goods


Tuesday, May 30, 2017 6:00 PM

DOH!! If you read the help file for Send-MailMessage, the -To parameter takes a string array, so the proper format would be "[email protected]","[email protected]", the operation I provided was creating a single string.

Set the -To parameter to be $user.mail and you should be all set

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''