I have a *.csv file with a few columns and I want to use the UsersTo and UsersCc list of users to send messages. The problem is with $CcAddress as I have multiple email address once I format them I get the following : error: Send-MailMessage : An invalid character was found in the mail header: ','. At C:\Discovery Scan Process\RSU notifications\Notifications.ps1:43 char:2
- Send-MailMessage -Encoding UTF32 -to $ToAddress -Cc $CcAddress -from $FromAddre ...
-
+ CategoryInfo : InvalidType: (:) [Send-MailMessage], FormatException + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage
CSV looks like this:
UsersTo,UsersCc,Domain
unknowTest.Test@test.com,"testOpen@new.com,Pvo.ovi@new.com",test.test
unknowNew.test@test.com,"testOpen@new.com,testOpen@new.com",new.test
unknowprod.mx@test.com,"testOpen@new.com,Evo.x@new.com",prod.test
unknowX.SS@test.com,"testOpen@new.com,Spekor1.@new.com",uat.test
Code:
$notificaitonList = import-csv .\'listOfUsers - Copy.csv'
$domains = 'test.test','prod.test'
foreach ($domain in $domains){
$FromAddress = "some@address"
$ToAddress = ($notificaitonList | Where-Object {$_.domain -like $domain}).UsersTo | foreach {"`"$_`""}
$Ccstr = (($notificaitonList | Where-Object {$_.domain -like "$domain"}).UsersCc).split(",")
$Ccstr = $Ccstr | foreach {"`"$_`""}
[string[]]$CcAddress= $Ccstr.Split(",") | foreach {"`"$_`""}
[string] $MessageSubject = "MessageSubject "
[string] $emailbody = "emailbody"
$SendingServer = "server"
Send-MailMessage -Encoding UTF32 -to $ToAddress -Cc $CcAddress -from $FromAddress -subject $MessageSubject -smtpServer $SendingServer -body $emailbody
}