forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-email.ps1
executable file
·44 lines (41 loc) · 1.22 KB
/
send-email.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<#
.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
}