Script: *send-email.ps1* ======================== This PowerShell script sends an email message. Parameters ---------- ```powershell PS> ./send-email.ps1 [[-From] ] [[-To] ] [[-Subject] ] [[-Body] ] [[-SMTPServer] ] [] -From Specifies the sender email address Required? false Position? 1 Default value Accept pipeline input? false Accept wildcard characters? false -To Specifies the recipient email address Required? false Position? 2 Default value Accept pipeline input? false Accept wildcard characters? false -Subject Specifies the subject line Required? false Position? 3 Default value Accept pipeline input? false Accept wildcard characters? false -Body Specifies the body message Required? false Position? 4 Default value Accept pipeline input? false Accept wildcard characters? false -SMTPServer Required? false Position? 5 Default value Accept pipeline input? false Accept wildcard characters? false [] This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. ``` Example ------- ```powershell PS> ./send-email ``` Notes ----- Author: Markus Fleschutz | License: CC0 Related Links ------------- https://github.com/fleschutz/PowerShell Script Content -------------- ```powershell <# .SYNOPSIS Sends an email message .DESCRIPTION This PowerShell script sends an email message. .PARAMETER From Specifies the sender email address .PARAMETER To Specifies the recipient email address .PARAMETER Subject Specifies the subject line .PARAMETER Body Specifies the body message .EXAMPLE PS> ./send-email .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> param([string]$From = "", [string]$To = "", [string]$Subject = "", [string]$Body = "", [string]$SMTPServer = "") try { if ($From -eq "") { $From = Read-Host "Enter sender email address" } if ($To -eq "") { $To = Read-Host "Enter recipient email address" } if ($Subject -eq "") { $Subject = Read-Host "Enter subject line" } if ($Body -eq "") { $Body = Read-Host "Enter body message" } if ($SMTPServer -eq "") { $SMTPServer = Read-Host "Enter SMTP server" } $msg = New-Object Net.Mail.MailMessage $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $msg.From = $From $msg.ReplyTo = $From $msg.To.Add($To) $msg.subject = $Subject $msg.body = $Body $smtp.Send($msg) "✔️ Message sent." exit 0 # success } catch { "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" exit 1 } ``` *(generated by convert-ps2md.ps1 using the comment-based help of send-email.ps1 as of 05/19/2024 10:25:25)*