Share via


error message with a script sending emails to multiple recipients.

Question

Tuesday, January 7, 2014 10:31 AM

Hi all,

I am very new with powershell scripting and I want to request your attention and help on this issue.

I am getting this error message "Send-MailMessage: An invalid character was found in the mail header: ','" given by this script:

Param (
    [string]$Path = "C:\Temp\",
    [string]$SMTPServer = "smtp.domain1.com",
    [string]$From = "[email protected]",
    [string[]]$To = "[email protected],[email protected]",
    [string]$Subject = "New backup file"
    )

$SMTPMessage = @{
    To = $To
    From = $From
    Subject = "$Subject at $Path"
    Smtpserver = $SMTPServer
}

$File = Get-ChildItem $Path | Where { $_.LastWriteTime -ge [datetime]::Now.AddHours(-6) }
If ($File)
{   $SMTPBody = "`nThe following files have recently been added/changed:`n`n"
    $File | ForEach { $SMTPBody += "$($_.FullName)`n" }
    Send-MailMessage @SMTPMessage -Body $SMTPBody
    
}

I found the ForEach() approach but when I put it inside I get an infinite loop.

Many thanks for any suggestions

All replies (2)

Tuesday, January 7, 2014 10:39 AM âś…Answered

[string[]]$To = "[email protected],[email protected]",

should be

[string[]]$To  = @("[email protected]","[email protected]")

Tuesday, January 7, 2014 11:25 AM

thanks it worked