Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell cmdlet that sends an e-mail via Outlook, with address and subject as arguments and message body from pipe
Function Global:Send-Email {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$True,Position=0)]
[String]$Address,
[Parameter(Mandatory=$True,Position=1)]
[String]$Subject,
[Parameter(ValueFromPipeline=$True)]
[String[]]$BodyLine
)
Begin {
$Body = ""
}
Process {
$Body += "$BodyLine`r`n"
}
End {
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.BodyFormat = 1
$Mail.To = "$Address"
$Mail.Subject = "$Subject"
$Mail.Body = "$Body"
$Mail.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
$Outlook = $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment