Hello,
I was going through the process of setting up a new module, and with a lot of the work I do, I schedule emailed reports via powershell quite often. So I wanted to create a function that allows me to add a -replyTo parameter on emails.
function SendEmail {
[CmdletBinding()]
[OutputType()]
param (
[Parameter(Mandatory = $true)]
[string]$EmailTo,
[Parameter(Mandatory = $true)]
[string]$EmailFrom,
[Parameter(Mandatory = $false)]
[string]$ReplyTo,
[Parameter(Mandatory = $false)]
[string]$EmailCC,
[Parameter(Mandatory = $false)]
[string]$EmailAttachment,
[Parameter(Mandatory = $True)]
[string]$EmailBody,
[Parameter(Mandatory = $true)]
[string]$EmailSubject
)
$ErrorActionPreference = "SilentlyContinue"
$to = "$EmailTo"
$emailCC = "$EmailCC"
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient("smtp.test.prv")
$msg.From = "$EmailFrom"
$msg.ReplyTo = "$ReplyTo"
foreach ($address in $to) {
$msg.To.Add($address)
}
foreach ($address in $emailCC) {
$msg.CC.Add($address)
}
foreach ($address in $emailBcc) {
$msg.Bcc.Add($address)
}
$msg.Subject = "$EmailSubject"
$msg.Attachments.Add($EmailAttachment)
$msg.Body = $EmailBody
$smtp.Send($msg)
$EmailAttachment.Dispose()
$msg.Dispose()
}
With the command looking like this, and it works with 1 email address in the To parameter;
SendEmail -EmailTo "olaf@powershellforums.org" -EmailFrom "Email Test<Email_Test@powershellforums.org>" -ReplyTo "me@test.com" -EmailAttachment $variable1 -EmailBody "Body Text" -EmailSubject "Subject text"
Problem is when I want to have more than 1 email address in the -EmailTo or -EmailCC fields.
SendEmail : Cannot process argument transformation on parameter 'EmailTo'. Cannot convert value to type System.String.
At line:1 char:20
+ ... il -EmailTo "user1@test.com","user2@test.com" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [SendEmail], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,SendEmail
Just looking for help as to what I’m doing wrong, and anything would be appreciated. Not as fluent on the “Advanced Functions” front.