-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestMessage.ps1
54 lines (49 loc) · 2.1 KB
/
TestMessage.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
45
46
47
48
49
50
51
52
53
54
<#
.SYNOPSIS
Sends an e-mail test message
.PARAMETER from
E-mail address used to send the messages.
Use a valid e-mail address.
.PARAMETER smtpUser
Used to authenticate to the SMTP server. If not supplied the script will take the result from the 'from' parameter.
Use a valid e-mail address.
.PARAMETER smtpPassword
Optional, only if the server requires authentication.
#>
[CmdletBinding(DefaultParameterSetName = 'sendsmail')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '', Justification = 'Will run in Docker, we can''t pass a SecureString.')]
param (
[ValidatePattern('^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*)$')]
[Parameter(Mandatory)]
[string]$smtpServer,
[ValidateRange(1, [System.UInt16]::MaxValue)]
[int]$smtpPort = 25,
[ValidatePattern('^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$')]
[Parameter(Mandatory)]
[string]$from,
[ValidatePattern('^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$')]
[Parameter(Mandatory)]
[string]$destinationAddress,
[string]$smtpUser,
[string]$smtpPassword
)
Set-StrictMode -Version 3.0
Write-Verbose "Starting..."
$body = @'
<p>Hello,</p>
<p>
This is a test e-mail message.<br />
</p>
<p>Thanks,<br>
<p>Your administrador<br>
'@
if (!($smtpUser)) {
$smtpUser = $from
}
$commandExpression = 'Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -From $from -To $destinationAddress -Subject "Test message" -Body $body -BodyAsHtml -Priority "High" -Encoding "utf8" -UseSsl'
if ($smtpPassword) {
$smtpCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $smtpUser, (ConvertTo-SecureString $smtpPassword -AsPlainText -Force)
$commandExpression += ' -Credential $smtpCredential'
}
Invoke-Expression $commandExpression
Write-Verbose "Done."