Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Function to send email
function sendMail
{
<#
.SYNOPSIS
Function to send email to supplied email address.
.DESCRIPTION
Function takes the provided email address, subject and message, formates an email and sends it to the configured SMTP server.
The subject line is optional.
The default subject string can be changed if you don't want to supply a subject parameter.
.NOTES
The following variable need to be set for this function to work correctly:
$date = (Get-Date -Format "M-d-yyyy")
$starTime = (Get-Date -Format "HH:mm:ss")
$smtpServ = "<SMTP Server>"
$fromAddr = "<From email address>"
.EXAMPLE
sendMail -EmailAddress "<To email address" -Subject "<Email subject line>" -Message "<Email message body>"
sendMail -EmailAddress "eryk.voelker@hpe.com" -Subject "Test email" -Message "Test Body: This is a test."
#>
param (
[Parameter(Mandatory)]
[string]$EmailAddress,
[Parameter()]
[string]$Subject = "Emergency Site Shutdown - $date", # Default value if none is provided.
[Parameter(Mandatory)]
[string]$Message
)
# Send email with formated message
Send-MailMessage -From $fromAddr -To $EmailAddress -Subject $Subject -Body $Message -SmtpServer $smtpServ -Port $smtpPort
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment