Powershell send-mailmessage - email to multiple recipients
$recipients = "Marcel <[email protected]>, Marcelt <[email protected]>"
is type of string
you need pass to send-mailmessage
a string[]
type (an array):
[string[]]$recipients = "Marcel <[email protected]>", "Marcelt <[email protected]>"
I think that not casting to string[] do the job for the coercing rules of powershell:
$recipients = "Marcel <[email protected]>", "Marcelt <[email protected]>"
is object[]
type but can do the same job.
Just creating a Powershell array will do the trick
$recipients = @("Marcel <[email protected]>", "Marcelt <[email protected]>")
The same approach can be used for attachments
$attachments = @("$PSScriptRoot\image003.png", "$PSScriptRoot\image004.jpg")
You must first convert the string to a string array, like this:
$recipients = "Marcel <[email protected]>,Marcelt <[email protected]>"
[string[]]$To = $recipients.Split(',')
Then use Send-MailMessage
like this:
Send-MailMessage -From "[email protected]" -To $To -subject "New files" -body "$teloadmin" -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer 192.168.170.61
To define an array of strings it is more comfortable to use $var = @('User1 ', 'User2 ').
$servername = hostname
$smtpserver = 'localhost'
$emailTo = @('username1 <[email protected]>', 'username2<[email protected]>')
$emailFrom = 'SomeServer <[email protected]>'
Send-MailMessage -To $emailTo -Subject 'Low available memory' -Body 'Warning' -SmtpServer $smtpserver -From $emailFrom