-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-test.psm1
45 lines (33 loc) · 1.92 KB
/
smtp-test.psm1
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
45
# Default values
$defaultSmtpServer = "smtp.gmail.com"
$defaultMessageSubject = "Test mail"
$defaultMessageBody = "Message from PowerShell."
$defaultSmtpPort = 587
$defaultUseSSLInput = "yes"
# Get login information from the user, use default values
$smtpServer = Read-Host `n"Enter the SMTP server address(default: $defaultSmtpServer)"
$smtpServer = if ($smtpServer) { $smtpServer } else { $defaultSmtpServer }
$smtpFrom = Read-Host "Enter the sender email address"
$smtpTo = Read-Host "Enter the recipient email address"
$messageSubject = Read-Host "Enter the email subject(default: $defaultMessageSubject)"
$messageSubject = if ($messageSubject) { $messageSubject } else { $defaultMessageSubject }
$messageBody = Read-Host "Enter email message body(default: $defaultMessageBody)"
$messageBody = if ($messageBody) { $messageBody } else { $defaultMessageBody }
$smtpUsername = Read-Host "Enter the SMTP user name"
$smtpPassword = Read-Host "Enter the SMTP password" -AsSecureString
$smtpPort = Read-Host "Enter the SMTP port(default: $defaultSmtpPort)"
$smtpPort = if ($smtpPort) { [int]$smtpPort } else { $defaultSmtpPort }
$useSSLInput = Read-Host "Do you want to use SSL?(yes/no) (default: $defaultUseSSLInput)"
$useSSLInput = if ($useSSLInput) { $useSSLInput } else { $defaultUseSSLInput }
# Determine the use of SSL
$useSSL = if ($useSSLInput -eq "yes") { $true } else { $false }
# Credential creation
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $smtpUsername, $smtpPassword
# Sending an email
try {
Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $messageSubject -Body $messageBody -SmtpServer $smtpServer -Credential $credentials -Port $smtpPort -UseSsl:$useSSL -ErrorAction Stop
Write-Host "An email was successfully sent to " -NoNewline
Write-Host "$smtpTo." -ForegroundColor Green
} catch {
Write-Host "Failed to send email. Error message: $_" -ForegroundColor Red
}