2022-12-04 10:40:18 +01:00
|
|
|
## The *send-email.ps1* Script
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script sends an email message.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
```powershell
|
2023-05-26 12:20:18 +02:00
|
|
|
/home/mf/Repos/PowerShell/Scripts/send-email.ps1 [[-From] <String>] [[-To] <String>] [[-Subject] <String>] [[-Body] <String>] [[-SMTPServer] <String>] [<CommonParameters>]
|
|
|
|
|
|
|
|
-From <String>
|
|
|
|
Specifies the sender email address
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-To <String>
|
|
|
|
Specifies the recipient email address
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-Subject <String>
|
|
|
|
Specifies the subject line
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 3
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-Body <String>
|
|
|
|
Specifies the body message
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 4
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-SMTPServer <String>
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 5
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
```powershell
|
|
|
|
PS> ./send-email
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Notes
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Related Links
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2022-11-17 20:02:26 +01:00
|
|
|
## Source Code
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Sends an email message
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script sends an email message.
|
2023-05-26 12:20:18 +02:00
|
|
|
.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
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
|
|
|
PS> ./send-email
|
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2023-05-26 12:20:18 +02:00
|
|
|
param([string]$From = "", [string]$To = "", [string]$Subject = "", [string]$Body = "", [string]$SMTPServer = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
try {
|
2023-05-26 12:20:18 +02:00
|
|
|
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)
|
2022-11-17 20:02:26 +01:00
|
|
|
$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
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2021-11-08 21:36:42 +01:00
|
|
|
*Generated by convert-ps2md.ps1 using the comment-based help of send-email.ps1*
|