Skip to content

Instantly share code, notes, and snippets.

@deed02392
Created September 3, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deed02392/b5d537dc0b1b42665edb to your computer and use it in GitHub Desktop.
Save deed02392/b5d537dc0b1b42665edb to your computer and use it in GitHub Desktop.
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