Skip to content

Instantly share code, notes, and snippets.

@jm-welch
Last active December 20, 2015 21:29
Show Gist options
  • Save jm-welch/6198171 to your computer and use it in GitHub Desktop.
Save jm-welch/6198171 to your computer and use it in GitHub Desktop.
My version of jfrmilner's Send-MailMessages function
function Send-MailMessages {
<#
.SYNOPSIS
Send a batch of emails very quickly.
.DESCRIPTION
This script is designed to assist in generating email messages for testing internal/external message flow for your messaging infrastructure.
The ability to quickly send a batch of messages with an attachment on a schedule can help track flow issues, to confirm mail routing, or to test 'high-load' scenarios.
.EXAMPLE
Send-MailMessages -Count 10
Send 10 messages with no delay.
.EXAMPLE
Send-MailMessages -Count 10 -Batch 1
Send 10 messages with no delay, and include "Batch 1" in the Subject.
.EXAMPLE
Send-MailMessages 10 1
Same as Example 2
.EXAMPLE
Send-MailMessages -Count 5 -Delay 300
Send 5 messages 5 minutes (300 seconds) apart.
.EXAMPLE
Send-MailMesages -Count 24 -Delay 3600 -AttachmentSizeMB 2
Send one message an hour for 24 hours, each with a 2 MB attachment.
.PARAMETER Count
Number of email messages to send in this batch
.PARAMETER Batch
(Optional) Prepends "Batch # - " to the subject of the email if set.
.PARAMETER Delay
(Optional) Number of seconds to delay between messages.
If not present or 0, there will be no delay between messages.
.PARAMETER AttachmentSizeMB
(Optional) Attach a file of the given size to the messages.
Files will be created in %TEMP% in the following format: "1mbfile.txt"
Once added, the attachment becomes part of the persistent settings. Run with -Setup to remove.
.PARAMETER Body
(Optional) Customize the text in the body of the email messages.
.PARAMETER TimeStamp
(Optional) If present, include a timestamp in the message subject.
.PARAMETER Setup
Manually trigger the Setup segment, and exit the script once complete.
.LINK
This Script: http://creativelazy.blogspot.com/2013/08/send-batches-of-test-email-from.html
Original : http://jfrmilner.wordpress.com/2010/08/26/send-mailmessages
.NOTES
Based on jfrmilner's Send-MailMessages.ps1 from http://jfrmilner.wordpress.com/2010/08/26/send-mailmessages
File Name: Send-MailMessages.ps1
Authors : jfrmilner ([email protected])
jm-welch ([email protected])
Requires : Powershell v2
Requires : Exchange Management Shell (to auto-discover the SMTP server)
Legal : This script is provided "AS IS" with no warranties or guarantees, and confers no rights.
You may use, modify, reproduce, and distribute this script file in any way provided that you agree to give the original author credit.
Version : v1.0 - 2010 Aug 08 (jfrmilner) - First Version http://poshcode.org/*
Version : v1.1 - 2012 April 26 (jfrmilner) - Fixed when only a single HT Server is available.
- Added check for existing file.
- Fixed attachment parameter to use varible.
Version : v2.0 - 2013 Aug 09 (jwelch) - Restructured script with new features/variables.
- Added option for no delay between messages.
- Added option to specify a "Batch" number.
- Made timestamping in Subject optional.
- Added Setup segment to store persistent variables, with check for Exchange function (so EMS isn't required, just recommended)
- Expanded AutoHelp comment with new values
- Added Try/Catch block to exit if we get an SmtpException
- Added test for administrator role to file create
#>
param([Int32]$Count=0,
[int32]$Batch=0,
[Int32]$Delay=0,
[Int32]$AttachmentSizeMB=0,
[string]$Body,
[switch]$TimeStamp,
[switch]$Setup)
if (!(Test-Path variable:MessageParameters) -or $Setup){
Write-Host -ForegroundColor Yellow "`nSetup Persistent Values"
if (!($Setup)) { Write-Host -ForegroundColor Yellow "(You will only have to do this once for this PS session)" }
$To = Read-Host 'Enter the "To" email address'
$From = Read-Host 'Enter the "From" email address'
if (! $Body) { $Body = "Test message, please ignore" }
Try {
Write-Host -ForegroundColor Yellow "`nSMTP servers:"
Get-TransportServer | Select -ExpandProperty Name
$SmtpServer = Read-Host "`nEnter the name of the SMTP server to use (blank to use first server in list)"
if ($SmtpServer -eq '') {
$SmtpServer = @(Get-TransportServer)[0].Name.ToString()
}
}
Catch [System.Management.Automation.CommandNotFoundException] {
Write-Host -ForegroundColor Red "Run this script in EMS to show a list of SMTP servers."
$SmtpServer = Read-Host "Manually specify the SMTP server to use"
}
$global:messageParameters = @{
Body = $Body
From = $From
To = $To
SmtpServer = $SmtpServer
}
}
if ($AttachmentSizeMB) {
if (!(Test-Path $Env:TMP\$($AttachmentSizeMB)mbfile.txt)) {
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host -ForegroundColor Red "Administrative privileges are required to create the attachment file."
Write-Host -ForegroundColor Red "Launch PowerShell using 'Run As Administrator' for this feature."
}
Else { fsutil file createnew $Env:TMP\$($AttachmentSizeMB)mbfile.txt $($AttachmentSizeMB * 1MB) }
}
if ((Test-Path $Env:TMP\$($AttachmentSizeMB)mbfile.txt)) {
$messageParameters.Add("Attachment", "$Env:TMP\$($AttachmentSizeMB)mbfile.txt")
}
}
$msg = "`nSending $Count Mail Message(s)"
if ($Batch -gt 0) { $msg += " as Batch $Batch" }
if ($Delay -gt 0) { $msg += " every $Delay seconds" }
$msg += " with the following parameters:"
Write-Host -ForegroundColor Yellow $msg
$MessageParameters
Write-Host -ForegroundColor Yellow "`nRun 'Send-MailMessages -Setup' to change these values"
if ($Setup) { return }
if ($Count -lt 1) {
Write-Host -ForegroundColor Red "Send aborted - no count provided."
Write-Host -ForegroundColor Red "Use '-Count #' to specify."
return
}
$Subject = "Test email "
if ($TimeStamp) { $Subject += (Get-Date).ToLongTimeString() }
if ($Batch -gt 0) { $Subject = "Batch $batch - " + $Subject }
1..$Count | %{
if ($Delay) { Sleep -Seconds $Delay }
Try { Send-MailMessage @MessageParameters -Subject ("$Subject (" + $_ + "/$Count)") -BodyAsHtml }
Catch [System.Net.Mail.SmtpException] {
Write-Host -ForegroundColor Red ("`nSmtpServer " + $MessageParameters.SmtpServer + " is not valid.")
Write-Host -ForegroundColor Red "Run 'Send-MailMessages -Setup' and specify a valid server."
return
}
}
Write-Host -ForegroundColor Yellow "`nProcessing complete"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment