Using Powershell to Send an Email (May 1 2014)
A few months back I switched jobs, I decided this was as good of a time as any to switch from using vbs to do my scripting to using Powershell. One of the first things I was looking to do is have my scripts send out an email for one reason or another.
Goal: Simple use Powershell to send an email.
Dependences: This solution requires that the host running the script has the ability to send email without authenticating. Also known as relaying.
The first thing we’re going to want to do is make the function that sends the mail. Since I want to reuse this I’m going to make the function as generic as possible. You might be tempted to hard code some of the information but that’s just bad mojo.
function sendMail ($From, $ReplyTo, $To, $Subject, $Body, $SMTPServer)
{
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($SMTPServer)
#Email structure
$msg.From = $From
$msg.ReplyTo = $ReplyTo
$msg.To.Add($To)
$msg.subject = $Subject
$msg.body = $Body
#Sending email
$smtp.Send($msg)
}
To use this function we’re going to need to call it. Example:
sendMail -From "[email protected]" -ReplyTo "[email protected]" -To "[email protected]" -Subject "This is my Subject" -Body "This is the Email body" -SMTPServer "mail.example.com"
Now, a couple things. The -From and the -Reply can be anything since I’m using my mail server that I have configured to trust me. Because I don’t really care about reply email I usually pick an address in the form [email protected]. I could just as easily sent this from [email protected] or with a legit return path or any combo of the above.
In reality I wouldn’t hard code the fields when calling the function because as I said, bad mojo. I’d really call it using something like.
sendMail -From $SendFrom -ReplyTo $ReplyTo -To $SendTo -Subject $EmailSubject -Body $EmailBody -SMTPServer $EmailServer
With the $SendFrom, $ReplyTo, $SendTo, $EmailSubject, $EmailBody, $EmailServer being populated in my script.
Now lets put this together and show you what it would look like if I were to make a production script.
#***************************************************************************************************
# File: Email.ps1
# Purpose: Send an email
# Version: 1.0
# Date: 11:14 AM Thursday, May 01, 2014
# Created by: Jeff Frye
#**************************************************************************************************
#----- Config -----#
$SMTPServer = "mail.example.com"
$From = "[email protected]"
$ReplyTo = "[email protected]"
$To = "[email protected]"
#-----------------------------------------------------------------------------------------------------------------------------------------#
#----- No Config Below this Line -----#
#-----------------------------------------------------------------------------------------------------------------------------------------#
function sendMail ($From, $ReplyTo, $To, $Subject, $Body, $SMTPServer)
{
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($SMTPServer)
#Email structure
$msg.From = $From
$msg.ReplyTo = $ReplyTo
$msg.To.Add($To)
$msg.subject = $Subject
$msg.body = $Body
#Sending email
$smtp.Send($msg)
}
$Subject = "Example Subject"
$Body = "Example Body"
sendMail -From $From -ReplyTo $ReplyTo -To $To -Subject $Subject -Body $Body -SMTPServer $SMTPServer