-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-Email-Cmdlet.ps1
198 lines (177 loc) · 8.51 KB
/
Send-Email-Cmdlet.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#
.Synopsis
This function sends email using SMTP Client.
.Description
This function sends email using SMTP Client to the email addresses in (To, Cc, Bcc).
The function can also send files as attachments in emails. It can also use filters (Include/Exclude) to match the required files in the supplied path.
.Parameter SMTPServer
This parameter is set as the 'SMTP Server'.
This parameter is mandatory.
.Parameter SMTPPort
This parameter is set as the 'SMTP port'.
This parameter is mandatory.
.Parameter From
This parameter is set as the 'From' email address. Also used as a user to authenticate the email.
This parameter is mandatory.
.Parameter Password
This parameter is set as the 'Password' for the from user email address.
This parameter can have null or empty string value in case there is no password required.
This parameter is mandatory.
.Parameter To
This parameter is set as the 'To' email (recipient) addresses. Uses a comma separated value for multiple email addresses.
This parameter is mandatory.
.Parameter Cc
This parameter is set as the 'Cc' email (recipient) addresses. Uses a comma separated value for multiple email addresses.
.Parameter Bcc
This parameter is set as the 'Bcc' email (recipient) addresses. Uses a comma separated value for multiple email addresses.
.Parameter Subject
This parameter is set as the 'Subject' in the email.
.Parameter Body
This parameter is set as the 'Body' in the email.
.Parameter AttachmentFolderPath
This parameter is set as the 'Attachment Folder Path' for attachments to add to the email (Ignores directories and only looks for files).
.Parameter AttachmentIncludeFilter
This parameter is set as the 'Attachment Type Include Match' for attachments to add to the email.
.Parameter AttachmentExcludeFilter
This parameter is set as the 'Attachment Type Exclude Match' for attachments to add to the email.
.Example
Send-Email -SMTPServer "<SMTP Server Address>" -SMTPPort "<SMTP Server Port>" -From "<From Address>" -Password "<Password>" -To "<To Addresses>" -Subject "<Subject Line>"
This command sends email using mandatory parameters.
.Example
Send-Email -SMTPServer "<SMTP Server Address>" -SMTPPort "<SMTP Server Port>" -From "<From Address>" -Password "<Password>" -To "<To Addresses>" -Cc "<Cc Addresses>" -Bcc "<Bcc Addresses>" -Subject "<Subject Line>" -Body "<Email Body>" -AttachmentFolderPath "<Attachment Folder Path>" -AttachmentIncludeFilter "<Include Filter>" -AttachmentExcludeFilter "<Exclude Filter>"
This command sends email using all the parameters.
#>
Function Send-Email
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$SMTPServer,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$SMTPPort,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$From,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[AllowNull()]
[AllowEmptyString()]
[String]$Password,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$To,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Cc,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Bcc,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Subject,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Body,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$AttachmentFolderPath,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$AttachmentIncludeFilter,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$AttachmentExcludeFilter
)
Begin
{
Function Write-Log([string]$logMessage)
{
$print = $logMessage.length + 6
Write-Output $("-" * ($print))
Write-Output "[[ $logMessage ]]"
write-output $("-" * ($print))
}
$toRecipients = $To.Split(',')
$ccRecipients = $Cc.Split(',')
$bccRecipients = $Bcc.Split(',')
$include = $AttachmentIncludeFilter
$exclude = $AttachmentExcludeFilter
if(![string]::IsNullOrEmpty($AttachmentFolderPath))
{
if(![string]::IsNullOrEmpty($AttachmentIncludeFilter) -and [string]::IsNullOrEmpty($AttachmentExcludeFilter))
{
$attachmentFiles = Get-ChildItem -Path $path -Filter $include -Recurse -ErrorAction SilentlyContinue -Force
}
elseif ([string]::IsNullOrEmpty($AttachmentIncludeFilter) -and ![string]::IsNullOrEmpty($AttachmentExcludeFilter))
{
$attachmentFiles = Get-ChildItem -Path $path -Exclude $exclude -Recurse -ErrorAction SilentlyContinue -Force
}
elseif((![string]::IsNullOrEmpty($AttachmentIncludeFilter)) -or (![string]::IsNullOrEmpty($AttachmentExcludeFilter)))
{
$attachmentFiles = Get-ChildItem -Path $path -Filter $include -Exclude $exclude -Recurse -ErrorAction SilentlyContinue -Force
}
else
{
$attachmentFiles = Get-ChildItem -Path $path -Recurse -ErrorAction SilentlyContinue -Force
}
$path = $AttachmentFolderPath
$attachments = New-Object System.Collections.ArrayList
foreach ($attachmentFile in $attachmentFiles)
{
$filename = $attachmentFile | Select-Object -Property Name
$filepath = Join-Path $path $filename.Name
if((Test-Path -Path $filepath -PathType leaf))
{
$attachments.add($filepath)
}
}
}
}
Process
{
try
{
$message = New-Object System.Net.Mail.MailMessage
$message.from = $From
foreach ($recipient in $toRecipients)
{
$message.to.add($recipient)
}
if(![string]::IsNullOrEmpty($ccRecipients))
{
foreach ($recipient in $ccRecipients)
{
$message.cc.add($recipient)
}
}
if(![string]::IsNullOrEmpty($bccRecipients))
{
foreach ($recipient in $bccRecipients)
{
$message.bcc.add($recipient)
}
}
if(![string]::IsNullOrEmpty($Subject))
{
$message.subject = $Subject
}
if(![string]::IsNullOrEmpty($Body))
{
$message.body = $Body
}
foreach ($attachment in $attachments)
{
$message.attachments.add($attachment)
}
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12;
$smtp.UseDefaultCredentials = $false;
$smtp.DeliveryMethod = [Net.Mail.SmtpDeliveryMethod]::Network;
$smtp.Credentials = New-Object System.Net.NetworkCredential($From, $Password);
$smtp.send($message)
}
catch
{
Write-Log(":: SCRIPT ERROR ::")
Write-Log("Error Message :: $_.Exception.Message")
Break
}
}
End
{
Write-Log(":::: Email Sent. ::::")
}
}
<# Send-Email -SMTPServer "<SMTP Server Address>" -SMTPPort "<SMTP Server Port>" -From "<From Address>" -Password "<Password>" -To "<To Addresses>" -Subject "<Subject Line>" #>
<# Send-Email -SMTPServer "<SMTP Server Address>" -SMTPPort "<SMTP Server Port>" -From "<From Address>" -Password "<Password>" -To "<To Addresses>" -Cc "<Cc Addresses>" -Bcc "<Bcc Addresses>" -Subject "<Subject Line>" -Body "<Email Body>" -AttachmentFolderPath "<Attachment Folder Path>" -AttachmentIncludeFilter "<Include Filter>" -AttachmentExcludeFilter "<Exclude Filter>" #>