locked
Send email to multiple recipients from text file RRS feed

  • Question

  • 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"
    "user2@abc123.com"
    "user3@abc123.com"

    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 "user1@abc123.com" -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" "user2@abc123.com" "user3@abc123.com". 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 @{mail=user2@abc123.com} but header is gone.

    Tuesday, May 30, 2017 3:28 PM

All replies

  •  $user = Import-Csv $_.fullname
      Send-MailMessage -To ($user.Mail -join ',') -From "user1@abc123.com" -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 3:49 PM
  • Thanks, still getting Send-MailMessage : An invalid character was found in the mail header: ','.
    Tuesday, May 30, 2017 4:42 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 4:55 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

    • Edited by MGO1970 Tuesday, May 30, 2017 5:49 PM
    Tuesday, May 30, 2017 5:49 PM
  • DOH!! If you read the help file for Send-MailMessage, the -To parameter takes a string array, so the proper format would be "user1@somewhere.com","user2@somewhere.com", 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''

    Tuesday, May 30, 2017 6:00 PM