-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathSend-SRMail.ps1
115 lines (99 loc) · 4.66 KB
/
Send-SRMail.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#Requires -Version 5.0
function SRSendMail{
<#
.SYNOPSIS
Function for send mail. You can use the function from other scripts
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.PARAMETER MailSender
Address from which the mail is sent
.PARAMETER MailRecipients
Addresses to which the mail is sent.
Enter names (optional) and the e-mail address, such as "John Doe <[email protected]>".
Use the comma to separate the addresses
.PARAMETER MailSubject
Subject of the email message
.PARAMETER MailBody
Body of the email message
.Parameter MailBodyEncoding
Type of encoding for the body
.PARAMETER MailUseSsl
Uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer to send mail
.PARAMETER MailPriority
Priority of the email message. The acceptable values for this parameter are: Normal, High, Low
.PARAMETER MailServer
Name of the SMTP server that sends the e-mail message.
The default value is the value of the $PSEmailServer preference variable
.PARAMETER MailServerCredential
User account that has permission to perform this action. The default is the current user.
.PARAMETER CopyRecipients
E-mail addresses to which a carbon copy (CC) of the e-mail message is sent.
Enter names (optional) and the e-mail address, such as "John Doe <[email protected]>".
Use the comma to separate the addresses
.PARAMETER Attachments
Path and file names of files to be attached to the e-mail message.
Use the comma to separate the files
.Parameter HtmlBody
Value of the Body parameter contains HTML
#>
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[string]$MailSender,
[parameter(Mandatory = $true)]
[string]$MailRecipients,
[parameter(Mandatory = $true)]
[string]$MailSubject,
[string]$MailBody,
[bool]$MailUseSsl,
[ValidateSet('Normal','High','Low')]
[string]$MailPriority ='Normal',
[ValidateSet('UTF8','ASCII','Default','UTF32','BigEndianUnicode','Byte','OEM','String','Unicode','UTF7','UTF8BOM','UTF8NoBOM')]
[string]$MailBodyEncoding = 'UTF8',
[string]$MailServer ,
[PSCredential]$MailServerCredential,
[string]$CopyRecipients,
[string]$Attachments,
[bool]$HtmlBody = $true
)
try{
if([System.String]::IsNullOrEmpty($MailBody) -eq $true){
$MailBody = ' '
}
$toTmp = $MailRecipients.Split(',')
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'BodyAsHtml' = $HtmlBody
'To' = $toTmp
'Subject' = $MailSubject
'Body' = $MailBody
'From' = $MailSender
'Priority' = $MailPriority
'UseSsl' = $MailUseSsl
'Encoding' = $MailBodyEncoding
}
if([System.String]::IsNullOrWhiteSpace($MailServer) -eq $false){ # Server
$cmdArgs.Add('SmtpServer' ,$MailServer)
}
if([System.String]::IsNullOrWhiteSpace($CopyRecipients) -ne $true){ # CC
$ccTmp = $CopyRecipients.Split(',')
$cmdArgs.Add('Cc', $ccTmp)
}
if([System.String]::IsNullOrWhiteSpace($Attachments) -ne $true){ # Attachments
$filTmp = $Attachments.Split(',')
$cmdArgs.Add('Attachments', $filTmp)
}
if($null -ne $MailServerCredential){# Credential
$cmdArgs.Add('Credential', $MailServerCredential)
}
Send-MailMessage @cmdArgs
Write-Output "Mail sent out"
}
catch{
throw
}
}