PowerShell, Office 365, Azure and automation
Send test email with PowerShell

Send test email with PowerShell

It is a quite common situation. when you need to send an email from a PowerShell script that contains a status report or another kind of data. Nevertheless, before you can send a normal email you would like to run a quick test to confirm that you can send email from this server. Despite of PowerShell is the only tool you might have in hand, it is a very powerful one. You can use this just for sending test email, but if you modify it a bit you can use it to send emails. 

<#
    .SYNOPSIS
        Send a test email 

    .DESCRIPTION
        A simple tool to allow an email to be sent from PowerShell for testing email delivery
		
    .PARAMETER sender
        The email address of the person sending the email

    .PARAMETER recipient
        The email address that you want to send the email to

    .PARAMETER
	    The subject to appear in the email
		
    .EXAMPLE
        ./Test-Email.ps1 -sender <my email address> -recipient <destination email address> -subject 'Test email'
#>

param (
	[Parameter(Mandatory=$True)]
	[string]$Recipient,
	[Parameter(Mandatory=$True)]
	[string]$Sender,
	[Parameter(Mandatory=$False)]
	[string]$Subject = "Test email",
	[Parameter(Mandatory=$True)]
	[string]$SMTPserver = "localhost"
)

Send-MailMessage `
	-To $Recipient `
	-From $Sender `
	-Subject $Subject `
	-SMTPServer $SMTPserver

## Script end

If you would like to further than just send test email and send a full email message – use the same commandlet, just add a body for your message.

There are more PowerShell scripts here

powershell logo 190

Leave a Reply

Your email address will not be published. Required fields are marked *