Nagpur Cha SysAdmin is a blog about tips, tricks, free PowerShell scripts, free tools and easy solutions for various day to day and advance tasks of system administrators.

Search This Blog

Tuesday 17 October 2017

Sending mail to multiple recipient at once using PowerShell

Hi All,

Have you ever tried sending mail to multiple recipient at once using PowerShell with Send-MailMessage ?

If yes, then you might also have faced issues while trying this. The most probable error you would have face can be the below one.


Error: An invalid character was found in the mail header: ','
Or there are few other errors which may generate while trying this. We can ignore the other errors and will not require me to put the list of all those alert. Because my main goal behind writing this post is to show you…

How to send email multiple recipient at once using PowerShell.
How to solve Error: An invalid character was found in the mail header: ','

Let’s get started :

Issue /Problem

$To = 'recipient1@xyz.com,recipient1@xyz.com'
send-MailMessage -From sender@xyz.com -to $To  -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25 
If you try to send emails to multiple recipients with above code, you will get below error message. 

Solution:

$To = 'recipient1@xyz.com,recipient1@xyz.com'
send-MailMessage -From sender@xyz.com -to ($To -split ',' ) -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25


This will resolve your issue!!!


The $To variable can also be filled from recipient list present in file in below way to get successful outcome.

$To = Get-Content "C:\test\users.txt";
send-MailMessage -From sender@xyz.com -to ($To -split ',' ) -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25

 The file should look like below.
Important observations.
The discussed issue mainly arises when we are trying to call recipient list from variable/ or from file through variable.

Kindly Share your experience in comment section with my blog/post if it did help you !!

Stay Blessed!!  
Thank You!! 

5 comments:

  1. Hello! Thanks for this post. Here is my code that I CAN NOT get to send an email to multiple recipients:

    param (
    [Parameter(Mandatory)]
    [ValidateScript({Test-Connection -ComputerName $_ -Quiet -Count 1})]
    [string[]]$Computername,
    [string]$LogFilePath = 'D:\ServerAdmins\Audit\Audit.csv',
    [string]$SmtpServer = 'webmail.test.org',
    [string]$EmailFrom = 'user1@test.org',
    [string]$EmailSubject = 'New Local Administrator Detected',
    [string]$EmailRecipient = 'user1@test.org,user2@test.org'

    What am I doing wrong?

    ReplyDelete
    Replies
    1. Hey, sorry for the late response. in case you are still looking for the reason.

      Just look at my -to section in above script, you will find that i am splitting the email id on character "," . that is what you need to do.

      Delete
  2. Hey, sorry for the late response. in case you are still looking for the reason.

    Just look at my -to section in above script, you will find that i am splitting the email id on character "," . that is what you need to do.

    ReplyDelete
  3. Amazing tip, man!
    Thank you!

    Recently had the same issue

    ReplyDelete
  4. Dude... This is awesome. This was exactly what I was looking for.. you solved the issue that I was breaking my head for 4 days. :) :)

    Thank you so much..!!

    ReplyDelete

Comments system