Проблемы Send-MailMessage CC и BCC - PullRequest
0 голосов
/ 30 ноября 2018

Я взял более старый скрипт для отправки электронных писем и пытался сделать его так, чтобы мы могли копировать и отправлять вложения и помещать его в функцию, чтобы мы могли неоднократно использовать его в нашей среде.

У меня возникли проблемыс отправкой электронной почты с Send-MailMessage, когда есть несколько человек, мне нужно в CC.Я установил некоторые параметры для функции, которые позволяют кому-то вводить CC или BCC, а также некоторые параметры, которые при добавлении в функцию автоматически отправляют на определенные адреса.

$ 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...