Powershell Send-MailMessage from multiple recipients from sql table

Soldato
Joined
8 Mar 2005
Posts
3,869
Location
London, UK
So I've set up a script which basically pulls data from a sql table and then based on certain conditions will then send out a mail notification. Historically I set this up to only send to a single recipient but now need top expand this via the "Send-MailMessage" cmdlet.

With this in mind I altered the sql table which stores the recipients as a single string in the following format (Write-Host confirms this):
The data does get pulled in correctly as a variabe
Code:
$mailrecipients = $item.mailrecipients
However when I pass this via the Send-Mailmessage cmdlet
Code:
Send-MailMessage -To $mailrecipients -From "[email protected]" -Priority Low -Subject "foosubject" -Body "foobody" -smtpServer 1.2.3.4
Send-MailMessage balks with
Code:
Send-MailMessage : An invalid character was found in the mail header: '"'....
Send-MailMessage : A recipient must be specified.
I must assume it does not like the format passed with $mailrecipients but various web checks suggest it is good.

Any help/pointers much appreciated.

TIA, Paul.
 
Last edited:
So I have removed the double-quotes between addresses
and the error is removed yet no mails are sent.


Code:
[string[]]$mailrecipients = @("<[email protected]>","<[email protected]>","<[email protected]>")
Send-MailMessage -To $mailrecipients -From "[email protected]" -Priority Low -Subject "foosubject" -Body "foobody" -smtpServer 1.2.3.4
EDIT - Works with a string array.

However this does not work inside my code and continues to throw out the invalid '"' character...

Something clearly awry with my code yet I can't see why its failing. I can see the recipients variable is populated and the command being executed.

EDIT
Clearly the way I'm handling multiple recipients is at fault. it does not get passed correctly to Send-MailMessage.

Tried various formats with no luck.
 
Last edited:
Powershell 3.0 (just using the standard ISE).
The friendly name is optional as confirmed in my previous post.

I suspect the issue is the variable isn't being passed or interpreted correctly from the sql query, even though the variable does appear to be populating correctly. The sql query pulls from the table previously but now it includes an additional column with the joined recipients list as a single string. (Comma delimited).

EDIT:

I think I've discovered the issue;

The sql data which populates the $mailrecipients is in the string format
This is a single string and not an array so is treated as a single mail address entry and promptly fails due to multiple '"' invalid characters.

SOLUTION
Code:
$Mailarray = $mailrecipients -split ','
 
Last edited:
Back
Top Bottom