-
-
Save I90Runner/a5d94851bd072e5060149fca6468ef63 to your computer and use it in GitHub Desktop.
PowerShell: Send Email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Send-Email{ | |
<# | |
.SYNOPSIS | |
Used to send data as an email to a list of addresses | |
.DESCRIPTION | |
This function is used to send an email to a list of addresses. The body can be provided in HTML or plain-text | |
.PARAMETER EmailFrom | |
Mandatory. The email addresses of who you want to send the email from. Example: "[email protected]" | |
.PARAMETER EmailTo | |
Mandatory. The email addresses of where to send the email to. Seperate multiple emails by ",". Example: "[email protected], [email protected]" | |
.PARAMETER EmailSubject | |
Mandatory. The subject of the email you want to send. Example: "Cool Script - [" + (Get-Date).ToShortDateString() + "]" | |
.PARAMETER EmailBody | |
Mandatory. The body of the email in plain-text or HTML format." | |
.PARAMETER EmailHTML | |
Mandatory. Boolean. True = email in HTML format (therefore body must be in HTML code). False = email in plain-text format" | |
.INPUTS | |
None - other than parameters above | |
.OUTPUTS | |
Email sent to the list of addresses specified | |
.NOTES | |
Version: 1.0 | |
Author: Luca Sturlese | |
Creation Date: 18/09/14 | |
Purpose/Change: Initial function development | |
.EXAMPLE | |
Send-Email -EmailFrom "[email protected]" -EmailTo "[email protected], [email protected]" -EmailSubject "Cool Script - [" + (Get-Date).ToShortDateString() + "]" -EmailBody $sHTMLBody -EmailHTML $True | |
.EXAMPLE | |
Send-Email -EmailFrom "[email protected]" -EmailTo "[email protected], [email protected]" -EmailSubject "Cool Script - [" + (Get-Date).ToShortDateString() + "]" -EmailBody "This is a test" -EmailHTML $False | |
#> | |
[CmdletBinding()] | |
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) | |
Begin{} | |
Process{ | |
Try{ | |
#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) | |
$oSMTP.Send($oMessage) | |
Exit 0 | |
} | |
Catch{ | |
Exit 1 | |
} | |
} | |
End{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment