Skip to content

Instantly share code, notes, and snippets.

@evoelker
Created January 29, 2019 20:52
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 evoelker/69a772194de1e81306260d7edd37157e to your computer and use it in GitHub Desktop.
Save evoelker/69a772194de1e81306260d7edd37157e to your computer and use it in GitHub Desktop.
# 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