Send-OutlookMailMessage with Voting Options

I needed a way to programmatically send emails through Outlook that contained "voting options" ... the little buttons that let the recipients vote yes or no or whatever on a given topic.  So I whipped up a reusable cmdlet right quick.

#
Function Send-OutlookMailMessage
{
    [CmdletBinding()]
    Param([Parameter(Mandatory=$True)][String[]]$To,
          [Parameter(Mandatory=$False)][String]$Subject,
          [Parameter(Mandatory=$False)][String]$Body,
          [Parameter(Mandatory=$False)][String[]]$Attachments,
          [Parameter(Mandatory=$False)][String[]]$VotingOptions,
          [Parameter(Mandatory=$False)][ValidateSet('High','Low','Normal')][String]$Priority = 'Normal',
          [Parameter(Mandatory=$False)][Switch]$BodyAsHTML = $False)

    BEGIN
    {
        Set-StrictMode -Version Latest
        [Bool]$ShouldProcess = $True
        [Bool]$OutlookWasAlreadyRunning = $True

        Try
        {
            Get-Process Outlook -ErrorAction Stop | Out-Null
        }
        Catch
        {
            $OutlookWasAlreadyRunning = $False
        }

        Try
        {
            $Outlook = New-Object -ComObject Outlook.Application -ErrorAction Stop
            If ($Outlook -EQ $Null)
            {
                Throw 'COM Object was null.'
            }
            Write-Verbose -Message 'Outlook COM object loaded.'
        }
        Catch
        {
            Write-Error "Failed to load Outlook! ($($_.Exception.Message))"        
            $ShouldProcess = $False
        }
    }
    PROCESS
    {
        If ($ShouldProcess)
        {
            Try
            {
                $Mail = $Outlook.CreateItem(0)
                If ($Mail -EQ $Null)
                {
                    Throw
                }
                Write-Verbose -Message 'Mail item created.'
            }
            Catch
            {
                Write-Error "Failed to create mail item! ($($_.Exception.Message))"
                $ShouldProcess = $False
                Return
            }

            Foreach ($Recipient In $To)
            {
                Try
                {
                    $Mail.Recipients.Add($Recipient) | Out-Null
                }
                Catch
                {
                    Write-Error "Failed to add recipient $Recipient"
                    $ShouldProcess = $False
                    Return
                }
                Write-Verbose -Message "Added recipient $Recipient"
            }

            If ($Subject.Length -GT 0)
            {
                $Mail.Subject = $Subject
            }
            Else
            {
                Write-Warning 'No email subject was supplied.'
            }
            If ($Body.Length -GT 0)
            {
                If ($BodyAsHTML)
                {
                    $Mail.HTMLBody = $Body
                }
                Else
                {
                    $Mail.Body = $Body
                }
            }
            Else
            {
                Write-Warning 'No email body was supplied.'
            }

            Foreach ($File In $Attachments)
            {
                Try
                {
                    # TODO: Suppress warning dialog box when attempting to send an 'unsafe' attachment?
                    $Mail.Attachments.Add($File) | Out-Null
                }
                Catch
                {
                    Write-Error "Error adding $File as an attachment!"
                    $ShouldProcess = $False
                    Return
                }
            }

            If ($VotingOptions.Count -GT 0)
            {
                $Mail.VotingOptions = $VotingOptions -Join ';'
            }

            Switch ($Priority)
            {
                'High'
                { 
                    $Mail.Importance = 2
                }
                'Low' 
                { 
                    $Mail.Importance = 0
                }
                'Normal' 
                { 
                    $Mail.Importance = 1
                }
                Default 
                { 
                    Write-Error "Debug priority!"
                    $ShouldProcess = $False
                    Return
                }
            }

            $Mail.Send()
        }
    }
    END
    {
        If ($Outlook)
        {
            If (-Not($OutlookWasAlreadyRunning))
            {
                $Outlook.Quit()
            }
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
        }
    }
}

Send-OutlookMailMessage -To 'ryan@myotherpcisacloud.com' -Subject 'Can you create an Outlook poll via Powershell?' -Body "

Well, can you?

" -VotingOptions 'Yes','No','IDK' -BodyAsHTML

Comments (2) -

What app do you code in for powershell.  ISE?

Yes, if it's more than two lines, then I use ISE.  You really can't beat it now that it has intellisense/autocomplete.  I dislike all the third party ISE knockoffs.

Comments are closed