Send-MailMessage CC和BCC问题

时间:2018-11-29 22:15:19

标签: powershell

我使用了一个较旧的脚本来发送电子邮件,并试图做到这一点,以便我们可以抄送并发送附件并将其放在一个函数中,以便我们可以在环境中重复使用它。

当我需要抄送多个人时,我无法通过Send-MailMessage发送电子邮件。我在一个函数上设置了一些参数,使某人可以输入抄送或密件抄送,但是还添加了一些开关,这些开关添加到该函数后将自动发送到某些地址。

$ cc_people尝试用作抄送时返回错误。  “无法验证参数'cc'上的参数。参数null,空或参数集合的元素包含空值。提供不包含任何空值的集合,然后它们再次发出命令。

function Send-IntEmail() {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [alias("Subject")]
        $EnterSubject,
        [Parameter(Mandatory=$True)]
        [alias("Body")]
        $EnterBody,

        [alias("cc")]
        $whatcc,
        [alias("bcc")]
        $whatbcc,

        [switch]$AddOnePerson,
        [switch]$AddSecondPerson,

        #[Parameter(Mandatory=$False)]
        [alias("Attach")]
        $attachments
    )

    #These never change
    $CLRRecipient = "EmailEntered@here.com"
    $msg = New-Object Net.Mail.MailMessage
    $smtpServer = "smtp"
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)

    $from = "no-reply@here.com"
    $msg.ReplyTo = $from
    $msg.From = $from

    #User defines these when running function
    $msg.To.Add($CLRRecipient)
    $msg.Subject = "$EnterSubject"
    $msg.Body = "$EnterBody" + "$(Get-TimeStamp)"+"`nEmail sent by $env:USERNAME"

    $cc_people = @()
    $bcc_people = @()

    if ($AddOnePerson) {
        $LK = "Email@Address.com"
        $cc_people += $LK
        #Write-Host "Including 1 as CC"
    }
    if ($AddSecondPerson) {
        $BB = "Email2@Address.com"
        $cc_people += $BB
        #Write-Host "Including 2 as CC"
    }
    if ($null -eq $cc) {
       $cc_people += $whatcc
       #Write-Host "Including $whatcc as CC"
    }
    if ($null -eq $bcc) {
       $bcc_people += $whatbcc
    }


    #Put an attachment on the email
    if ($attachments -ne $null) {
        Write-Host "`nYou are attaching the following to your email $Attachments `n Your email is being sent to $clrrecipient" -ForegroundColor Yellow
        $AttachBody = "$EnterBody" + "$(Get-TimeStamp)"+"`nEmail sent by $env:USERNAME"
        Write-Host "happens for every attachment email"

        #Evaluate if there is any CC or BCC
        if ($cc_people -ne $null) {
            ##Evaluate if there is a BCC and send with BCC and CC
            if ($bcc_people -ne $null) {
                Send-MailMessage -To $CLRRecipient -From $from -Subject $EnterSubject -Body $AttachBody -SmtpServer $smtpServer -Attachments $attachments -CC $cc_people -Bcc $bcc_people
                Write-Host "`n If you don't seen any errors the email has been sent to $CLRrecipient `n" -ForegroundColor Magenta
                Write-Host "CC: Included to $cc_people" -ForegroundColor DarkGreen -BackgroundColor DarkYellow
                Write-Host "BCC: Included to $bcc_people"-ForegroundColor DarkGreen
                Break
            } else {
                Write-Host "This made it through the CC Eval and evaluated there wasn't any BCC"
                Send-MailMessage -To $CLRRecipient -From $from -Subject $EnterSubject -Body $AttachBody -SmtpServer $smtpServer -Attachments $attachments -cc $cc_people
                Write-Host "CC: Included to $cc_people" -ForegroundColor DarkGreen -BackgroundColor DarkYellow
                Write-Host "`n If you don't seen any errors the email has been sent to $CLRrecipient `n" -ForegroundColor Red
                Break
            }
        }

        if ($bcc_people -ne $null) {
            Send-MailMessage -To $CLRRecipient -From $from -Subject $EnterSubject -Body $AttachBody -SmtpServer $smtpServer -Attachments $attachments -Bcc $bcc_people
            Write-Host "`n If you don't seen any errors the email has been sent to $CLRrecipient `n" -ForegroundColor Magenta
            Write-Host "No One is CC on this email" -ForegroundColor DarkGreen
            Write-Host "BCC: Included to $bcc_people"-ForegroundColor DarkGreen
            Break
        }

        #There are no CC or BB on the email just send to To and attachments

        Send-MailMessage -To $CLRRecipient -From $from -Subject $EnterSubject -Body $AttachBody -SmtpServer $smtpServer -Attachments $attachments
        Write-Host "No CC or BCC Included"
        Write-Host "`n If you don't seen any errors the email has been sent to $CLRrecipient `n" -ForegroundColor Gray
        # End Attachments
    }

    $smtp.Send($msg)
    $msg.Attachments.Clear()
    $smtp.SendCompleted
    Write-Host "`n If you don't seen any errors the email has been sent to $CLRrecipient" -ForegroundColor Magenta
}

0 个答案:

没有答案