Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Function to send email
function sendMail
Function to send email to supplied email address.
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.
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>"
sendMail -EmailAddress "<To email address" -Subject "<Email subject line>" -Message "<Email message body>"
sendMail -EmailAddress "" -Subject "Test email" -Message "Test Body: This is a test."
param (
[string]$Subject = "Emergency Site Shutdown - $date", # Default value if none is provided.
# 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