我正在尝试向PowerShell中的多个收件人发送电子邮件。
我知道PowerShell 1.0的方式,但我不想使用它。
我的电子邮件功能如下所示
function Send-Mail() {
Param (
# email attributes
[string]$smtpServer,
[string]$from,
[string[]]$to,
[string]$subject,
[string]$body
)
Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from
}
我可以做到这一点:
$smtpServer = "email.server.local"
$from = "[email protected]"
$subject = "Subject"
$body = "Test"
Send-Mail $smtpServer $from ("[email protected]", "[email protected]") $subject $body
..但是如果我放
$to = "[email protected]", "[email protected]"
Send-Mail $smtpServer $from $to $subject $body
邮件仅发给第二个收件人
如果我$to
在函数中本地设置,那么它也可以正常工作,因此问题在于将参数传递给了函数。
我以这种方式发送它,并且它就像一种魅力。PS v4,Windows Server 2012
Send-MailMessage -To "[email protected]", "[email protected]" -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com
为了您的功能,我将电子邮件作为数组提交。这对我也有用。
$array = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-MailMessage -To $array -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com
...最终,这对我来说使用您的功能就可以了。
function Send-Mail() {
Param (
# email attributes
[string]$smtpServer,
[string]$from,
[string[]]$to,
[string]$subject,
[string]$body
)
Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from
}
$array = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-Mail -smtpServer "smarthost.domain.com" `
-from "[email protected]" `
-to $array `
-subject "Test" `
-body "TEST"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句