Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell: Send Email
Function Send-Email{
Used to send data as an email to a list of addresses
This function is used to send an email to a list of addresses. The body can be provided in HTML or plain-text
Mandatory. The email addresses of who you want to send the email from. Example: ""
Mandatory. The email addresses of where to send the email to. Seperate multiple emails by ",". Example: ","
.PARAMETER EmailSubject
Mandatory. The subject of the email you want to send. Example: "Cool Script - [" + (Get-Date).ToShortDateString() + "]"
Mandatory. The body of the email in plain-text or HTML format."
Mandatory. Boolean. True = email in HTML format (therefore body must be in HTML code). False = email in plain-text format"
None - other than parameters above
Email sent to the list of addresses specified
Version: 1.0
Author: Luca Sturlese
Creation Date: 18/09/14
Purpose/Change: Initial function development
Send-Email -EmailFrom "" -EmailTo "," -EmailSubject "Cool Script - [" + (Get-Date).ToShortDateString() + "]" -EmailBody $sHTMLBody -EmailHTML $True
Send-Email -EmailFrom "" -EmailTo "," -EmailSubject "Cool Script - [" + (Get-Date).ToShortDateString() + "]" -EmailBody "This is a test" -EmailHTML $False
Param ([Parameter(Mandatory=$true)][string]$EmailFrom, [Parameter(Mandatory=$true)][string]$EmailTo, [Parameter(Mandatory=$true)][string]$EmailSubject, [Parameter(Mandatory=$true)][string]$EmailBody, [Parameter(Mandatory=$true)][boolean]$EmailHTML)
#SMTP Settings
$sSMTPServer = "Set your SMTP Server here"
#Create Embedded HTML Email Message
$oMessage = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$oMessage.Subject = $EmailSubject
$oMessage.IsBodyHtml = $EmailHTML
$oMessage.Body = $EmailBody
#Create SMTP object and send email
$oSMTP = New-Object Net.Mail.SmtpClient($sSMTPServer)
Exit 0
Exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment